|
1 | 1 | package com.back.domain.study.plan.service; |
2 | 2 |
|
| 3 | +import com.back.domain.study.plan.dto.StudyPlanDeleteRequest; |
3 | 4 | import com.back.domain.study.plan.dto.StudyPlanRequest; |
4 | 5 | import com.back.domain.study.plan.dto.StudyPlanResponse; |
5 | 6 | import com.back.domain.study.plan.entity.RepeatRule; |
@@ -201,7 +202,7 @@ private boolean shouldRepeatOnDate(StudyPlan originalPlan, LocalDate targetDate) |
201 | 202 | private StudyPlanException getEffectiveException(Long planId, LocalDate targetDate) { |
202 | 203 | // 해당 날짜에 직접적인 예외가 있는지 확인 |
203 | 204 | Optional<StudyPlanException> directException = studyPlanExceptionRepository |
204 | | - .findByPlanIdAndDate(planId, targetDate.atStartOfDay()); |
| 205 | + .findByPlanIdAndDate(planId, targetDate); |
205 | 206 | if (directException.isPresent()) { |
206 | 207 | return directException.get(); |
207 | 208 | } |
@@ -320,7 +321,7 @@ private UpdateType determineUpdateType(StudyPlan originalPlan, StudyPlanRequest |
320 | 321 |
|
321 | 322 | // 1-2. 반복 계획에서 다른 날짜인 경우 -> 기존 예외 확인 |
322 | 323 | Optional<StudyPlanException> existingException = studyPlanExceptionRepository |
323 | | - .findByPlanIdAndDate(originalPlan.getId(), requestDate.atStartOfDay()); |
| 324 | + .findByPlanIdAndDate(originalPlan.getId(), requestDate); |
324 | 325 |
|
325 | 326 | if (existingException.isPresent()) { |
326 | 327 | return UpdateType.REPEAT_INSTANCE_UPDATE; // 기존 예외 수정 |
@@ -357,7 +358,7 @@ private StudyPlanResponse createRepeatException(StudyPlan originalPlan, StudyPla |
357 | 358 |
|
358 | 359 | StudyPlanException exception = new StudyPlanException(); |
359 | 360 | exception.setStudyPlan(originalPlan); |
360 | | - exception.setExceptionDate(exceptionDate.atStartOfDay()); |
| 361 | + exception.setExceptionDate(exceptionDate); |
361 | 362 | exception.setExceptionType(StudyPlanException.ExceptionType.MODIFIED); |
362 | 363 | exception.setApplyScope(applyScope); // 파라미터로 받은 applyScope |
363 | 364 |
|
@@ -396,7 +397,7 @@ private StudyPlanResponse updateExistingException(StudyPlan originalPlan, StudyP |
396 | 397 | LocalDate exceptionDate = request.getStartDate().toLocalDate(); |
397 | 398 |
|
398 | 399 | StudyPlanException existingException = studyPlanExceptionRepository |
399 | | - .findByPlanIdAndDate(originalPlan.getId(), exceptionDate.atStartOfDay()) |
| 400 | + .findByPlanIdAndDate(originalPlan.getId(), exceptionDate) |
400 | 401 | .orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND)); |
401 | 402 |
|
402 | 403 | // 기존 예외 정보 업데이트 |
@@ -449,7 +450,49 @@ private void updateRepeatRule(RepeatRule repeatRule, StudyPlanRequest.RepeatRule |
449 | 450 | } |
450 | 451 |
|
451 | 452 | // ==================== 삭제 =================== |
| 453 | + @Transactional |
| 454 | + public void deleteStudyPlan(Long userId, Long planId, LocalDate selectedDate, StudyPlanDeleteRequest request) { |
| 455 | + StudyPlan studyPlan = studyPlanRepository.findById(planId) |
| 456 | + .orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND)); |
| 457 | + |
| 458 | + validateUserAccess(studyPlan, userId); |
452 | 459 |
|
| 460 | + // 단발성 계획 삭제 (request가 null이거나 deleteScope가 없는 경우) |
| 461 | + if (studyPlan.getRepeatRule() == null || request == null || request.getDeleteScope() == null) { |
| 462 | + studyPlanRepository.delete(studyPlan); |
| 463 | + return; |
| 464 | + } |
| 465 | + |
| 466 | + // 반복성 계획 삭제 - deleteScope에 따른 처리 |
| 467 | + deleteRepeatPlan(studyPlan, selectedDate, request.getDeleteScope()); |
| 468 | + } |
| 469 | + |
| 470 | + private void deleteRepeatPlan(StudyPlan studyPlan, LocalDate selectedDate, StudyPlanDeleteRequest.DeleteScope deleteScope) { |
| 471 | + switch (deleteScope) { |
| 472 | + case FROM_THIS_DATE: |
| 473 | + // 원본 날짜부터 삭제하는 경우 = 전체 계획 삭제 |
| 474 | + if (selectedDate.equals(studyPlan.getStartDate().toLocalDate())) { |
| 475 | + studyPlanRepository.delete(studyPlan); // CASCADE로 RepeatRule, Exception 모두 삭제 |
| 476 | + } else { |
| 477 | + // 중간 날짜부터 삭제하는 경우 = untilDate 수정 |
| 478 | + RepeatRule repeatRule = studyPlan.getRepeatRule(); |
| 479 | + LocalDate newUntilDate = selectedDate.minusDays(1); |
| 480 | + repeatRule.setUntilDate(newUntilDate); |
| 481 | + studyPlanRepository.save(studyPlan); |
| 482 | + } |
| 483 | + break; |
| 484 | + |
| 485 | + case THIS_ONLY: |
| 486 | + // 선택한 날짜만 삭제 - 예외 생성 |
| 487 | + StudyPlanException exception = new StudyPlanException(); |
| 488 | + exception.setStudyPlan(studyPlan); |
| 489 | + exception.setExceptionDate(selectedDate); |
| 490 | + exception.setExceptionType(StudyPlanException.ExceptionType.DELETED); |
| 491 | + exception.setApplyScope(StudyPlanException.ApplyScope.THIS_ONLY); |
| 492 | + studyPlanExceptionRepository.save(exception); |
| 493 | + break; |
| 494 | + } |
| 495 | + } |
453 | 496 |
|
454 | 497 | // ==================== 유틸 =================== |
455 | 498 | // 인가 (작성자 일치 확인) |
|
0 commit comments