From c356b78ad34809eb57653c36d847fa8599008cdc Mon Sep 17 00:00:00 2001 From: bbbang105 <2018111366@dgu.ac.kr> Date: Thu, 24 Oct 2024 01:15:06 +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=82=AD=EC=A0=9C=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 | 11 ++++++++++ .../global/common/status/SuccessStatus.java | 1 + .../repository/FixedEventRepository.java | 4 +++- .../custom/FixedEventRepositoryCustom.java | 1 + .../custom/FixedEventRepositoryImpl.java | 20 +++++++++++++++++++ .../onetime/service/FixedEventService.java | 14 ++++++++++++- .../onetime/service/FixedScheduleService.java | 3 ++- 7 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/main/java/side/onetime/controller/FixedController.java b/src/main/java/side/onetime/controller/FixedController.java index f5ee855..080b6fa 100644 --- a/src/main/java/side/onetime/controller/FixedController.java +++ b/src/main/java/side/onetime/controller/FixedController.java @@ -70,4 +70,15 @@ public ResponseEntity> modifyFixedEvent( return ApiResponse.onSuccess(SuccessStatus._MODIFY_FIXED_SCHEDULE); } + + // 고정 이벤트 & 스케줄 삭제 API + @DeleteMapping("/{id}") + public ResponseEntity> removeFixedEvent( + @RequestHeader("Authorization") String authorizationHeader, + @PathVariable("id") Long fixedEventId) { + + fixedEventService.removeFixedEvent(authorizationHeader, fixedEventId); + + return ApiResponse.onSuccess(SuccessStatus._REMOVE_FIXED_SCHEDULE); + } } 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 9e5cce9..943f144 100644 --- a/src/main/java/side/onetime/global/common/status/SuccessStatus.java +++ b/src/main/java/side/onetime/global/common/status/SuccessStatus.java @@ -49,6 +49,7 @@ public enum SuccessStatus implements BaseCode { _GET_ALL_FIXED_SCHEDULES(HttpStatus.OK, "200", "전체 고정 스케줄 조회에 성공했습니다."), _GET_FIXED_SCHEDULE_DETAIL(HttpStatus.OK, "200", "특정 고정 스케줄 상세 조회에 성공했습니다."), _MODIFY_FIXED_SCHEDULE(HttpStatus.OK, "200", "고정 스케줄 수정에 성공했습니다."), + _REMOVE_FIXED_SCHEDULE(HttpStatus.OK, "200", "고정 스케줄 삭제에 성공했습니다."), ; private final HttpStatus httpStatus; diff --git a/src/main/java/side/onetime/repository/FixedEventRepository.java b/src/main/java/side/onetime/repository/FixedEventRepository.java index ce2d75b..c915949 100644 --- a/src/main/java/side/onetime/repository/FixedEventRepository.java +++ b/src/main/java/side/onetime/repository/FixedEventRepository.java @@ -5,6 +5,8 @@ import side.onetime.domain.User; import side.onetime.repository.custom.FixedEventRepositoryCustom; +import java.util.Optional; + public interface FixedEventRepository extends JpaRepository, FixedEventRepositoryCustom { - FixedEvent findByUserAndId(User user, Long id); + Optional findByUserAndId(User user, Long id); } \ No newline at end of file diff --git a/src/main/java/side/onetime/repository/custom/FixedEventRepositoryCustom.java b/src/main/java/side/onetime/repository/custom/FixedEventRepositoryCustom.java index c51a321..2a846e5 100644 --- a/src/main/java/side/onetime/repository/custom/FixedEventRepositoryCustom.java +++ b/src/main/java/side/onetime/repository/custom/FixedEventRepositoryCustom.java @@ -8,4 +8,5 @@ public interface FixedEventRepositoryCustom { List findAllByUser(User user); FixedEvent findByUserAndFixedEventIdCustom(User user, Long fixedEventId); + void deleteFixedEventAndSelections(User user, Long fixedEventId); } \ No newline at end of file diff --git a/src/main/java/side/onetime/repository/custom/FixedEventRepositoryImpl.java b/src/main/java/side/onetime/repository/custom/FixedEventRepositoryImpl.java index 16092e2..1fe4a7b 100644 --- a/src/main/java/side/onetime/repository/custom/FixedEventRepositoryImpl.java +++ b/src/main/java/side/onetime/repository/custom/FixedEventRepositoryImpl.java @@ -2,6 +2,7 @@ import com.querydsl.jpa.impl.JPAQueryFactory; import lombok.RequiredArgsConstructor; +import org.springframework.transaction.annotation.Transactional; import side.onetime.domain.FixedEvent; import side.onetime.domain.User; @@ -18,6 +19,7 @@ public class FixedEventRepositoryImpl implements FixedEventRepositoryCustom { // 유저 고정 스케줄 목록 조회 @Override + @Transactional(readOnly = true) public List findAllByUser(User user) { return queryFactory.selectFrom(fixedEvent) .leftJoin(fixedEvent.fixedSelections, fixedSelection) @@ -30,6 +32,7 @@ public List findAllByUser(User user) { // 특정 고정 스케줄 상세 조회 @Override + @Transactional(readOnly = true) public FixedEvent findByUserAndFixedEventIdCustom(User user, Long fixedEventId) { return queryFactory.selectFrom(fixedEvent) .leftJoin(fixedEvent.fixedSelections, fixedSelection) @@ -40,4 +43,21 @@ public FixedEvent findByUserAndFixedEventIdCustom(User user, Long fixedEventId) .and(fixedEvent.id.eq(fixedEventId))) .fetchOne(); } + + // 고정 이벤트 & 스케줄 삭제 + @Override + @Transactional + public void deleteFixedEventAndSelections(User user, Long fixedEventId) { + // fixed_selections 삭제 + queryFactory.delete(fixedSelection) + .where(fixedSelection.fixedEvent.id.eq(fixedEventId) + .and(fixedSelection.fixedEvent.user.eq(user))) + .execute(); + + // fixed_events 삭제 + queryFactory.delete(fixedEvent) + .where(fixedEvent.id.eq(fixedEventId) + .and(fixedEvent.user.eq(user))) + .execute(); + } } \ No newline at end of file diff --git a/src/main/java/side/onetime/service/FixedEventService.java b/src/main/java/side/onetime/service/FixedEventService.java index 5179866..239e070 100644 --- a/src/main/java/side/onetime/service/FixedEventService.java +++ b/src/main/java/side/onetime/service/FixedEventService.java @@ -7,6 +7,8 @@ import side.onetime.domain.User; import side.onetime.dto.fixed.request.CreateFixedEventRequest; import side.onetime.dto.fixed.request.ModifyFixedEventRequest; +import side.onetime.exception.CustomException; +import side.onetime.exception.status.FixedErrorStatus; import side.onetime.repository.FixedEventRepository; import side.onetime.util.JwtUtil; @@ -36,8 +38,18 @@ public void createFixedEvent(String authorizationHeader, CreateFixedEventRequest @Transactional public void modifyFixedEvent(String authorizationHeader, Long fixedEventId, ModifyFixedEventRequest modifyFixedEventRequest) { User user = jwtUtil.getUserFromHeader(authorizationHeader); - FixedEvent fixedEvent = fixedEventRepository.findByUserAndId(user, fixedEventId); + FixedEvent fixedEvent = fixedEventRepository.findByUserAndId(user, fixedEventId) + .orElseThrow(() -> new CustomException(FixedErrorStatus._NOT_FOUND_FIXED_EVENT)); fixedEvent.updateTitle(modifyFixedEventRequest.title()); fixedEventRepository.save(fixedEvent); } + + // 고정 이벤트 & 스케줄 삭제 메서드 + @Transactional + public void removeFixedEvent(String authorizationHeader, Long fixedEventId) { + User user = jwtUtil.getUserFromHeader(authorizationHeader); + FixedEvent fixedEvent = fixedEventRepository.findByUserAndId(user, fixedEventId) + .orElseThrow(() -> new CustomException(FixedErrorStatus._NOT_FOUND_FIXED_EVENT)); + fixedEventRepository.deleteFixedEventAndSelections(user, fixedEventId); + } } \ 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 34d717f..0008639 100644 --- a/src/main/java/side/onetime/service/FixedScheduleService.java +++ b/src/main/java/side/onetime/service/FixedScheduleService.java @@ -120,7 +120,8 @@ public FixedEventDetailResponse getFixedScheduleDetail(String authorizationHeade @Transactional public void modifyFixedSchedule(String authorizationHeader, Long fixedEventId, ModifyFixedEventRequest modifyFixedEventRequest) { User user = jwtUtil.getUserFromHeader(authorizationHeader); - FixedEvent fixedEvent = fixedEventRepository.findByUserAndId(user, fixedEventId); + FixedEvent fixedEvent = fixedEventRepository.findByUserAndId(user, fixedEventId) + .orElseThrow(() -> new CustomException(FixedErrorStatus._NOT_FOUND_FIXED_EVENT)); List times = modifyFixedEventRequest.schedules().get(0).times(); String startTime = times.get(0);