From 8c4bab3d1763612da3363999f851bc15bdb4b3fc Mon Sep 17 00:00:00 2001 From: sergioqfeg1 Date: Tue, 16 Apr 2024 16:47:25 +0200 Subject: [PATCH] chore: added finished games --- api/src/main/java/lab/en2b/quizapi/statistics/Statistics.java | 3 +++ .../java/lab/en2b/quizapi/statistics/StatisticsService.java | 3 +++ .../en2b/quizapi/statistics/dtos/StatisticsResponseDto.java | 2 ++ .../statistics/mappers/StatisticsResponseDtoMapper.java | 1 + 4 files changed, 9 insertions(+) diff --git a/api/src/main/java/lab/en2b/quizapi/statistics/Statistics.java b/api/src/main/java/lab/en2b/quizapi/statistics/Statistics.java index 5edba8f7..b054ac7c 100644 --- a/api/src/main/java/lab/en2b/quizapi/statistics/Statistics.java +++ b/api/src/main/java/lab/en2b/quizapi/statistics/Statistics.java @@ -31,6 +31,9 @@ public class Statistics { @JoinColumn(name = "user_id") private User user; + @NonNull + private Long finishedGames; + public Long getCorrectRate() { if(total == 0){ return 0L; diff --git a/api/src/main/java/lab/en2b/quizapi/statistics/StatisticsService.java b/api/src/main/java/lab/en2b/quizapi/statistics/StatisticsService.java index e067afba..a4839924 100644 --- a/api/src/main/java/lab/en2b/quizapi/statistics/StatisticsService.java +++ b/api/src/main/java/lab/en2b/quizapi/statistics/StatisticsService.java @@ -2,6 +2,7 @@ import lab.en2b.quizapi.commons.user.User; import lab.en2b.quizapi.commons.user.UserService; +import lab.en2b.quizapi.game.GameRepository; import lab.en2b.quizapi.statistics.dtos.StatisticsResponseDto; import lab.en2b.quizapi.statistics.mappers.StatisticsResponseDtoMapper; import lombok.RequiredArgsConstructor; @@ -21,6 +22,7 @@ public class StatisticsService { private final StatisticsRepository statisticsRepository; private final UserService userService; private final StatisticsResponseDtoMapper statisticsResponseDtoMapper; + private final GameRepository gameRepository; /** * Updates the statistics for a user. If no statistics are found for the user, they are created. @@ -37,6 +39,7 @@ public StatisticsResponseDto getStatisticsForUser(Authentication authentication) .correct(0L) .wrong(0L) .total(0L) + .finishedGames(gameRepository.countFinishedGamesForUser(user.getId())) .build())); } diff --git a/api/src/main/java/lab/en2b/quizapi/statistics/dtos/StatisticsResponseDto.java b/api/src/main/java/lab/en2b/quizapi/statistics/dtos/StatisticsResponseDto.java index 203a99f4..f0503def 100644 --- a/api/src/main/java/lab/en2b/quizapi/statistics/dtos/StatisticsResponseDto.java +++ b/api/src/main/java/lab/en2b/quizapi/statistics/dtos/StatisticsResponseDto.java @@ -20,5 +20,7 @@ public class StatisticsResponseDto { private UserResponseDto user; @JsonProperty("correct_rate") private Long correctRate; + @JsonProperty("finished_games") + private Long finishedGames; } diff --git a/api/src/main/java/lab/en2b/quizapi/statistics/mappers/StatisticsResponseDtoMapper.java b/api/src/main/java/lab/en2b/quizapi/statistics/mappers/StatisticsResponseDtoMapper.java index c4b301f0..0d6652eb 100644 --- a/api/src/main/java/lab/en2b/quizapi/statistics/mappers/StatisticsResponseDtoMapper.java +++ b/api/src/main/java/lab/en2b/quizapi/statistics/mappers/StatisticsResponseDtoMapper.java @@ -23,6 +23,7 @@ public StatisticsResponseDto apply(Statistics statistics) { .total(statistics.getTotal()) .user(userResponseDtoMapper.apply(statistics.getUser())) .correctRate(statistics.getCorrectRate()) + .finishedGames(statistics.getFinishedGames()) .build(); } }