From 6433d4d98efd0d41898bc35710f135e13c59f560 Mon Sep 17 00:00:00 2001 From: bbbang105 <2018111366@dgu.ac.kr> Date: Mon, 4 Nov 2024 00:17:17 +0900 Subject: [PATCH] =?UTF-8?q?#110=20[fix]=20:=20=EB=A9=A4=EB=B2=84=EA=B0=80?= =?UTF-8?q?=20=EB=A7=8C=EB=93=A0=20=EC=9D=B4=EB=B2=A4=ED=8A=B8=EC=97=90=20?= =?UTF-8?q?=EC=B2=98=EC=9D=8C=20=EC=A0=91=EC=86=8D=ED=95=9C=20=EA=B2=BD?= =?UTF-8?q?=EC=9A=B0=20=EC=A1=B0=ED=9A=8C=EA=B0=80=20=EB=90=98=EC=A7=80=20?= =?UTF-8?q?=EC=95=8A=EB=8A=94=20=EB=AC=B8=EC=A0=9C=EB=A5=BC=20=ED=95=B4?= =?UTF-8?q?=EA=B2=B0=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EventParticipationRepository.java | 7 +--- .../side/onetime/service/EventService.java | 13 ++++--- .../side/onetime/service/ScheduleService.java | 36 ++++++++++--------- 3 files changed, 29 insertions(+), 27 deletions(-) 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<>();