-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
/players/stats will return the statistics of the player based on ID
- Loading branch information
1 parent
742bd12
commit 807a944
Showing
16 changed files
with
473 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
backend/src/main/java/com/sg/backend/controllers/StatsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
127
backend/src/main/java/com/sg/backend/service/StatsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.