-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from garamb1/football-data-org
Add football data support
- Loading branch information
Showing
24 changed files
with
2,889 additions
and
16 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
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 |
---|---|---|
@@ -1 +1 @@ | ||
0.5.2-testing | ||
0.6 |
22 changes: 22 additions & 0 deletions
22
src/main/java/it/garambo/retrosearch/controller/FootballController.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,22 @@ | ||
package it.garambo.retrosearch.controller; | ||
|
||
import it.garambo.retrosearch.sports.football.repository.FootballRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
|
||
@Controller | ||
@ConditionalOnBean(FootballRepository.class) | ||
public class FootballController { | ||
|
||
@Autowired private FootballRepository footballRepository; | ||
|
||
@GetMapping(path = {"/football", "/sports/football"}) | ||
public String football(Model model) { | ||
model.addAttribute("updatedAt", footballRepository.getUpdatedAt()); | ||
model.addAttribute("results", footballRepository.getAllMatches()); | ||
return "football"; | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
src/main/java/it/garambo/retrosearch/sports/football/client/FootballDataOrgClient.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,34 @@ | ||
package it.garambo.retrosearch.sports.football.client; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import it.garambo.retrosearch.http.HttpService; | ||
import it.garambo.retrosearch.sports.football.model.FootballDataResponse; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.List; | ||
import org.apache.http.message.BasicHeader; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@ConditionalOnProperty(value = "retrosearch.sports.football.enable", havingValue = "true") | ||
public class FootballDataOrgClient { | ||
|
||
private final String API_URL = "https://api.football-data.org/v4/matches/"; | ||
|
||
@Value("${retrosearch.sports.football.api.key:}") | ||
private String apiKey; | ||
|
||
@Autowired HttpService httpService; | ||
|
||
public FootballDataResponse fetchFootballData() throws IOException, URISyntaxException { | ||
URI apiUri = new URI(API_URL); | ||
BasicHeader apiKeyHeader = new BasicHeader("X-Auth-Token", apiKey); | ||
|
||
String response = httpService.get(apiUri, List.of(apiKeyHeader)); | ||
return new ObjectMapper().readValue(response, FootballDataResponse.class); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/it/garambo/retrosearch/sports/football/model/FootballDataResponse.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,8 @@ | ||
package it.garambo.retrosearch.sports.football.model; | ||
|
||
import it.garambo.retrosearch.sports.football.model.match.Match; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public record FootballDataResponse( | ||
Map<String, String> filters, Map<String, String> resultSet, List<Match> matches) {} |
6 changes: 6 additions & 0 deletions
6
src/main/java/it/garambo/retrosearch/sports/football/model/match/Area.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,6 @@ | ||
package it.garambo.retrosearch.sports.football.model.match; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public record Area(int id, String name, String code) {} |
6 changes: 6 additions & 0 deletions
6
src/main/java/it/garambo/retrosearch/sports/football/model/match/Competition.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,6 @@ | ||
package it.garambo.retrosearch.sports.football.model.match; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public record Competition(int id, String name, String code) {} |
3 changes: 3 additions & 0 deletions
3
src/main/java/it/garambo/retrosearch/sports/football/model/match/HomeAwayScore.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,3 @@ | ||
package it.garambo.retrosearch.sports.football.model.match; | ||
|
||
public record HomeAwayScore(int home, int away) {} |
32 changes: 32 additions & 0 deletions
32
src/main/java/it/garambo/retrosearch/sports/football/model/match/Match.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 it.garambo.retrosearch.sports.football.model.match; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import it.garambo.retrosearch.sports.football.model.match.enums.Status; | ||
import java.util.Date; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public record Match( | ||
int id, | ||
Date utcDate, | ||
Date lastUpdated, | ||
Area area, | ||
Status status, | ||
Competition competition, | ||
Team homeTeam, | ||
Team awayTeam, | ||
Score score) | ||
implements Comparable<Match> { | ||
|
||
@Override | ||
public int compareTo(@NotNull Match o) { | ||
return this.area.id() | ||
- o.area.id() | ||
+ this.competition.id() | ||
- o.competition.id() | ||
+ this.id | ||
- o.id | ||
+ this.status.ordinal() | ||
- this.status.ordinal(); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/it/garambo/retrosearch/sports/football/model/match/Score.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,6 @@ | ||
package it.garambo.retrosearch.sports.football.model.match; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public record Score(HomeAwayScore halfTime, HomeAwayScore fullTime) {} |
6 changes: 6 additions & 0 deletions
6
src/main/java/it/garambo/retrosearch/sports/football/model/match/Team.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,6 @@ | ||
package it.garambo.retrosearch.sports.football.model.match; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public record Team(int id, String name, String shortName) {} |
23 changes: 23 additions & 0 deletions
23
src/main/java/it/garambo/retrosearch/sports/football/model/match/enums/Status.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,23 @@ | ||
package it.garambo.retrosearch.sports.football.model.match.enums; | ||
|
||
public enum Status { | ||
SCHEDULED("Scheduled"), | ||
TIMED("Timed"), | ||
IN_PLAY("In Play"), | ||
PAUSED("Paused"), | ||
FINISHED("Finished"), | ||
SUSPENDED("Suspended"), | ||
POSTPONED("Postponed"), | ||
CANCELLED("Canceled"), | ||
AWARDED("Awarded"); | ||
|
||
final String description; | ||
|
||
private Status(String description) { | ||
this.description = description; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/it/garambo/retrosearch/sports/football/repository/FootballRepository.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,18 @@ | ||
package it.garambo.retrosearch.sports.football.repository; | ||
|
||
import it.garambo.retrosearch.sports.football.model.match.Match; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
public interface FootballRepository { | ||
|
||
Map<String, Set<Match>> getAllMatches(); | ||
|
||
Set<Match> getAllMatchesByArea(String areaName); | ||
|
||
void updateAll(List<Match> newMatches); | ||
|
||
Date getUpdatedAt(); | ||
} |
45 changes: 45 additions & 0 deletions
45
...in/java/it/garambo/retrosearch/sports/football/repository/InMemoryFootballRepository.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,45 @@ | ||
package it.garambo.retrosearch.sports.football.repository; | ||
|
||
import it.garambo.retrosearch.sports.football.model.match.Match; | ||
import java.util.*; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Component | ||
@ConditionalOnProperty(value = "retrosearch.sports.football.enable", havingValue = "true") | ||
public class InMemoryFootballRepository implements FootballRepository { | ||
|
||
private Map<String, Set<Match>> matchesByArea; | ||
private Date updatedAt; | ||
|
||
@Override | ||
public Map<String, Set<Match>> getAllMatches() { | ||
return matchesByArea; | ||
} | ||
|
||
@Override | ||
public Set<Match> getAllMatchesByArea(String areaName) { | ||
return matchesByArea.get(areaName); | ||
} | ||
|
||
@Override | ||
public void updateAll(List<Match> newMatches) { | ||
Map<String, Set<Match>> updatedMatches = new HashMap<>(); | ||
newMatches.forEach( | ||
match -> { | ||
String areaName = match.area().name(); | ||
updatedMatches.putIfAbsent(areaName, new HashSet<>()); | ||
updatedMatches.get(areaName).add(match); | ||
}); | ||
log.info("Football scores updated"); | ||
matchesByArea = updatedMatches; | ||
updatedAt = new Date(); | ||
} | ||
|
||
@Override | ||
public Date getUpdatedAt() { | ||
return updatedAt; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...main/java/it/garambo/retrosearch/sports/football/scheduled/FootballDataScheduledTask.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,31 @@ | ||
package it.garambo.retrosearch.sports.football.scheduled; | ||
|
||
import it.garambo.retrosearch.sports.football.client.FootballDataOrgClient; | ||
import it.garambo.retrosearch.sports.football.model.FootballDataResponse; | ||
import it.garambo.retrosearch.sports.football.repository.FootballRepository; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Component | ||
@ConditionalOnProperty(value = "retrosearch.sports.football.enable", havingValue = "true") | ||
public class FootballDataScheduledTask { | ||
|
||
@Autowired private FootballDataOrgClient apiClient; | ||
|
||
@Autowired private FootballRepository repository; | ||
|
||
@Scheduled(fixedRate = 30 * 60 * 1000) | ||
private void updateFootballData() { | ||
try { | ||
log.info("Updating football result list..."); | ||
FootballDataResponse footballData = apiClient.fetchFootballData(); | ||
repository.updateAll(footballData.matches()); | ||
} catch (Exception e) { | ||
log.error("Football result list update failed:", e); | ||
} | ||
} | ||
} |
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.