Skip to content

Commit

Permalink
Merge pull request #242 from Arquisoft/chore/gamesPlayed
Browse files Browse the repository at this point in the history
Chore/games played
  • Loading branch information
UO283615 authored Apr 17, 2024
2 parents 6508765 + c60fc3f commit 8e56bf9
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 1 deletion.
3 changes: 3 additions & 0 deletions api/src/main/java/lab/en2b/quizapi/game/GameRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ public interface GameRepository extends JpaRepository<Game,Long> {

@Query(value = "SELECT * FROM Games g WHERE user_id = ?1 AND g.is_game_over = false LIMIT 1", nativeQuery = true)
Optional<Game> 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);
}
1 change: 1 addition & 0 deletions api/src/main/java/lab/en2b/quizapi/game/GameService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions api/src/main/java/lab/en2b/quizapi/statistics/Statistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -37,6 +39,7 @@ public StatisticsResponseDto getStatisticsForUser(Authentication authentication)
.correct(0L)
.wrong(0L)
.total(0L)
.finishedGames(gameRepository.countFinishedGamesForUser(user.getId()))
.build()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ public class StatisticsResponseDto {
private UserResponseDto user;
@JsonProperty("correct_rate")
private Long correctRate;
@JsonProperty("finished_games")
private Long finishedGames;

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public StatisticsResponseDto apply(Statistics statistics) {
.total(statistics.getTotal())
.user(userResponseDtoMapper.apply(statistics.getUser()))
.correctRate(statistics.getCorrectRate())
.finishedGames(statistics.getFinishedGames())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,6 +43,9 @@ public class StatisticsServiceTest {
@Mock
private StatisticsResponseDtoMapper statisticsResponseDtoMapper;

@Mock
private GameRepository gameRepository;

private User defaultUser;

private Statistics defaultStatistics1;
Expand All @@ -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("[email protected]")
Expand All @@ -79,6 +83,7 @@ public void setUp(){
.correct(5L)
.wrong(5L)
.total(10L)
.finishedGames(1L)
.build();

this.defaultStatisticsResponseDto1 = StatisticsResponseDto.builder()
Expand All @@ -88,6 +93,7 @@ public void setUp(){
.total(10L)
.correctRate(50L)
.user(defaultUserResponseDto)
.finishedGames(1L)
.build();

this.defaultStatistics2 = Statistics.builder()
Expand All @@ -96,6 +102,7 @@ public void setUp(){
.correct(7L)
.wrong(3L)
.total(10L)
.finishedGames(1L)
.build();

this.defaultStatisticsResponseDto2 = StatisticsResponseDto.builder()
Expand All @@ -105,6 +112,7 @@ public void setUp(){
.total(10L)
.correctRate(70L)
.user(defaultUserResponseDto)
.finishedGames(1L)
.build();
}

Expand Down Expand Up @@ -135,6 +143,7 @@ public void getTopTenStatisticsTestWhenThereAreNotTenAndAreEqual(){
.correct(5L)
.wrong(5L)
.total(10L)
.finishedGames(1L)
.build();
StatisticsResponseDto defaultStatisticsResponseDto3 = StatisticsResponseDto.builder()
.id(2L)
Expand All @@ -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);
Expand All @@ -159,41 +169,47 @@ public void getTopTenStatisticsWhenThereAreTen(){
.correct(1L)
.wrong(9L)
.total(10L)
.finishedGames(1L)
.build();
Statistics defaultStatistics4 = Statistics.builder()
.id(4L)
.user(defaultUser)
.correct(2L)
.wrong(8L)
.total(10L)
.finishedGames(1L)
.build();
Statistics defaultStatistics5 = Statistics.builder()
.id(5L)
.user(defaultUser)
.correct(3L)
.wrong(7L)
.total(10L)
.finishedGames(1L)
.build();
Statistics defaultStatistics6 = Statistics.builder()
.id(6L)
.user(defaultUser)
.correct(4L)
.wrong(6L)
.total(10L)
.finishedGames(1L)
.build();
Statistics defaultStatistics7 = Statistics.builder()
.id(7L)
.user(defaultUser)
.correct(6L)
.wrong(4L)
.total(10L)
.finishedGames(1L)
.build();
Statistics defaultStatistics8 = Statistics.builder()
.id(8L)
.user(defaultUser)
.correct(8L)
.wrong(2L)
.total(10L)
.finishedGames(1L)
.build();
List<Statistics> statistics = List.of(defaultStatistics8, defaultStatistics2, defaultStatistics7,
defaultStatistics1, defaultStatistics6, defaultStatistics5, defaultStatistics4, defaultStatistics3);
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -246,6 +267,7 @@ public void getTopTenStatisticsWhenThereAreTen(){
.wrong(2L)
.total(10L)
.correctRate(80L)
.finishedGames(1L)
.user(defaultUserResponseDto)
.build());
List<StatisticsResponseDto> result = statistics.stream().map(statisticsResponseDtoMapper::apply).toList();
Expand All @@ -260,48 +282,55 @@ public void getTopTenWhenThereAreMoreThanTen(){
.correct(1L)
.wrong(9L)
.total(10L)
.finishedGames(1L)
.build();
Statistics defaultStatistics4 = Statistics.builder()
.id(4L)
.user(defaultUser)
.correct(2L)
.wrong(8L)
.total(10L)
.finishedGames(1L)
.build();
Statistics defaultStatistics5 = Statistics.builder()
.id(5L)
.user(defaultUser)
.correct(3L)
.wrong(7L)
.total(10L)
.finishedGames(1L)
.build();
Statistics defaultStatistics6 = Statistics.builder()
.id(6L)
.user(defaultUser)
.correct(4L)
.wrong(6L)
.total(10L)
.finishedGames(1L)
.build();
Statistics defaultStatistics7 = Statistics.builder()
.id(7L)
.user(defaultUser)
.correct(6L)
.wrong(4L)
.total(10L)
.finishedGames(1L)
.build();
Statistics defaultStatistics8 = Statistics.builder()
.id(8L)
.user(defaultUser)
.correct(8L)
.wrong(2L)
.total(10L)
.finishedGames(1L)
.build();
Statistics defaultStatistics9 = Statistics.builder()
.id(9L)
.user(defaultUser)
.correct(9L)
.wrong(1L)
.total(10L)
.finishedGames(1L)
.build();
List<Statistics> statistics = List.of(defaultStatistics9, defaultStatistics8, defaultStatistics2,
defaultStatistics7, defaultStatistics1, defaultStatistics6, defaultStatistics5, defaultStatistics4,
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -363,6 +398,7 @@ public void getTopTenWhenThereAreMoreThanTen(){
.wrong(1L)
.total(10L)
.correctRate(90L)
.finishedGames(1L)
.user(defaultUserResponseDto)
.build());
List<StatisticsResponseDto> result = statistics.stream().limit(10).
Expand Down

0 comments on commit 8e56bf9

Please sign in to comment.