diff --git a/src/main/java/side/onetime/repository/EventParticipationRepository.java b/src/main/java/side/onetime/repository/EventParticipationRepository.java index 94c27f7..6d15e20 100644 --- a/src/main/java/side/onetime/repository/EventParticipationRepository.java +++ b/src/main/java/side/onetime/repository/EventParticipationRepository.java @@ -1,19 +1,14 @@ package side.onetime.repository; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.query.Param; import side.onetime.domain.Event; import side.onetime.domain.EventParticipation; import side.onetime.domain.User; import java.util.List; -import java.util.Optional; public interface EventParticipationRepository extends JpaRepository { List findAllByEvent(Event event); List findAllByUser(User user); - @Query("SELECT COUNT(ep) FROM EventParticipation ep WHERE ep.event = :event") - int countByEvent(@Param("event") Event event); - Optional findByUserAndEvent(User user, Event event); + EventParticipation findByUserAndEvent(User user, Event event); } \ No newline at end of file diff --git a/src/main/java/side/onetime/service/EventService.java b/src/main/java/side/onetime/service/EventService.java index 7606562..c51fab1 100644 --- a/src/main/java/side/onetime/service/EventService.java +++ b/src/main/java/side/onetime/service/EventService.java @@ -129,9 +129,10 @@ public GetEventResponse getEvent(String eventId, String authorizationHeader) { EventStatus eventStatus = null; if (authorizationHeader != null) { User user = jwtUtil.getUserFromHeader(authorizationHeader); - EventParticipation eventParticipation = eventParticipationRepository.findByUserAndEvent(user, event) - .orElseThrow(() -> new CustomException(EventParticipationErrorStatus._NOT_FOUND_EVENT_PARTICIPATION)); - eventStatus = eventParticipation.getEventStatus(); + EventParticipation eventParticipation = eventParticipationRepository.findByUserAndEvent(user, event); + if (eventParticipation != null) { + eventStatus = eventParticipation.getEventStatus(); + } } return GetEventResponse.of(event, ranges, eventStatus); @@ -308,8 +309,10 @@ public void removeUserCreatedEvent(String authorizationHeader, String eventId) { Event event = eventRepository.findByEventId(UUID.fromString(eventId)) .orElseThrow(() -> new CustomException(EventErrorStatus._NOT_FOUND_EVENT)); - EventParticipation eventParticipation = eventParticipationRepository.findByUserAndEvent(user, event) - .orElseThrow(() -> new CustomException(EventParticipationErrorStatus._NOT_FOUND_EVENT_PARTICIPATION)); + EventParticipation eventParticipation = eventParticipationRepository.findByUserAndEvent(user, event); + if (eventParticipation == null) { + throw new CustomException(EventParticipationErrorStatus._NOT_FOUND_EVENT_PARTICIPATION); + } if (!EventStatus.CREATOR.equals(eventParticipation.getEventStatus())) { // 해당 이벤트의 생성자가 아닌 경우 throw new CustomException(EventParticipationErrorStatus._IS_NOT_USERS_CREATED_EVENT_PARTICIPATION); diff --git a/src/main/java/side/onetime/service/ScheduleService.java b/src/main/java/side/onetime/service/ScheduleService.java index cf6c89b..23fc331 100644 --- a/src/main/java/side/onetime/service/ScheduleService.java +++ b/src/main/java/side/onetime/service/ScheduleService.java @@ -71,14 +71,16 @@ public void createDaySchedulesForAuthenticatedUser(CreateDayScheduleRequest crea .orElseThrow(() -> new CustomException(EventErrorStatus._NOT_FOUND_EVENT)); User user = jwtUtil.getUserFromHeader(authorizationHeader); // 참여 정보가 없는 경우 참여자로 저장 - eventParticipationRepository.findByUserAndEvent(user, event) - .orElseGet(() -> eventParticipationRepository.save( - EventParticipation.builder() - .user(user) - .event(event) - .eventStatus(EventStatus.PARTICIPANT) - .build() - )); + EventParticipation eventParticipation = eventParticipationRepository.findByUserAndEvent(user, event); + if (eventParticipation == null) { + eventParticipationRepository.save( + EventParticipation.builder() + .user(user) + .event(event) + .eventStatus(EventStatus.PARTICIPANT) + .build() + ); + } List daySchedules = createDayScheduleRequest.daySchedules(); List newSelections = new ArrayList<>(); @@ -141,14 +143,16 @@ public void createDateSchedulesForAuthenticatedUser(CreateDateScheduleRequest cr .orElseThrow(() -> new CustomException(EventErrorStatus._NOT_FOUND_EVENT)); User user = jwtUtil.getUserFromHeader(authorizationHeader); // 참여 정보가 없는 경우 참여자로 저장 - eventParticipationRepository.findByUserAndEvent(user, event) - .orElseGet(() -> eventParticipationRepository.save( - EventParticipation.builder() - .user(user) - .event(event) - .eventStatus(EventStatus.PARTICIPANT) - .build() - )); + EventParticipation eventParticipation = eventParticipationRepository.findByUserAndEvent(user, event); + if (eventParticipation == null) { + eventParticipationRepository.save( + EventParticipation.builder() + .user(user) + .event(event) + .eventStatus(EventStatus.PARTICIPANT) + .build() + ); + } List dateSchedules = createDateScheduleRequest.dateSchedules(); List newSelections = new ArrayList<>();