Skip to content

Commit

Permalink
chore: added finished games
Browse files Browse the repository at this point in the history
  • Loading branch information
sergioqfeg1 committed Apr 16, 2024
1 parent 99690e3 commit 8c4bab3
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 0 deletions.
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();
}
}

0 comments on commit 8c4bab3

Please sign in to comment.