From ab7dafeec9064363086f36d231c3557f120626a3 Mon Sep 17 00:00:00 2001 From: sergioqfeg1 Date: Sun, 7 Apr 2024 16:32:01 +0200 Subject: [PATCH 1/3] fix: updateStatistics new method --- .../main/java/lab/en2b/quizapi/statistics/Statistics.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 ee727760..015ea2b7 100644 --- a/api/src/main/java/lab/en2b/quizapi/statistics/Statistics.java +++ b/api/src/main/java/lab/en2b/quizapi/statistics/Statistics.java @@ -35,10 +35,10 @@ public Long getCorrectRate() { return (correct * 100) / total; } - public void updateStatistics(Statistics statistics){ - this.correct += statistics.getCorrect(); - this.wrong += statistics.getWrong(); - this.total += statistics.getTotal(); + public void updateStatistics(Long correct, Long wrong, Long total){ + this.correct += correct; + this.wrong += wrong; + this.total += total; } } From 370f95b0c024103ed70773260d78577283c159ac Mon Sep 17 00:00:00 2001 From: sergioqfeg1 Date: Sun, 7 Apr 2024 16:33:46 +0200 Subject: [PATCH 2/3] chore: added statistics at the end of the game --- .../lab/en2b/quizapi/game/GameService.java | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) 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 e01afc66..39704e44 100644 --- a/api/src/main/java/lab/en2b/quizapi/game/GameService.java +++ b/api/src/main/java/lab/en2b/quizapi/game/GameService.java @@ -64,23 +64,26 @@ public GameResponseDto answerQuestion(Long id, GameAnswerDto dto, Authentication System.out.println("Total round: " + game.getRounds()); if (game.isLastRound()){ - - /** - Statistics statistics = Statistics.builder() - .user(game.getUser()) - .correct(Long.valueOf(game.getCorrectlyAnsweredQuestions())) - .wrong(Long.valueOf(game.getRounds() - game.getCorrectlyAnsweredQuestions())) - .total(Long.valueOf(game.getRounds())) - .build(); - Statistics oldStatistics = statisticsRepository.findByUserId(game.getUser().getId()).orElseThrow(); - statisticsRepository.delete(oldStatistics); - oldStatistics.updateStatistics(statistics); - statisticsRepository.save(oldStatistics); - **/ - game.setGameOver(true); gameRepository.save(game); } + if (game.isGameOver()){ + if (statisticsRepository.findByUserId(game.getUser().getId()).isPresent()){ + Statistics statistics = statisticsRepository.findByUserId(game.getUser().getId()).get(); + statistics.updateStatistics(Long.valueOf(game.getCorrectlyAnsweredQuestions()), + Long.valueOf(game.getQuestions().size()-game.getCorrectlyAnsweredQuestions()), + Long.valueOf(game.getRounds())); + statisticsRepository.save(statistics); + } else { + Statistics statistics = Statistics.builder() + .user(game.getUser()) + .correct(Long.valueOf(game.getCorrectlyAnsweredQuestions())) + .wrong(Long.valueOf(game.getQuestions().size()-game.getCorrectlyAnsweredQuestions())) + .total(Long.valueOf(game.getRounds())) + .build(); + statisticsRepository.save(statistics); + } + } return gameResponseDtoMapper.apply(game); } From 1ccd5e830b057b1d3aa100312a3659f7687de49c Mon Sep 17 00:00:00 2001 From: sergioqfeg1 Date: Sun, 7 Apr 2024 16:57:18 +0200 Subject: [PATCH 3/3] test: testing getTopTenStatistics --- .../statistics/StatisticsServiceTest.java | 219 ++++++++++++++++++ 1 file changed, 219 insertions(+) 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 4d60c11b..005401b4 100644 --- a/api/src/test/java/lab/en2b/quizapi/statistics/StatisticsServiceTest.java +++ b/api/src/test/java/lab/en2b/quizapi/statistics/StatisticsServiceTest.java @@ -151,4 +151,223 @@ public void getTopTenStatisticsTestWhenThereAreNotTenAndAreEqual(){ Assertions.assertEquals(List.of(defaultStatisticsResponseDto1,defaultStatisticsResponseDto3), result); } + @Test + public void getTopTenStatisticsWhenThereAreTen(){ + Statistics defaultStatistics3 = Statistics.builder() + .id(3L) + .user(defaultUser) + .correct(1L) + .wrong(9L) + .total(10L) + .build(); + Statistics defaultStatistics4 = Statistics.builder() + .id(4L) + .user(defaultUser) + .correct(2L) + .wrong(8L) + .total(10L) + .build(); + Statistics defaultStatistics5 = Statistics.builder() + .id(5L) + .user(defaultUser) + .correct(3L) + .wrong(7L) + .total(10L) + .build(); + Statistics defaultStatistics6 = Statistics.builder() + .id(6L) + .user(defaultUser) + .correct(4L) + .wrong(6L) + .total(10L) + .build(); + Statistics defaultStatistics7 = Statistics.builder() + .id(7L) + .user(defaultUser) + .correct(6L) + .wrong(4L) + .total(10L) + .build(); + Statistics defaultStatistics8 = Statistics.builder() + .id(8L) + .user(defaultUser) + .correct(8L) + .wrong(2L) + .total(10L) + .build(); + List statistics = List.of(defaultStatistics8, defaultStatistics2, defaultStatistics7, + defaultStatistics1, defaultStatistics6, defaultStatistics5, defaultStatistics4, defaultStatistics3); + when(statisticsRepository.findAll()).thenReturn(statistics); + when(statisticsResponseDtoMapper.apply(defaultStatistics1)).thenReturn(defaultStatisticsResponseDto1); + when(statisticsResponseDtoMapper.apply(defaultStatistics2)).thenReturn(defaultStatisticsResponseDto2); + when(statisticsResponseDtoMapper.apply(defaultStatistics3)).thenReturn(StatisticsResponseDto.builder() + .id(3L) + .right(1L) + .wrong(9L) + .total(10L) + .correctRate(10L) + .user(defaultUserResponseDto) + .build()); + when(statisticsResponseDtoMapper.apply(defaultStatistics4)).thenReturn(StatisticsResponseDto.builder() + .id(4L) + .right(2L) + .wrong(8L) + .total(10L) + .correctRate(20L) + .user(defaultUserResponseDto) + .build()); + when(statisticsResponseDtoMapper.apply(defaultStatistics5)).thenReturn(StatisticsResponseDto.builder() + .id(5L) + .right(3L) + .wrong(7L) + .total(10L) + .correctRate(30L) + .user(defaultUserResponseDto) + .build()); + when(statisticsResponseDtoMapper.apply(defaultStatistics6)).thenReturn(StatisticsResponseDto.builder() + .id(6L) + .right(4L) + .wrong(6L) + .total(10L) + .correctRate(40L) + .user(defaultUserResponseDto) + .build()); + when(statisticsResponseDtoMapper.apply(defaultStatistics7)).thenReturn(StatisticsResponseDto.builder() + .id(7L) + .right(6L) + .wrong(4L) + .total(10L) + .correctRate(60L) + .user(defaultUserResponseDto) + .build()); + when(statisticsResponseDtoMapper.apply(defaultStatistics8)).thenReturn(StatisticsResponseDto.builder() + .id(8L) + .right(8L) + .wrong(2L) + .total(10L) + .correctRate(80L) + .user(defaultUserResponseDto) + .build()); + List result = statistics.stream().map(statisticsResponseDtoMapper::apply).toList(); + Assertions.assertEquals(statisticsService.getTopTenStatistics(), result); + } + + @Test + public void getTopTenWhenThereAreMoreThanTen(){ + Statistics defaultStatistics3 = Statistics.builder() + .id(3L) + .user(defaultUser) + .correct(1L) + .wrong(9L) + .total(10L) + .build(); + Statistics defaultStatistics4 = Statistics.builder() + .id(4L) + .user(defaultUser) + .correct(2L) + .wrong(8L) + .total(10L) + .build(); + Statistics defaultStatistics5 = Statistics.builder() + .id(5L) + .user(defaultUser) + .correct(3L) + .wrong(7L) + .total(10L) + .build(); + Statistics defaultStatistics6 = Statistics.builder() + .id(6L) + .user(defaultUser) + .correct(4L) + .wrong(6L) + .total(10L) + .build(); + Statistics defaultStatistics7 = Statistics.builder() + .id(7L) + .user(defaultUser) + .correct(6L) + .wrong(4L) + .total(10L) + .build(); + Statistics defaultStatistics8 = Statistics.builder() + .id(8L) + .user(defaultUser) + .correct(8L) + .wrong(2L) + .total(10L) + .build(); + Statistics defaultStatistics9 = Statistics.builder() + .id(9L) + .user(defaultUser) + .correct(9L) + .wrong(1L) + .total(10L) + .build(); + List statistics = List.of(defaultStatistics9, defaultStatistics8, defaultStatistics2, + defaultStatistics7, defaultStatistics1, defaultStatistics6, defaultStatistics5, defaultStatistics4, + defaultStatistics3); + when(statisticsRepository.findAll()).thenReturn(statistics); + when(statisticsResponseDtoMapper.apply(defaultStatistics1)).thenReturn(defaultStatisticsResponseDto1); + when(statisticsResponseDtoMapper.apply(defaultStatistics2)).thenReturn(defaultStatisticsResponseDto2); + when(statisticsResponseDtoMapper.apply(defaultStatistics3)).thenReturn(StatisticsResponseDto.builder() + .id(3L) + .right(1L) + .wrong(9L) + .total(10L) + .correctRate(10L) + .user(defaultUserResponseDto) + .build()); + when(statisticsResponseDtoMapper.apply(defaultStatistics4)).thenReturn(StatisticsResponseDto.builder() + .id(4L) + .right(2L) + .wrong(8L) + .total(10L) + .correctRate(20L) + .user(defaultUserResponseDto) + .build()); + when(statisticsResponseDtoMapper.apply(defaultStatistics5)).thenReturn(StatisticsResponseDto.builder() + .id(5L) + .right(3L) + .wrong(7L) + .total(10L) + .correctRate(30L) + .user(defaultUserResponseDto) + .build()); + when(statisticsResponseDtoMapper.apply(defaultStatistics6)).thenReturn(StatisticsResponseDto.builder() + .id(6L) + .right(4L) + .wrong(6L) + .total(10L) + .correctRate(40L) + .user(defaultUserResponseDto) + .build()); + when(statisticsResponseDtoMapper.apply(defaultStatistics7)).thenReturn(StatisticsResponseDto.builder() + .id(7L) + .right(6L) + .wrong(4L) + .total(10L) + .correctRate(60L) + .user(defaultUserResponseDto) + .build()); + when(statisticsResponseDtoMapper.apply(defaultStatistics8)).thenReturn(StatisticsResponseDto.builder() + .id(8L) + .right(8L) + .wrong(2L) + .total(10L) + .correctRate(80L) + .user(defaultUserResponseDto) + .build()); + when(statisticsResponseDtoMapper.apply(defaultStatistics9)).thenReturn(StatisticsResponseDto.builder() + .id(9L) + .right(9L) + .wrong(1L) + .total(10L) + .correctRate(90L) + .user(defaultUserResponseDto) + .build()); + List result = statistics.stream().limit(10). + map(statisticsResponseDtoMapper::apply).toList(); + Assertions.assertEquals(statisticsService.getTopTenStatistics(), result); + } + }