Skip to content

Commit

Permalink
SMA-67: add rating does not work if user has multiple events (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivamach authored Apr 27, 2024
1 parent 8cf269e commit 0e9bd08
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ public class RatingDTO {
@NotNull(message = "Opponent score cannot be null")
@Min(value = 0, message = "Opponent score must be at least 0")
private Integer opponentScore;

private Long eventId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@Repository
public interface EventPlayerRepository extends JpaRepository<EventPlayer, Long> {

Optional<EventPlayer> findEventPlayerByPlayer(User player);
Optional<EventPlayer> findEventPlayerByPlayerAndEventId(User player, Long eventId);

List<EventPlayer> findEventPlayersByEvent(Event event);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class RatingService {

public void addRating(RatingDTO ratingDTO) {
User player = userService.getUserFromContext();
EventPlayer eventPlayer = getEventPlayer(player);
EventPlayer eventPlayer = getEventPlayer(player, ratingDTO.getEventId());
User opponent = findOpponent(eventPlayer, player);
Rating userRating = ratingMapper.toUserRatingEntity(ratingDTO);
Rating eventRating = ratingMapper.toEventRatingEntity(ratingDTO);
Expand All @@ -55,9 +55,9 @@ public void addRating(RatingDTO ratingDTO) {
userEventRatingRepository.save(userEventRating);
}

private EventPlayer getEventPlayer(User player) {
private EventPlayer getEventPlayer(User player, Long eventId) {
Optional<EventPlayer> eventPlayerOptional =
eventPlayerRepository.findEventPlayerByPlayer(player);
eventPlayerRepository.findEventPlayerByPlayerAndEventId(player, eventId);
return eventPlayerOptional.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ void addRating() {
.eventStarRating(2)
.myScore(10)
.opponentScore(9)
.eventId(1L)
.build();

// Authentication and Player
Expand All @@ -50,7 +51,8 @@ void addRating() {

// EventPlayer
EventPlayer eventPlayer = mock(EventPlayer.class);
when(eventPlayerRepository.findEventPlayerByPlayer(any())).thenReturn(Optional.of(eventPlayer));
when(eventPlayerRepository.findEventPlayerByPlayerAndEventId(any(), any()))
.thenReturn(Optional.of(eventPlayer));

// Opponent
Event event = mock(Event.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ export default function RateGameComponent(p: Props) {
eventStarRating: matchRating as number,
myScore: userScore,
opponentScore: opponentScore,
eventId: myEvent!.id!,
}
console.log(rating)

try {
await RatingControllerService.addRating(rating)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export type RatingDTO = {
eventStarRating: number;
myScore: number;
opponentScore: number;
eventId?: number;
};

0 comments on commit 0e9bd08

Please sign in to comment.