Skip to content

Commit

Permalink
Added /fixtures & /fixtures/matchweek feature to allow user to retrie…
Browse files Browse the repository at this point in the history
…ve and see fixtures based on matchweek
  • Loading branch information
augustinecyr committed Apr 22, 2023
1 parent a13a892 commit 018a090
Show file tree
Hide file tree
Showing 17 changed files with 641 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.authorizeRequests()
.antMatchers("/login", "/error", "/auth/google/callback",
"/login/oauth2/code/github", "/login/oauth/access_token", "/login/oauth/access_token?code=",
"/token", "/contact", "/clubs/squad", "/players", "/players/stats", "/googlemap" ,"/players/delete")
"/token", "/contact", "/clubs/squad", "/players", "/players/stats", "/googlemap" ,"/players/delete","/fixtures","/fixtures/matchweek")
.permitAll()
.anyRequest()
.authenticated()
Expand All @@ -38,8 +38,7 @@ public WebSecurityCustomizer webSecurityCustomizer() {
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("http://localhost:4200"));
configuration.setAllowedOrigins(Arrays.asList("https://vttpminiproject-02.vercel.app"));
configuration.setAllowedOrigins(Arrays.asList("http://localhost:4200" , "https://vttpminiproject-02.vercel.app" ));
configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE"));
configuration.setAllowedHeaders(Arrays.asList("*"));
configuration.setExposedHeaders(Arrays.asList("Authorization"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.sg.backend.controllers;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sg.backend.models.Fixtures;
import com.sg.backend.service.FixturesService;

@RestController
@RequestMapping
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class FixturesController {

@Autowired
private FixturesService fixturesSvc;

@GetMapping(path = "/fixtures/matchweek")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public List<Fixtures> getFixtures(HttpSession sess, @RequestParam String dayID){
fixturesSvc.getFixtures(dayID);
List<Fixtures> fixtures = fixturesSvc.getFixtures(dayID);
return fixtures;
}

}
125 changes: 125 additions & 0 deletions backend/src/main/java/com/sg/backend/models/Fixtures.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package com.sg.backend.models;

import java.io.StringReader;

import jakarta.json.Json;
import jakarta.json.JsonObject;
import jakarta.json.JsonReader;
import jakarta.json.JsonValue;

public class Fixtures {
private String fullMatchDate;
private String matchTime;
private String homeClubName;
private String homeClubImage;
private String awayClubName;
private String awayClubImage;
private String result;

public String getFullMatchDate() {
return fullMatchDate;
}

public void setFullMatchDate(String fullMatchDate) {
this.fullMatchDate = fullMatchDate;
}

public String getMatchTime() {
return matchTime;
}

public void setMatchTime(String matchTime) {
this.matchTime = matchTime;
}

public String getHomeClubName() {
return homeClubName;
}

public void setHomeClubName(String homeClubName) {
this.homeClubName = homeClubName;
}

public String getHomeClubImage() {
return homeClubImage;
}

public void setHomeClubImage(String homeClubImage) {
this.homeClubImage = homeClubImage;
}

public String getAwayClubName() {
return awayClubName;
}

public void setAwayClubName(String awayClubName) {
this.awayClubName = awayClubName;
}

public String getAwayClubImage() {
return awayClubImage;
}

public void setAwayClubImage(String awayClubImage) {
this.awayClubImage = awayClubImage;
}

public String getResult() {
return result;
}

public void setResult(String result) {
this.result = result;
}

public static Fixtures create(JsonObject json) {
final Fixtures fixture = new Fixtures();
fixture.setFullMatchDate(json.getString("fullMatchDate"));
fixture.setMatchTime(json.getString("matchTime"));
fixture.setHomeClubName(json.getString("homeClubName"));
fixture.setHomeClubImage(json.getString("homeClubImage"));
fixture.setAwayClubName(json.getString("awayClubName"));
fixture.setAwayClubImage(json.getString("awayClubImage"));
fixture.setResult(json.getString("result"));
return fixture;
}

public static Fixtures create(String json) {
try (StringReader strReader = new StringReader(json)) {
JsonReader j = Json.createReader(strReader);
return create(j.readObject());
}
}

public JsonObject toJson() {
return Json.createObjectBuilder()
.add("fullMatchDate", this.fullMatchDate)
.add("matchTime", this.matchTime)
.add("homeClubName", this.homeClubName)
.add("homeClubImage", this.homeClubImage)
.add("awayClubName", this.awayClubName)
.add("awayClubName", this.awayClubImage)
.add("result", this.result)
.build();
}

public JsonValue toJsonValue() {
return Json.createObjectBuilder()
.add("fullMatchDate", this.fullMatchDate)
.add("matchTime", this.matchTime)
.add("homeClubName", this.homeClubName)
.add("homeClubImage", this.homeClubImage)
.add("awayClubName", this.awayClubName)
.add("awayClubName", this.awayClubImage)
.add("result", this.result)
.build();
}

@Override
public String toString() {
return "Fixtures [fullMatchDate=" + fullMatchDate + ", matchTime=" + matchTime + ", homeClubName="
+ homeClubName + ", homeClubImage=" + homeClubImage + ", awayClubName=" + awayClubName
+ ", awayClubImage=" + awayClubImage + ", result=" + result + "]";
}

}
100 changes: 100 additions & 0 deletions backend/src/main/java/com/sg/backend/service/FixturesService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.sg.backend.service;

import java.io.StringReader;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import com.sg.backend.models.Fixtures;

import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
import jakarta.json.JsonReader;
import jakarta.json.JsonValue;

@Service
public class FixturesService {

private static final String URL = "https://transfermarket.p.rapidapi.com/matches/list-by-game-plan";

@Value("${X_RapidAPI_Key}")
private String rapidAPI;

public List<Fixtures> getFixtures(String dayID) {
String payload;
System.out.println("Attempting request to TransferMarkt.com....");

try {

String url = UriComponentsBuilder
.fromUriString(URL)
.queryParam("seasonID", "2022")
.queryParam("leagueID", "GB1")
.queryParam("domain", "com")
.queryParam("dayID", dayID)
.encode()
.toUriString();

RequestEntity<Void> req = RequestEntity
.get(url)
.header("X-RapidAPI-Key", "%s".formatted(rapidAPI))
.build();

System.out.println(">>> [url]: " + url);

System.out.println("-----------------------------------------------------------");

RestTemplate template = new RestTemplate();

ResponseEntity<String> resp;
resp = template.exchange(req, String.class);

payload = resp.getBody();

System.out.println(">>> [payload]: " + payload);
System.out.println("-----------------------------------------------------------");

} catch (Exception ex) {
System.err.printf("Error: %s\n", ex.getMessage());
return Collections.emptyList();
}

List<Fixtures> fixtures = new LinkedList<>();

try (StringReader strReader = new StringReader(payload)) {
JsonReader r = Json.createReader(strReader);
JsonObject j = r.readObject();

JsonArray fixturesArr = j.getJsonArray("playDayMatches");

System.out.println("Total number of fixtures: " + fixturesArr.size());
System.out.println("-----------------------------------------------------------");

for (JsonValue v : j.getJsonArray("playDayMatches")) {
if (v.getValueType() == JsonValue.ValueType.OBJECT) {
try {
fixtures.add(Fixtures.create((JsonObject) v));
} catch (Exception ex) {
System.err.printf("Error: %s\n", ex.getMessage());
}
}
}
for (Fixtures fixture : fixtures) {
System.out.println(fixture.getFullMatchDate() + ": " + fixture.getMatchTime() + ": "
+ fixture.getHomeClubName() + ": " + fixture.getHomeClubImage() + ": "
+ fixture.getAwayClubName() + ": " + fixture.getAwayClubImage() + ": " + fixture.getResult());
}

}
return fixtures;

}
}
8 changes: 8 additions & 0 deletions frontend/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ import { MatListModule } from '@angular/material/list';
import { MatGridListModule } from '@angular/material/grid-list';
import { AboutusComponent } from './components/aboutus.component';
import { MapService } from './map.service';
import { FixturesComponent } from './components/fixtures.component';
import { FixturesService } from './fixtures.service';
import { MatSelectModule } from '@angular/material/select';
const appRoutes: Routes = [
{ path: '', component: MainComponent },
// e.g. using :email makes <email> dynamic
Expand All @@ -46,6 +49,8 @@ const appRoutes: Routes = [
{ path: 'players/stats', component: StatsComponent },
{ path: 'players', component: StatsComponent },
{ path: 'about', component: AboutusComponent },
{ path: 'fixtures', component: FixturesComponent },
{ path: 'fixtures/matchweek', component: FixturesComponent },
{ path: '**', redirectTo: '/', pathMatch: 'full' },
];

Expand All @@ -61,6 +66,7 @@ const appRoutes: Routes = [
ClubsComponent,
StatsComponent,
AboutusComponent,
FixturesComponent,
],
imports: [
FormsModule,
Expand All @@ -83,13 +89,15 @@ const appRoutes: Routes = [
MatDividerModule,
MatListModule,
MatGridListModule,
MatSelectModule,
],
providers: [
UserService,
ContactService,
ClubService,
StatsService,
MapService,
FixturesService,
],
bootstrap: [AppComponent],
})
Expand Down
1 change: 1 addition & 0 deletions frontend/src/app/components/aboutus.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Dashboard <mat-icon matSuffix>arrow_drop_down</mat-icon>
</button>
<mat-menu #dashboardMenu="matMenu">
<button mat-menu-item routerLink="/fixtures">Fixtures</button>
<button mat-menu-item routerLink="/players">Players</button>
<button mat-menu-item routerLink="/clubs">Clubs</button>
</mat-menu>
Expand Down
1 change: 1 addition & 0 deletions frontend/src/app/components/account.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Dashboard <mat-icon matSuffix>arrow_drop_down</mat-icon>
</button>
<mat-menu #dashboardMenu="matMenu">
<button mat-menu-item routerLink="/fixtures">Fixtures</button>
<button mat-menu-item routerLink="/players">Players</button>
<button mat-menu-item routerLink="/clubs">Clubs</button>
</mat-menu>
Expand Down
1 change: 1 addition & 0 deletions frontend/src/app/components/clubs.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Dashboard <mat-icon matSuffix>arrow_drop_down</mat-icon>
</button>
<mat-menu #dashboardMenu="matMenu">
<button mat-menu-item routerLink="/fixtures">Fixtures</button>
<button mat-menu-item routerLink="/players">Players</button>
<button mat-menu-item routerLink="/clubs">Clubs</button>
</mat-menu>
Expand Down
1 change: 1 addition & 0 deletions frontend/src/app/components/contact.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Dashboard <mat-icon matSuffix>arrow_drop_down</mat-icon>
</button>
<mat-menu #dashboardMenu="matMenu">
<button mat-menu-item routerLink="/fixtures">Fixtures</button>
<button mat-menu-item routerLink="/players">Players</button>
<button mat-menu-item routerLink="/clubs">Clubs</button>
</mat-menu>
Expand Down
Loading

1 comment on commit 018a090

@vercel
Copy link

@vercel vercel bot commented on 018a090 Apr 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.