Skip to content

Commit

Permalink
/players/stats will return the statistics of the player based on ID
Browse files Browse the repository at this point in the history
  • Loading branch information
augustinecyr committed Apr 10, 2023
1 parent 742bd12 commit 807a944
Show file tree
Hide file tree
Showing 16 changed files with 473 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable().antMatcher("/**")
.authorizeRequests()
.antMatchers("/login", "/error", "/auth/google/callback",
"/login/oauth2/code/github","/login/oauth/access_token","/login/oauth/access_token?code=", "/token", "/contact","/club/squad","/players")
"/login/oauth2/code/github","/login/oauth/access_token","/login/oauth/access_token?code=", "/token", "/contact","/club/squad","/players","/players/stats")
.permitAll()
.anyRequest()
.authenticated()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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.Stats;
import com.sg.backend.service.StatsService;

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

@Autowired
private StatsService statsSvc;

@GetMapping(path="/players/stats")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public List<Stats> getStats(HttpSession sess, @RequestParam String id){
statsSvc.getStats(id);
List<Stats> stats = statsSvc.getStats(id);
return stats;
}
}
114 changes: 114 additions & 0 deletions backend/src/main/java/com/sg/backend/models/Stats.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.sg.backend.models;

import java.io.StringReader;

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

public class Stats {
private int yellowCards;
private int redCards;
private int goals;
private int assists;
private int cleanSheets;
private int concededGoals;
private boolean isGoalkeeper;

public int getYellowCards() {
return yellowCards;
}

public void setYellowCards(int yellowCards) {
this.yellowCards = yellowCards;
}

public int getRedCards() {
return redCards;
}

public void setRedCards(int redCards) {
this.redCards = redCards;
}

public int getGoals() {
return goals;
}

public void setGoals(int goals) {
this.goals = goals;
}

public int getAssists() {
return assists;
}

public void setAssists(int assists) {
this.assists = assists;
}

public int getCleanSheets() {
return cleanSheets;
}

public void setCleanSheets(int cleanSheets) {
this.cleanSheets = cleanSheets;
}

public int getConcededGoals() {
return concededGoals;
}

public void setConcededGoals(int concededGoals) {
this.concededGoals = concededGoals;
}

public boolean isGoalkeeper() {
return isGoalkeeper;
}

public void setGoalkeeper(boolean isGoalkeeper) {
this.isGoalkeeper = isGoalkeeper;
}


public static Stats create(JsonObject performance) {
final Stats stat = new Stats();
stat.setYellowCards(performance.getInt("yellowCards"));
stat.setAssists(performance.getInt("assists"));
stat.setGoals(performance.getInt("goals"));
stat.setCleanSheets(performance.getInt("toNil"));
stat.setRedCards(performance.getInt("redCards"));
stat.setGoalkeeper(performance.getBoolean("isGoalkeeper"));
return stat;
}


public static Stats create(int yellowCards, int redCards, int goals, int assists, int cleanSheets,
int concededGoals, boolean isGoalkeeper) {
final Stats stat = new Stats();
stat.setYellowCards(yellowCards);
stat.setRedCards(redCards);
stat.setGoals(goals);
stat.setAssists(assists);
stat.setCleanSheets(cleanSheets);
stat.setConcededGoals(concededGoals);
stat.setGoalkeeper(isGoalkeeper);
return stat;
}

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

@Override
public String toString() {
return "Stats [yellowCards=" + yellowCards + ", redCards=" + redCards + ", goals=" + goals + ", assists="
+ assists + ", cleanSheets=" + cleanSheets + ", concededGoals=" + concededGoals + ", isGoalkeeper="
+ isGoalkeeper + "]";
}

}
127 changes: 127 additions & 0 deletions backend/src/main/java/com/sg/backend/service/StatsService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
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.Stats;

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

