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);