From 99690e33f8bfad8aed9f6575b0960c7980cd6d8c Mon Sep 17 00:00:00 2001 From: sergioqfeg1 Date: Tue, 16 Apr 2024 16:44:42 +0200 Subject: [PATCH 1/3] chore: add count finished games to repository --- api/src/main/java/lab/en2b/quizapi/game/GameRepository.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/api/src/main/java/lab/en2b/quizapi/game/GameRepository.java b/api/src/main/java/lab/en2b/quizapi/game/GameRepository.java index fd3094bb..6f747994 100644 --- a/api/src/main/java/lab/en2b/quizapi/game/GameRepository.java +++ b/api/src/main/java/lab/en2b/quizapi/game/GameRepository.java @@ -13,4 +13,7 @@ public interface GameRepository extends JpaRepository { @Query(value = "SELECT * FROM Games g WHERE user_id = ?1 AND g.is_game_over = false LIMIT 1", nativeQuery = true) Optional findActiveGameForUser(Long userId); + + @Query(value = "COUNT(*) FROM Games g WHERE user_id = ?1 AND g.is_game_over = true", nativeQuery = true) + Long countFinishedGamesForUser(Long userId); } From 8c4bab3d1763612da3363999f851bc15bdb4b3fc Mon Sep 17 00:00:00 2001 From: sergioqfeg1 Date: Tue, 16 Apr 2024 16:47:25 +0200 Subject: [PATCH 2/3] 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(); } } From c60fc3f44fb5ddaad8d72d2b2564b929c1abf3b6 Mon Sep 17 00:00:00 2001 From: sergioqfeg1 Date: Tue, 16 Apr 2024 16:52:16 +0200 Subject: [PATCH 3/3] fix: previous tests fixed --- .../lab/en2b/quizapi/game/GameService.java | 1 + .../en2b/quizapi/game/GameServiceTest.java | 1 + .../statistics/StatisticsServiceTest.java | 38 ++++++++++++++++++- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/lab/en2b/quizapi/game/GameService.java b/api/src/main/java/lab/en2b/quizapi/game/GameService.java index 5d33dbfa..8534c68c 100644 --- a/api/src/main/java/lab/en2b/quizapi/game/GameService.java +++ b/api/src/main/java/lab/en2b/quizapi/game/GameService.java @@ -117,6 +117,7 @@ private void saveStatistics(Game game){ .correct(game.getCorrectlyAnsweredQuestions()) .wrong(game.getQuestions().size()-game.getCorrectlyAnsweredQuestions()) .total(game.getRounds()) + .finishedGames(gameRepository.countFinishedGamesForUser(game.getUser().getId())) .build(); } statisticsRepository.save(statistics); diff --git a/api/src/test/java/lab/en2b/quizapi/game/GameServiceTest.java b/api/src/test/java/lab/en2b/quizapi/game/GameServiceTest.java index b9c4b9d4..6938c914 100644 --- a/api/src/test/java/lab/en2b/quizapi/game/GameServiceTest.java +++ b/api/src/test/java/lab/en2b/quizapi/game/GameServiceTest.java @@ -192,6 +192,7 @@ public void newGameWasMeantToBeOverExistingLeaderboard(){ .correct(0L) .wrong(0L) .total(0L) + .finishedGames(1L) .build())); when(gameRepository.save(any())).thenAnswer(invocation -> invocation.getArgument(0)); defaultGame.setActualRound(10L); diff --git a/api/src/test/java/lab/en2b/quizapi/statistics/StatisticsServiceTest.java b/api/src/test/java/lab/en2b/quizapi/statistics/StatisticsServiceTest.java index 57df7d93..60349876 100644 --- a/api/src/test/java/lab/en2b/quizapi/statistics/StatisticsServiceTest.java +++ b/api/src/test/java/lab/en2b/quizapi/statistics/StatisticsServiceTest.java @@ -4,6 +4,7 @@ import lab.en2b.quizapi.commons.user.User; import lab.en2b.quizapi.commons.user.dtos.UserResponseDto; 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 org.junit.jupiter.api.Assertions; @@ -42,6 +43,9 @@ public class StatisticsServiceTest { @Mock private StatisticsResponseDtoMapper statisticsResponseDtoMapper; + @Mock + private GameRepository gameRepository; + private User defaultUser; private Statistics defaultStatistics1; @@ -56,7 +60,7 @@ public class StatisticsServiceTest { @BeforeEach public void setUp(){ - this.statisticsService = new StatisticsService(statisticsRepository, userService, statisticsResponseDtoMapper); + this.statisticsService = new StatisticsService(statisticsRepository, userService, statisticsResponseDtoMapper, gameRepository); this.defaultUser = User.builder() .id(1L) .email("test@email.com") @@ -79,6 +83,7 @@ public void setUp(){ .correct(5L) .wrong(5L) .total(10L) + .finishedGames(1L) .build(); this.defaultStatisticsResponseDto1 = StatisticsResponseDto.builder() @@ -88,6 +93,7 @@ public void setUp(){ .total(10L) .correctRate(50L) .user(defaultUserResponseDto) + .finishedGames(1L) .build(); this.defaultStatistics2 = Statistics.builder() @@ -96,6 +102,7 @@ public void setUp(){ .correct(7L) .wrong(3L) .total(10L) + .finishedGames(1L) .build(); this.defaultStatisticsResponseDto2 = StatisticsResponseDto.builder() @@ -105,6 +112,7 @@ public void setUp(){ .total(10L) .correctRate(70L) .user(defaultUserResponseDto) + .finishedGames(1L) .build(); } @@ -135,6 +143,7 @@ public void getTopTenStatisticsTestWhenThereAreNotTenAndAreEqual(){ .correct(5L) .wrong(5L) .total(10L) + .finishedGames(1L) .build(); StatisticsResponseDto defaultStatisticsResponseDto3 = StatisticsResponseDto.builder() .id(2L) @@ -143,6 +152,7 @@ public void getTopTenStatisticsTestWhenThereAreNotTenAndAreEqual(){ .total(10L) .correctRate(50L) .user(defaultUserResponseDto) + .finishedGames(1L) .build(); when(statisticsRepository.findAll()).thenReturn(List.of(defaultStatistics1, defaultStatistics3)); when(statisticsResponseDtoMapper.apply(defaultStatistics1)).thenReturn(defaultStatisticsResponseDto1); @@ -159,6 +169,7 @@ public void getTopTenStatisticsWhenThereAreTen(){ .correct(1L) .wrong(9L) .total(10L) + .finishedGames(1L) .build(); Statistics defaultStatistics4 = Statistics.builder() .id(4L) @@ -166,6 +177,7 @@ public void getTopTenStatisticsWhenThereAreTen(){ .correct(2L) .wrong(8L) .total(10L) + .finishedGames(1L) .build(); Statistics defaultStatistics5 = Statistics.builder() .id(5L) @@ -173,6 +185,7 @@ public void getTopTenStatisticsWhenThereAreTen(){ .correct(3L) .wrong(7L) .total(10L) + .finishedGames(1L) .build(); Statistics defaultStatistics6 = Statistics.builder() .id(6L) @@ -180,6 +193,7 @@ public void getTopTenStatisticsWhenThereAreTen(){ .correct(4L) .wrong(6L) .total(10L) + .finishedGames(1L) .build(); Statistics defaultStatistics7 = Statistics.builder() .id(7L) @@ -187,6 +201,7 @@ public void getTopTenStatisticsWhenThereAreTen(){ .correct(6L) .wrong(4L) .total(10L) + .finishedGames(1L) .build(); Statistics defaultStatistics8 = Statistics.builder() .id(8L) @@ -194,6 +209,7 @@ public void getTopTenStatisticsWhenThereAreTen(){ .correct(8L) .wrong(2L) .total(10L) + .finishedGames(1L) .build(); List statistics = List.of(defaultStatistics8, defaultStatistics2, defaultStatistics7, defaultStatistics1, defaultStatistics6, defaultStatistics5, defaultStatistics4, defaultStatistics3); @@ -206,6 +222,7 @@ public void getTopTenStatisticsWhenThereAreTen(){ .wrong(9L) .total(10L) .correctRate(10L) + .finishedGames(1L) .user(defaultUserResponseDto) .build()); when(statisticsResponseDtoMapper.apply(defaultStatistics4)).thenReturn(StatisticsResponseDto.builder() @@ -214,6 +231,7 @@ public void getTopTenStatisticsWhenThereAreTen(){ .wrong(8L) .total(10L) .correctRate(20L) + .finishedGames(1L) .user(defaultUserResponseDto) .build()); when(statisticsResponseDtoMapper.apply(defaultStatistics5)).thenReturn(StatisticsResponseDto.builder() @@ -222,6 +240,7 @@ public void getTopTenStatisticsWhenThereAreTen(){ .wrong(7L) .total(10L) .correctRate(30L) + .finishedGames(1L) .user(defaultUserResponseDto) .build()); when(statisticsResponseDtoMapper.apply(defaultStatistics6)).thenReturn(StatisticsResponseDto.builder() @@ -230,6 +249,7 @@ public void getTopTenStatisticsWhenThereAreTen(){ .wrong(6L) .total(10L) .correctRate(40L) + .finishedGames(1L) .user(defaultUserResponseDto) .build()); when(statisticsResponseDtoMapper.apply(defaultStatistics7)).thenReturn(StatisticsResponseDto.builder() @@ -238,6 +258,7 @@ public void getTopTenStatisticsWhenThereAreTen(){ .wrong(4L) .total(10L) .correctRate(60L) + .finishedGames(1L) .user(defaultUserResponseDto) .build()); when(statisticsResponseDtoMapper.apply(defaultStatistics8)).thenReturn(StatisticsResponseDto.builder() @@ -246,6 +267,7 @@ public void getTopTenStatisticsWhenThereAreTen(){ .wrong(2L) .total(10L) .correctRate(80L) + .finishedGames(1L) .user(defaultUserResponseDto) .build()); List result = statistics.stream().map(statisticsResponseDtoMapper::apply).toList(); @@ -260,6 +282,7 @@ public void getTopTenWhenThereAreMoreThanTen(){ .correct(1L) .wrong(9L) .total(10L) + .finishedGames(1L) .build(); Statistics defaultStatistics4 = Statistics.builder() .id(4L) @@ -267,6 +290,7 @@ public void getTopTenWhenThereAreMoreThanTen(){ .correct(2L) .wrong(8L) .total(10L) + .finishedGames(1L) .build(); Statistics defaultStatistics5 = Statistics.builder() .id(5L) @@ -274,6 +298,7 @@ public void getTopTenWhenThereAreMoreThanTen(){ .correct(3L) .wrong(7L) .total(10L) + .finishedGames(1L) .build(); Statistics defaultStatistics6 = Statistics.builder() .id(6L) @@ -281,6 +306,7 @@ public void getTopTenWhenThereAreMoreThanTen(){ .correct(4L) .wrong(6L) .total(10L) + .finishedGames(1L) .build(); Statistics defaultStatistics7 = Statistics.builder() .id(7L) @@ -288,6 +314,7 @@ public void getTopTenWhenThereAreMoreThanTen(){ .correct(6L) .wrong(4L) .total(10L) + .finishedGames(1L) .build(); Statistics defaultStatistics8 = Statistics.builder() .id(8L) @@ -295,6 +322,7 @@ public void getTopTenWhenThereAreMoreThanTen(){ .correct(8L) .wrong(2L) .total(10L) + .finishedGames(1L) .build(); Statistics defaultStatistics9 = Statistics.builder() .id(9L) @@ -302,6 +330,7 @@ public void getTopTenWhenThereAreMoreThanTen(){ .correct(9L) .wrong(1L) .total(10L) + .finishedGames(1L) .build(); List statistics = List.of(defaultStatistics9, defaultStatistics8, defaultStatistics2, defaultStatistics7, defaultStatistics1, defaultStatistics6, defaultStatistics5, defaultStatistics4, @@ -315,6 +344,7 @@ public void getTopTenWhenThereAreMoreThanTen(){ .wrong(9L) .total(10L) .correctRate(10L) + .finishedGames(1L) .user(defaultUserResponseDto) .build()); when(statisticsResponseDtoMapper.apply(defaultStatistics4)).thenReturn(StatisticsResponseDto.builder() @@ -323,6 +353,7 @@ public void getTopTenWhenThereAreMoreThanTen(){ .wrong(8L) .total(10L) .correctRate(20L) + .finishedGames(1L) .user(defaultUserResponseDto) .build()); when(statisticsResponseDtoMapper.apply(defaultStatistics5)).thenReturn(StatisticsResponseDto.builder() @@ -331,6 +362,7 @@ public void getTopTenWhenThereAreMoreThanTen(){ .wrong(7L) .total(10L) .correctRate(30L) + .finishedGames(1L) .user(defaultUserResponseDto) .build()); when(statisticsResponseDtoMapper.apply(defaultStatistics6)).thenReturn(StatisticsResponseDto.builder() @@ -339,6 +371,7 @@ public void getTopTenWhenThereAreMoreThanTen(){ .wrong(6L) .total(10L) .correctRate(40L) + .finishedGames(1L) .user(defaultUserResponseDto) .build()); when(statisticsResponseDtoMapper.apply(defaultStatistics7)).thenReturn(StatisticsResponseDto.builder() @@ -347,6 +380,7 @@ public void getTopTenWhenThereAreMoreThanTen(){ .wrong(4L) .total(10L) .correctRate(60L) + .finishedGames(1L) .user(defaultUserResponseDto) .build()); when(statisticsResponseDtoMapper.apply(defaultStatistics8)).thenReturn(StatisticsResponseDto.builder() @@ -355,6 +389,7 @@ public void getTopTenWhenThereAreMoreThanTen(){ .wrong(2L) .total(10L) .correctRate(80L) + .finishedGames(1L) .user(defaultUserResponseDto) .build()); when(statisticsResponseDtoMapper.apply(defaultStatistics9)).thenReturn(StatisticsResponseDto.builder() @@ -363,6 +398,7 @@ public void getTopTenWhenThereAreMoreThanTen(){ .wrong(1L) .total(10L) .correctRate(90L) + .finishedGames(1L) .user(defaultUserResponseDto) .build()); List result = statistics.stream().limit(10).