Skip to content

Commit

Permalink
Merge pull request #189 from Arquisoft/feat/statistics
Browse files Browse the repository at this point in the history
Feat/statistics
  • Loading branch information
UO283615 authored Apr 7, 2024
2 parents 3b432a2 + 1ccd5e8 commit 929c960
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 18 deletions.
31 changes: 17 additions & 14 deletions api/src/main/java/lab/en2b/quizapi/game/GameService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions api/src/main/java/lab/en2b/quizapi/statistics/Statistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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> 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<StatisticsResponseDto> 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> 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<StatisticsResponseDto> result = statistics.stream().limit(10).
map(statisticsResponseDtoMapper::apply).toList();
Assertions.assertEquals(statisticsService.getTopTenStatistics(), result);
}

}

0 comments on commit 929c960

Please sign in to comment.