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 f606506..7bcd332 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 91880e4..b0617dc 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 5a1065e..28bd95b 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 bca9a63..d09ead0 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 d39dff6..6943cdd 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 a803481..d929dc4 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; };