From a4978ad5c59eb1db196cc720c9361dae2cc6c977 Mon Sep 17 00:00:00 2001 From: bbbang105 <2018111366@dgu.ac.kr> Date: Thu, 24 Oct 2024 00:54:26 +0900 Subject: [PATCH] =?UTF-8?q?#90=20[feat]=20:=20=EC=9C=A0=EC=A0=80=EB=8A=94?= =?UTF-8?q?=20=EB=A7=88=EC=9D=B4=ED=8E=98=EC=9D=B4=EC=A7=80=EC=97=90?= =?UTF-8?q?=EC=84=9C=20=EA=B3=A0=EC=A0=95=20=EC=8A=A4=EC=BC=80=EC=A4=84?= =?UTF-8?q?=EC=9D=84=20=EC=88=98=EC=A0=95=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 --- .../onetime/controller/FixedController.java | 22 +++++++++++++-- .../java/side/onetime/domain/FixedEvent.java | 12 ++++++++ .../request/ModifyFixedEventRequest.java | 28 +++++++++++++++++++ .../global/common/status/SuccessStatus.java | 1 + .../onetime/service/FixedEventService.java | 12 +++++++- .../onetime/service/FixedScheduleService.java | 28 +++++++++++++++---- 6 files changed, 95 insertions(+), 8 deletions(-) create mode 100644 src/main/java/side/onetime/dto/fixed/request/ModifyFixedEventRequest.java diff --git a/src/main/java/side/onetime/controller/FixedController.java b/src/main/java/side/onetime/controller/FixedController.java index accf958..f5ee855 100644 --- a/src/main/java/side/onetime/controller/FixedController.java +++ b/src/main/java/side/onetime/controller/FixedController.java @@ -5,6 +5,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import side.onetime.dto.fixed.request.CreateFixedEventRequest; +import side.onetime.dto.fixed.request.ModifyFixedEventRequest; import side.onetime.dto.fixed.response.FixedEventDetailResponse; import side.onetime.dto.fixed.response.FixedEventResponse; import side.onetime.global.common.ApiResponse; @@ -46,10 +47,27 @@ public ResponseEntity>> getAllFixedSchedule @GetMapping("/{id}") public ResponseEntity> getFixedScheduleDetail( @RequestHeader("Authorization") String authorizationHeader, - @PathVariable("id") Long fixedScheduleId) { + @PathVariable("id") Long fixedEventId) { - FixedEventDetailResponse fixedEventDetailResponse = fixedScheduleService.getFixedScheduleDetail(authorizationHeader, fixedScheduleId); + FixedEventDetailResponse fixedEventDetailResponse = fixedScheduleService.getFixedScheduleDetail(authorizationHeader, fixedEventId); return ApiResponse.onSuccess(SuccessStatus._GET_FIXED_SCHEDULE_DETAIL, fixedEventDetailResponse); } + + // 고정 이벤트 또는 스케줄 수정 API + @PatchMapping("/{id}") + public ResponseEntity> modifyFixedEvent( + @RequestHeader("Authorization") String authorizationHeader, + @PathVariable("id") Long fixedEventId, + @RequestBody ModifyFixedEventRequest modifyFixedEventRequest) { + + if (modifyFixedEventRequest.title() != null) { + fixedEventService.modifyFixedEvent(authorizationHeader, fixedEventId, modifyFixedEventRequest); + } + if (modifyFixedEventRequest.schedules() != null) { + fixedScheduleService.modifyFixedSchedule(authorizationHeader, fixedEventId, modifyFixedEventRequest); + } + + return ApiResponse.onSuccess(SuccessStatus._MODIFY_FIXED_SCHEDULE); + } } diff --git a/src/main/java/side/onetime/domain/FixedEvent.java b/src/main/java/side/onetime/domain/FixedEvent.java index 83bc4d9..3692296 100644 --- a/src/main/java/side/onetime/domain/FixedEvent.java +++ b/src/main/java/side/onetime/domain/FixedEvent.java @@ -42,4 +42,16 @@ public FixedEvent(User user, String title, String startTime, String endTime) { this.startTime = startTime; this.endTime = endTime; } + + public void updateTitle(String title) { + this.title = title; + } + + public void updateStartTime(String startTime) { + this.startTime = startTime; + } + + public void updateEndTime(String endTime) { + this.endTime = endTime; + } } \ No newline at end of file diff --git a/src/main/java/side/onetime/dto/fixed/request/ModifyFixedEventRequest.java b/src/main/java/side/onetime/dto/fixed/request/ModifyFixedEventRequest.java new file mode 100644 index 0000000..6a87bf5 --- /dev/null +++ b/src/main/java/side/onetime/dto/fixed/request/ModifyFixedEventRequest.java @@ -0,0 +1,28 @@ +package side.onetime.dto.fixed.request; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.PropertyNamingStrategies; +import com.fasterxml.jackson.databind.annotation.JsonNaming; +import side.onetime.domain.FixedEvent; +import side.onetime.domain.User; +import side.onetime.dto.fixed.response.FixedScheduleResponse; + +import java.util.List; + +import static side.onetime.util.DateUtil.addThirtyMinutes; + +@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class) +@JsonInclude(JsonInclude.Include.NON_NULL) +public record ModifyFixedEventRequest( + String title, + List schedules +) { + public FixedEvent toEntity(User user, String startTime, String endTime) { + return FixedEvent.builder() + .user(user) + .title(title) + .startTime(startTime) + .endTime(addThirtyMinutes(endTime)) + .build(); + } +} \ No newline at end of file diff --git a/src/main/java/side/onetime/global/common/status/SuccessStatus.java b/src/main/java/side/onetime/global/common/status/SuccessStatus.java index 12c67f2..9e5cce9 100644 --- a/src/main/java/side/onetime/global/common/status/SuccessStatus.java +++ b/src/main/java/side/onetime/global/common/status/SuccessStatus.java @@ -48,6 +48,7 @@ public enum SuccessStatus implements BaseCode { _CREATED_FIXED_SCHEDULE(HttpStatus.CREATED, "201", "고정 스케줄 등록에 성공했습니다."), _GET_ALL_FIXED_SCHEDULES(HttpStatus.OK, "200", "전체 고정 스케줄 조회에 성공했습니다."), _GET_FIXED_SCHEDULE_DETAIL(HttpStatus.OK, "200", "특정 고정 스케줄 상세 조회에 성공했습니다."), + _MODIFY_FIXED_SCHEDULE(HttpStatus.OK, "200", "고정 스케줄 수정에 성공했습니다."), ; private final HttpStatus httpStatus; diff --git a/src/main/java/side/onetime/service/FixedEventService.java b/src/main/java/side/onetime/service/FixedEventService.java index 8f8b62e..5179866 100644 --- a/src/main/java/side/onetime/service/FixedEventService.java +++ b/src/main/java/side/onetime/service/FixedEventService.java @@ -6,6 +6,7 @@ import side.onetime.domain.FixedEvent; import side.onetime.domain.User; import side.onetime.dto.fixed.request.CreateFixedEventRequest; +import side.onetime.dto.fixed.request.ModifyFixedEventRequest; import side.onetime.repository.FixedEventRepository; import side.onetime.util.JwtUtil; @@ -28,6 +29,15 @@ public void createFixedEvent(String authorizationHeader, CreateFixedEventRequest String endTime = times.get(times.size() - 1); FixedEvent fixedEvent = createFixedEventRequest.toEntity(user, startTime, endTime); fixedEventRepository.save(fixedEvent); - fixedScheduleService.createFixedSchedules(createFixedEventRequest, fixedEvent); + fixedScheduleService.createFixedSchedules(createFixedEventRequest.schedules(), fixedEvent); + } + + // 고정 이벤트 수정 메서드 + @Transactional + public void modifyFixedEvent(String authorizationHeader, Long fixedEventId, ModifyFixedEventRequest modifyFixedEventRequest) { + User user = jwtUtil.getUserFromHeader(authorizationHeader); + FixedEvent fixedEvent = fixedEventRepository.findByUserAndId(user, fixedEventId); + fixedEvent.updateTitle(modifyFixedEventRequest.title()); + fixedEventRepository.save(fixedEvent); } } \ No newline at end of file diff --git a/src/main/java/side/onetime/service/FixedScheduleService.java b/src/main/java/side/onetime/service/FixedScheduleService.java index dec1f2e..34d717f 100644 --- a/src/main/java/side/onetime/service/FixedScheduleService.java +++ b/src/main/java/side/onetime/service/FixedScheduleService.java @@ -7,7 +7,7 @@ import side.onetime.domain.FixedSchedule; import side.onetime.domain.FixedSelection; import side.onetime.domain.User; -import side.onetime.dto.fixed.request.CreateFixedEventRequest; +import side.onetime.dto.fixed.request.ModifyFixedEventRequest; import side.onetime.dto.fixed.response.FixedEventDetailResponse; import side.onetime.dto.fixed.response.FixedEventResponse; import side.onetime.dto.fixed.response.FixedScheduleResponse; @@ -23,6 +23,8 @@ import java.util.Map; import java.util.stream.Collectors; +import static side.onetime.util.DateUtil.addThirtyMinutes; + @Service @RequiredArgsConstructor public class FixedScheduleService { @@ -33,8 +35,7 @@ public class FixedScheduleService { // 고정 스케줄 등록 메서드 @Transactional - public void createFixedSchedules(CreateFixedEventRequest createFixedEventRequest, FixedEvent fixedEvent) { - List fixedScheduleResponses = createFixedEventRequest.schedules(); + public void createFixedSchedules(List fixedScheduleResponses, FixedEvent fixedEvent) { List fixedSelections = new ArrayList<>(); for (FixedScheduleResponse fixedScheduleResponse : fixedScheduleResponses) { @@ -90,11 +91,11 @@ public List getAllFixedSchedules(String authorizationHeader) // 특정 고정 스케줄 상세 조회 메서드 @Transactional(readOnly = true) - public FixedEventDetailResponse getFixedScheduleDetail(String authorizationHeader, Long fixedScheduleId) { + public FixedEventDetailResponse getFixedScheduleDetail(String authorizationHeader, Long fixedEventId) { User user = jwtUtil.getUserFromHeader(authorizationHeader); // 고정 이벤트 조회 - FixedEvent fixedEvent = fixedEventRepository.findByUserAndFixedEventId(user, fixedScheduleId); + FixedEvent fixedEvent = fixedEventRepository.findByUserAndFixedEventIdCustom(user, fixedEventId); if (fixedEvent == null) { throw new CustomException(FixedErrorStatus._NOT_FOUND_FIXED_EVENT); } @@ -114,4 +115,21 @@ public FixedEventDetailResponse getFixedScheduleDetail(String authorizationHeade // 고정 이벤트 상세 정보 반환 return FixedEventDetailResponse.of(fixedEvent.getTitle(), fixedEvent.getStartTime(), fixedEvent.getEndTime(), scheduleResponses); } + + // 고정 스케줄 수정 메서드 + @Transactional + public void modifyFixedSchedule(String authorizationHeader, Long fixedEventId, ModifyFixedEventRequest modifyFixedEventRequest) { + User user = jwtUtil.getUserFromHeader(authorizationHeader); + FixedEvent fixedEvent = fixedEventRepository.findByUserAndId(user, fixedEventId); + + List times = modifyFixedEventRequest.schedules().get(0).times(); + String startTime = times.get(0); + String endTime = times.get(times.size() - 1); + fixedEvent.updateStartTime(startTime); + fixedEvent.updateEndTime(addThirtyMinutes(endTime)); + fixedEventRepository.save(fixedEvent); + + fixedSelectionRepository.deleteFixedSelectionsByEvent(fixedEvent); + createFixedSchedules(modifyFixedEventRequest.schedules(), fixedEvent); + } } \ No newline at end of file