From aac44692500a82516589451927846ffafb72c3e3 Mon Sep 17 00:00:00 2001 From: Teller501 Date: Mon, 4 Dec 2023 18:03:05 +0100 Subject: [PATCH] added method that checks if user is participating in event --- .../api/UserEventDetailsController.java | 6 ++++++ .../repository/UserEventDetailsRepository.java | 3 +++ .../service/UserEventDetailsService.java | 9 +++++++++ 3 files changed, 18 insertions(+) diff --git a/src/main/java/dk/kea/onav2ndproject_rest/api/UserEventDetailsController.java b/src/main/java/dk/kea/onav2ndproject_rest/api/UserEventDetailsController.java index c238275..7e19208 100644 --- a/src/main/java/dk/kea/onav2ndproject_rest/api/UserEventDetailsController.java +++ b/src/main/java/dk/kea/onav2ndproject_rest/api/UserEventDetailsController.java @@ -1,5 +1,6 @@ package dk.kea.onav2ndproject_rest.api; +import dk.kea.onav2ndproject_rest.dto.EventDTO; import dk.kea.onav2ndproject_rest.dto.UserDTO; import dk.kea.onav2ndproject_rest.entity.User; import dk.kea.onav2ndproject_rest.service.UserEventDetailsService; @@ -27,4 +28,9 @@ public ResponseEntity> getAdditionalNotesByUserIdAndEventId(@PathVa return new ResponseEntity<>(userEventDetailService.getAdditionalNotesByUserIdAndEventId(userId, eventId), HttpStatus.OK); } + @GetMapping("/participating/{userId}/{eventId}") + public ResponseEntity isUserParticipatingInEvent(@PathVariable int userId, @PathVariable int eventId) { + return new ResponseEntity<>(userEventDetailService.isUserParticipatingInEvent(userId, eventId), HttpStatus.OK); + } + } diff --git a/src/main/java/dk/kea/onav2ndproject_rest/repository/UserEventDetailsRepository.java b/src/main/java/dk/kea/onav2ndproject_rest/repository/UserEventDetailsRepository.java index 33e158c..06ac266 100644 --- a/src/main/java/dk/kea/onav2ndproject_rest/repository/UserEventDetailsRepository.java +++ b/src/main/java/dk/kea/onav2ndproject_rest/repository/UserEventDetailsRepository.java @@ -1,5 +1,6 @@ package dk.kea.onav2ndproject_rest.repository; +import dk.kea.onav2ndproject_rest.entity.Event; import dk.kea.onav2ndproject_rest.entity.User; import dk.kea.onav2ndproject_rest.entity.UserEventDetails; import org.springframework.data.jpa.repository.JpaRepository; @@ -17,5 +18,7 @@ public interface UserEventDetailsRepository extends JpaRepository findAdditionalNotesByUserIdAndEventId(@Param("userId") int userId, @Param("eventId") int eventId); Optional findByEventIdAndUserId(Integer eventId, Long userId); + @Query("SELECT CASE WHEN COUNT(ued) > 0 THEN true ELSE false END FROM UserEventDetails ued WHERE ued.user.id = :userId AND ued.event.id = :eventId AND ued.participating = true") + boolean isUserParticipatingInEvent(int userId, int eventId); } diff --git a/src/main/java/dk/kea/onav2ndproject_rest/service/UserEventDetailsService.java b/src/main/java/dk/kea/onav2ndproject_rest/service/UserEventDetailsService.java index e1dd498..7934a0a 100644 --- a/src/main/java/dk/kea/onav2ndproject_rest/service/UserEventDetailsService.java +++ b/src/main/java/dk/kea/onav2ndproject_rest/service/UserEventDetailsService.java @@ -1,7 +1,10 @@ package dk.kea.onav2ndproject_rest.service; +import dk.kea.onav2ndproject_rest.dto.EventConverter; +import dk.kea.onav2ndproject_rest.dto.EventDTO; import dk.kea.onav2ndproject_rest.dto.UserConverter; import dk.kea.onav2ndproject_rest.dto.UserDTO; +import dk.kea.onav2ndproject_rest.entity.Event; import dk.kea.onav2ndproject_rest.entity.User; import dk.kea.onav2ndproject_rest.repository.UserEventDetailsRepository; import org.springframework.beans.factory.annotation.Autowired; @@ -16,6 +19,8 @@ public class UserEventDetailsService { UserEventDetailsRepository userEventDetailsRepository; @Autowired UserConverter userConverter; + @Autowired + EventConverter eventConverter; public List getParticipatingUsersByEventId(int eventId) { List users = userEventDetailsRepository.findParticipatingUsersByEventId(eventId); @@ -25,4 +30,8 @@ public List getParticipatingUsersByEventId(int eventId) { public List getAdditionalNotesByUserIdAndEventId(int userId, int eventId) { return userEventDetailsRepository.findAdditionalNotesByUserIdAndEventId(userId, eventId); } + + public boolean isUserParticipatingInEvent(int userId, int eventId) { + return userEventDetailsRepository.isUserParticipatingInEvent(userId, eventId); + } }