From de309ea05c760ca3a9dbc3b68f0715c8f9336a0d Mon Sep 17 00:00:00 2001 From: Liam Date: Sun, 4 Aug 2024 00:36:59 +0100 Subject: [PATCH] api: add `/improved/best/{platform}` endpoint --- .../fascinated/controller/ScoresController.java | 17 +++++++++++++++++ .../fascinated/repository/ScoreRepository.java | 16 +++++++++++++++- .../cc/fascinated/services/ScoreService.java | 13 ++++++++++++- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/API/src/main/java/cc/fascinated/controller/ScoresController.java b/API/src/main/java/cc/fascinated/controller/ScoresController.java index 3a5eb95..c7785cc 100644 --- a/API/src/main/java/cc/fascinated/controller/ScoresController.java +++ b/API/src/main/java/cc/fascinated/controller/ScoresController.java @@ -98,4 +98,21 @@ public class ScoresController { 30 )); } + + /** + * A GET mapping to retrieve the score + * history for a leaderboard + * + * @param platform the platform to get the history from + * @return the score history + * @throws BadRequestException if there were no history found + */ + @ResponseBody + @GetMapping(value = "/improved/best/{platform}") + public ResponseEntity getBestImprovedScores(@PathVariable String platform) { + return ResponseEntity.ok(scoreService.getBestImprovedScores( + Platform.Platforms.getPlatform(platform), + 30 + )); + } } diff --git a/API/src/main/java/cc/fascinated/repository/ScoreRepository.java b/API/src/main/java/cc/fascinated/repository/ScoreRepository.java index 79150c9..928181d 100644 --- a/API/src/main/java/cc/fascinated/repository/ScoreRepository.java +++ b/API/src/main/java/cc/fascinated/repository/ScoreRepository.java @@ -53,7 +53,21 @@ public interface ScoreRepository extends MongoRepository { "{ $match: { 'previousScores.0': { $exists: true } } }", "{ $sort: { pp: -1 } }" }) - List getImprovedScores(@NonNull Platform.Platforms platform, @NonNull String playerId, @NonNull Date after); + List getUserImprovedScores(@NonNull Platform.Platforms platform, @NonNull String playerId, @NonNull Date after); + + /** + * Gets the best improved scores from the platform. + * + * @param platform the platform to get the scores from + * @param after the date to get the scores after + * @return the scores + */ + @Aggregation(pipeline = { + "{ $match: { platform: ?0, timestamp: { $gt: ?1 } } }", + "{ $match: { 'previousScores.0': { $exists: true } } }", + "{ $sort: { pp: -1 } }" + }) + List getBestImprovedScores(@NonNull Platform.Platforms platform, @NonNull Date after); /** * Gets a score from a platform and leaderboard id. diff --git a/API/src/main/java/cc/fascinated/services/ScoreService.java b/API/src/main/java/cc/fascinated/services/ScoreService.java index de46746..9c25218 100644 --- a/API/src/main/java/cc/fascinated/services/ScoreService.java +++ b/API/src/main/java/cc/fascinated/services/ScoreService.java @@ -225,7 +225,18 @@ public class ScoreService { * @return The improved scores. */ public List getImprovedScores(@NonNull Platform.Platforms platform, @NonNull User user, int days) { - return scoreRepository.getImprovedScores(platform, user.getSteamId(), DateUtils.getDaysAgo(days)); + return scoreRepository.getUserImprovedScores(platform, user.getSteamId(), DateUtils.getDaysAgo(days)); + } + + /** + * Gets the best improved scores from the platform. + * + * @param platform The platform to get the scores from. + * @param days The amount of days in the past to get the scores from. + * @return The scores. + */ + public List getBestImprovedScores(@NonNull Platform.Platforms platform, int days) { + return scoreRepository.getBestImprovedScores(platform, DateUtils.getDaysAgo(days)); } /**