@Service
public class StatsService {

private static final String URL = "https://transfermarket.p.rapidapi.com/players/get-performance-summary";

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

public List<Stats> getStats(String id) {

String payload;
System.out.println("Attempting request to TransferMarkt.com....");

try {

String url = UriComponentsBuilder
.fromUriString(URL)
.queryParam("id", id)
.queryParam("domain", "com")
.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();
}
try (StringReader strReader = new StringReader(payload)) {
JsonReader r = Json.createReader(strReader);
JsonObject j = r.readObject();

List<Stats> stats = new LinkedList<>();

JsonString goalsScored = j.getJsonArray("competitionPerformanceSummery")
.getJsonObject(0) // premier league is the first competition
.getJsonObject("performance")
.getJsonString("goals");
int goals = Integer.parseInt(goalsScored.getString());

JsonString yellowCardsJson = j.getJsonArray("competitionPerformanceSummery")
.getJsonObject(0)
.getJsonObject("performance")
.getJsonString("yellowCards");
int yellowCards = Integer.parseInt(yellowCardsJson.getString());

JsonString redCardsJson = j.getJsonArray("competitionPerformanceSummery")
.getJsonObject(0)
.getJsonObject("performance")
.getJsonString("redCards");
int redCards = Integer.parseInt(redCardsJson.getString());

JsonString assistsJson = j.getJsonArray("competitionPerformanceSummery")
.getJsonObject(0)
.getJsonObject("performance")
.getJsonString("assists");
int assists = Integer.parseInt(assistsJson.getString());

JsonNumber cleanSheetsJson = j.getJsonArray("competitionPerformanceSummery")
.getJsonObject(0)
.getJsonObject("performance")
.getJsonNumber("toNil");
int cleanSheets = cleanSheetsJson.intValue();
// cast error resolved
JsonValue isGoalkeeperValue = j.getJsonArray("competitionPerformanceSummery")
.getJsonObject(0)
.getJsonObject("performance")
.get("isGoalkeeper");
boolean isGoalkeeper = Boolean.parseBoolean(isGoalkeeperValue.toString());

// put all the values into the object and then put the object into the stats
// list
Stats s = new Stats();
s.setGoals(goals);
s.setYellowCards(yellowCards);
s.setRedCards(redCards);
s.setAssists(assists);
s.setCleanSheets(cleanSheets);
s.setGoalkeeper(isGoalkeeper);
stats.add(s);
System.out.println(stats);
return stats;
}

}

}
30 changes: 30 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@material/theme": "^14.0.0",
"angular-oauth2": "^4.2.0",
"angular-oauth2-oidc": "^15.0.1",
"chart.js": "^4.2.1",
"googleapis": "^113.0.0",
"ngx-cookie-service": "^15.0.0",
"oauth2": "^0.0.1",
Expand Down
11 changes: 9 additions & 2 deletions frontend/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import { ClubsComponent } from './components/clubs.component';
import { ClubService } from './clubs.service';
import { StatsService } from './stats.service';
import { StatsComponent } from './components/stats.component';
import { MatSidenavModule } from '@angular/material/sidenav';
import { MatTableModule } from '@angular/material/table';
import { MatDividerModule } from '@angular/material/divider';
import { MatListModule } from '@angular/material/list';

const appRoutes: Routes = [
{ path: '', component: MainComponent },
Expand Down Expand Up @@ -69,8 +73,11 @@ const appRoutes: Routes = [
MatExpansionModule,
MatMenuModule,
MatInputModule,
MatFormFieldModule

MatFormFieldModule,
MatSidenavModule,
MatTableModule,
MatDividerModule,
MatListModule,
],
providers: [UserService,ContactService,ClubService,StatsService],
bootstrap: [AppComponent]
Expand Down
2 changes: 0 additions & 2 deletions frontend/src/app/components/account.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
<div style="flex: 1;">
<button mat-button [matMenuTriggerFor]="dashboardMenu" matMenuTriggerHover class="button-anim">Dashboard <mat-icon matSuffix>arrow_drop_down</mat-icon></button>
<mat-menu #dashboardMenu="matMenu">
<button mat-menu-item routerLink="/analytics">Analytics</button>
<button mat-menu-item routerLink="/matches">Matches</button>
<button mat-menu-item routerLink="/players">Players</button>
<button mat-menu-item routerLink="/club">Clubs</button>
</mat-menu>
Expand Down
Loading

0 comments on commit 807a944

Please sign in to comment.