From 0e9bd08fc05d6e3e5fd7bf80a607d80c3f0f4ebf Mon Sep 17 00:00:00 2001 From: Ivana Machacikova <131284389+ivamach@users.noreply.github.com> Date: Sat, 27 Apr 2024 09:02:02 +0200 Subject: [PATCH] SMA-67: add rating does not work if user has multiple events (#87) --- .../src/main/java/com/sportsmatch/dtos/RatingDTO.java | 2 ++ .../com/sportsmatch/repositories/EventPlayerRepository.java | 2 +- .../main/java/com/sportsmatch/services/RatingService.java | 6 +++--- .../java/com/sportsmatch/services/RatingServiceTest.java | 4 +++- .../sportsmatch-app/src/components/RateGameComponent.tsx | 2 +- .../sportsmatch-app/src/generated/api/models/RatingDTO.ts | 1 + 6 files changed, 11 insertions(+), 6 deletions(-) diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/dtos/RatingDTO.java b/backend/sportsmatch/src/main/java/com/sportsmatch/dtos/RatingDTO.java index f606506d..7bcd332b 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/dtos/RatingDTO.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/dtos/RatingDTO.java @@ -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; } diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/repositories/EventPlayerRepository.java b/backend/sportsmatch/src/main/java/com/sportsmatch/repositories/EventPlayerRepository.java index 91880e40..b0617dc1 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/repositories/EventPlayerRepository.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/repositories/EventPlayerRepository.java @@ -15,7 +15,7 @@ @Repository public interface EventPlayerRepository extends JpaRepository { - Optional findEventPlayerByPlayer(User player); + Optional findEventPlayerByPlayerAndEventId(User player, Long eventId); List findEventPlayersByEvent(Event event); diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/services/RatingService.java b/backend/sportsmatch/src/main/java/com/sportsmatch/services/RatingService.java index 5a1065ed..28bd95bc 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/services/RatingService.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/services/RatingService.java @@ -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); @@ -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 eventPlayerOptional = - eventPlayerRepository.findEventPlayerByPlayer(player); + eventPlayerRepository.findEventPlayerByPlayerAndEventId(player, eventId); return eventPlayerOptional.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND)); } diff --git a/backend/sportsmatch/src/test/java/com/sportsmatch/services/RatingServiceTest.java b/backend/sportsmatch/src/test/java/com/sportsmatch/services/RatingServiceTest.java index bca9a633..d09ead0d 100644 --- a/backend/sportsmatch/src/test/java/com/sportsmatch/services/RatingServiceTest.java +++ b/backend/sportsmatch/src/test/java/com/sportsmatch/services/RatingServiceTest.java @@ -42,6 +42,7 @@ void addRating() { .eventStarRating(2) .myScore(10) .opponentScore(9) + .eventId(1L) .build(); // Authentication and Player @@ -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); diff --git a/frontend/sportsmatch-app/src/components/RateGameComponent.tsx b/frontend/sportsmatch-app/src/components/RateGameComponent.tsx index d39dff63..6943cdd4 100644 --- a/frontend/sportsmatch-app/src/components/RateGameComponent.tsx +++ b/frontend/sportsmatch-app/src/components/RateGameComponent.tsx @@ -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) diff --git a/frontend/sportsmatch-app/src/generated/api/models/RatingDTO.ts b/frontend/sportsmatch-app/src/generated/api/models/RatingDTO.ts index a8034810..d929dc4f 100644 --- a/frontend/sportsmatch-app/src/generated/api/models/RatingDTO.ts +++ b/frontend/sportsmatch-app/src/generated/api/models/RatingDTO.ts @@ -8,5 +8,6 @@ export type RatingDTO = { eventStarRating: number; myScore: number; opponentScore: number; + eventId?: number; };