From 311415aacbd55a39eec18f3b7978a5be4776b0e3 Mon Sep 17 00:00:00 2001 From: bbbang105 <2018111366@dgu.ac.kr> Date: Tue, 1 Oct 2024 03:25:44 +0900 Subject: [PATCH] =?UTF-8?q?#52=20[feat]=20:=20=EB=A1=9C=EA=B7=B8=EC=9D=B8?= =?UTF-8?q?=20=EC=9C=A0=EC=A0=80=EA=B0=80=20=EC=8A=A4=EC=BC=80=EC=A4=84?= =?UTF-8?q?=EC=9D=84=20=EB=93=B1=EB=A1=9D=ED=95=A0=20=EC=88=98=20=EC=9E=88?= =?UTF-8?q?=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ScheduleController.java | 22 +++-- .../repository/SelectionRepository.java | 5 +- .../side/onetime/service/ScheduleService.java | 82 +++++++++++++++++-- 3 files changed, 91 insertions(+), 18 deletions(-) diff --git a/src/main/java/side/onetime/controller/ScheduleController.java b/src/main/java/side/onetime/controller/ScheduleController.java index 903f597..265dc3f 100644 --- a/src/main/java/side/onetime/controller/ScheduleController.java +++ b/src/main/java/side/onetime/controller/ScheduleController.java @@ -19,18 +19,28 @@ public class ScheduleController { // 요일 스케줄 등록 API @PostMapping("/day") public ResponseEntity> createDaySchedules( - @RequestBody ScheduleDto.CreateDayScheduleRequest createDayScheduleRequest) { - - scheduleService.createDaySchedules(createDayScheduleRequest); + @RequestBody ScheduleDto.CreateDayScheduleRequest createDayScheduleRequest, + @RequestHeader(value = "Authorization", required = false) String authorizationHeader) { + + if (authorizationHeader != null) { + scheduleService.createDaySchedulesForAuthenticatedUser(createDayScheduleRequest, authorizationHeader); + } else { + scheduleService.createDaySchedulesForAnonymousUser(createDayScheduleRequest); + } return ApiResponse.onSuccess(SuccessStatus._CREATED_DAY_SCHEDULES); } // 날짜 스케줄 등록 API @PostMapping("/date") public ResponseEntity> createDateSchedules( - @RequestBody ScheduleDto.CreateDateScheduleRequest createDateScheduleRequest) { - - scheduleService.createDateSchedules(createDateScheduleRequest); + @RequestBody ScheduleDto.CreateDateScheduleRequest createDateScheduleRequest, + @RequestHeader(value = "Authorization", required = false) String authorizationHeader) { + + if (authorizationHeader != null) { + scheduleService.createDateSchedulesForAuthenticatedUser(createDateScheduleRequest, authorizationHeader); + } else { + scheduleService.createDateSchedulesForAnonymousUser(createDateScheduleRequest); + } return ApiResponse.onSuccess(SuccessStatus._CREATED_DATE_SCHEDULES); } diff --git a/src/main/java/side/onetime/repository/SelectionRepository.java b/src/main/java/side/onetime/repository/SelectionRepository.java index 66d9a29..6f01c69 100644 --- a/src/main/java/side/onetime/repository/SelectionRepository.java +++ b/src/main/java/side/onetime/repository/SelectionRepository.java @@ -3,14 +3,13 @@ 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.Member; -import side.onetime.domain.Selection; +import side.onetime.domain.*; import java.util.List; public interface SelectionRepository extends JpaRepository { void deleteAllByMember(Member member); + void deleteAllByUserAndScheduleIn(User user, List schedules); @Query("SELECT s FROM Selection s JOIN FETCH s.schedule sc WHERE sc.event = :event") List findAllSelectionsByEvent(@Param("event") Event event); } \ No newline at end of file diff --git a/src/main/java/side/onetime/service/ScheduleService.java b/src/main/java/side/onetime/service/ScheduleService.java index d77349f..0998a74 100644 --- a/src/main/java/side/onetime/service/ScheduleService.java +++ b/src/main/java/side/onetime/service/ScheduleService.java @@ -3,18 +3,15 @@ import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; -import side.onetime.domain.Event; -import side.onetime.domain.Member; -import side.onetime.domain.Schedule; -import side.onetime.domain.Selection; +import side.onetime.domain.*; import side.onetime.dto.ScheduleDto; import side.onetime.exception.*; import side.onetime.repository.EventRepository; import side.onetime.repository.MemberRepository; import side.onetime.repository.ScheduleRepository; import side.onetime.repository.SelectionRepository; +import side.onetime.util.JwtUtil; -import java.time.LocalTime; import java.util.*; import java.util.stream.Collectors; @@ -25,10 +22,11 @@ public class ScheduleService { private final MemberRepository memberRepository; private final ScheduleRepository scheduleRepository; private final SelectionRepository selectionRepository; + private final JwtUtil jwtUtil; - // 요일 스케줄 등록 메서드 + // 요일 스케줄 등록 메서드 (비로그인) @Transactional - public void createDaySchedules(ScheduleDto.CreateDayScheduleRequest createDayScheduleRequest) { + public void createDaySchedulesForAnonymousUser(ScheduleDto.CreateDayScheduleRequest createDayScheduleRequest) { Event event = eventRepository.findByEventId(UUID.fromString(createDayScheduleRequest.getEventId())) .orElseThrow(() -> new EventException(EventErrorResult._NOT_FOUND_EVENT)); Member member = memberRepository.findByMemberId(UUID.fromString(createDayScheduleRequest.getMemberId())) @@ -56,9 +54,41 @@ public void createDaySchedules(ScheduleDto.CreateDayScheduleRequest createDaySch selectionRepository.saveAll(selections); } - // 날짜 스케줄 등록 메서드 + // 요일 스케줄 등록 메서드 (로그인) @Transactional - public void createDateSchedules(ScheduleDto.CreateDateScheduleRequest createDateScheduleRequest) { + public void createDaySchedulesForAuthenticatedUser(ScheduleDto.CreateDayScheduleRequest createDayScheduleRequest, String authorizationHeader) { + Event event = eventRepository.findByEventId(UUID.fromString(createDayScheduleRequest.getEventId())) + .orElseThrow(() -> new EventException(EventErrorResult._NOT_FOUND_EVENT)); + User user = jwtUtil.getUserFromHeader(authorizationHeader); + + List daySchedules = createDayScheduleRequest.getDaySchedules(); + List newSelections = new ArrayList<>(); + List allSchedules = new ArrayList<>(); + + for (ScheduleDto.DaySchedule daySchedule : daySchedules) { + String day = daySchedule.getDay(); + List times = daySchedule.getTimes(); + List schedules = scheduleRepository.findAllByEventAndDay(event, day) + .orElseThrow(() -> new ScheduleException(ScheduleErrorResult._NOT_FOUND_DAY_SCHEDULES)); + + for (Schedule schedule : schedules) { + if (times.contains(schedule.getTime())) { + newSelections.add(Selection.builder() + .user(user) + .schedule(schedule) + .build()); + } + } + allSchedules.addAll(schedules); + } + selectionRepository.deleteAllByUserAndScheduleIn(user, allSchedules); + selectionRepository.saveAll(newSelections); + } + + + // 날짜 스케줄 등록 메서드 (비로그인) + @Transactional + public void createDateSchedulesForAnonymousUser(ScheduleDto.CreateDateScheduleRequest createDateScheduleRequest) { Event event = eventRepository.findByEventId(UUID.fromString(createDateScheduleRequest.getEventId())) .orElseThrow(() -> new EventException(EventErrorResult._NOT_FOUND_EVENT)); Member member = memberRepository.findByMemberId(UUID.fromString(createDateScheduleRequest.getMemberId())) @@ -86,6 +116,40 @@ public void createDateSchedules(ScheduleDto.CreateDateScheduleRequest createDate selectionRepository.saveAll(selections); } + // 날짜 스케줄 등록 메서드 (로그인) + @Transactional + public void createDateSchedulesForAuthenticatedUser(ScheduleDto.CreateDateScheduleRequest createDateScheduleRequest, String authorizationHeader) { + Event event = eventRepository.findByEventId(UUID.fromString(createDateScheduleRequest.getEventId())) + .orElseThrow(() -> new EventException(EventErrorResult._NOT_FOUND_EVENT)); + User user = jwtUtil.getUserFromHeader(authorizationHeader); + + List dateSchedules = createDateScheduleRequest.getDateSchedules(); + List newSelections = new ArrayList<>(); + List allSchedules = new ArrayList<>(); + + for (ScheduleDto.DateSchedule dateSchedule : dateSchedules) { + String date = dateSchedule.getDate(); + List times = dateSchedule.getTimes(); + + List schedules = scheduleRepository.findAllByEventAndDate(event, date) + .orElseThrow(() -> new ScheduleException(ScheduleErrorResult._NOT_FOUND_DATE_SCHEDULES)); + + for (Schedule schedule : schedules) { + if (times.contains(schedule.getTime())) { + newSelections.add(Selection.builder() + .user(user) + .schedule(schedule) + .build()); + } + } + + allSchedules.addAll(schedules); + } + + selectionRepository.deleteAllByUserAndScheduleIn(user, allSchedules); + selectionRepository.saveAll(newSelections); + } + // 전체 요일 스케줄 반환 메서드 @Transactional public List getAllDaySchedules(String eventId) {