Skip to content

Commit bdc67c9

Browse files
authored
Merge pull request #60 from YAPP-Github/refactor/T3-157
[T3-157] 루틴 삭제 서비스 로직에 v2가 호환되도록 수정
2 parents b7f852d + 0dfa7ab commit bdc67c9

7 files changed

Lines changed: 86 additions & 28 deletions

File tree

src/main/java/bitnagil/bitnagil_backend/routine/controller/RoutineController.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,28 @@
33
import java.time.LocalDate;
44
import java.util.UUID;
55

6-
import bitnagil.bitnagil_backend.routine.domain.enums.RoutineType;
76
import bitnagil.bitnagil_backend.routine.request.DeleteRoutineByDayRequest;
87
import bitnagil.bitnagil_backend.routine.request.UpdateRoutineCompletionRequest;
98
import bitnagil.bitnagil_backend.routine.response.RoutineSearchResultDto;
109
import jakarta.validation.constraints.NotNull;
1110

12-
import org.springframework.security.core.parameters.P;
1311
import org.springframework.web.bind.annotation.DeleteMapping;
1412
import org.springframework.web.bind.annotation.PatchMapping;
1513
import org.springframework.web.bind.annotation.PathVariable;
1614
import org.springframework.web.bind.annotation.PostMapping;
1715
import org.springframework.web.bind.annotation.RequestBody;
1816
import org.springframework.web.bind.annotation.RequestMapping;
1917
import org.springframework.web.bind.annotation.RestController;
20-
import bitnagil.bitnagil_backend.routine.request.RoutineSearchRequest;
2118
import bitnagil.bitnagil_backend.routine.response.RoutineSearchResponse;
2219
import org.springframework.web.bind.annotation.*;
2320

2421
import bitnagil.bitnagil_backend.global.annotation.CurrentUser;
2522
import bitnagil.bitnagil_backend.global.response.CustomResponseDto;
2623
import bitnagil.bitnagil_backend.routine.controller.spec.RoutineSpec;
27-
import bitnagil.bitnagil_backend.routine.domain.Routine;
2824
import bitnagil.bitnagil_backend.routine.request.RegisterRoutineRequest;
2925
import bitnagil.bitnagil_backend.routine.request.UpdateRoutineRequest;
3026
import bitnagil.bitnagil_backend.routine.service.RoutineService;
3127
import bitnagil.bitnagil_backend.user.domain.User;
32-
import lombok.Getter;
3328
import lombok.RequiredArgsConstructor;
3429

3530
@RestController
@@ -56,7 +51,7 @@ public CustomResponseDto<Object> updateRoutine(@CurrentUser User user,
5651
}
5752

5853
@DeleteMapping("/{routineId}")
59-
public CustomResponseDto<Object> deleteRoutine(@CurrentUser User user, @PathVariable UUID routineId) {
54+
public CustomResponseDto<Object> deleteRoutine(@CurrentUser User user, @PathVariable String routineId) {
6055
routineService.deleteRoutine(user, routineId);
6156

6257
return CustomResponseDto.from(null);

src/main/java/bitnagil/bitnagil_backend/routine/controller/spec/RoutineSpec.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
import java.time.LocalDate;
44
import java.util.UUID;
55

6-
import org.springframework.web.bind.annotation.PathVariable;
7-
import org.springframework.web.bind.annotation.RequestBody;
8-
9-
import bitnagil.bitnagil_backend.global.annotation.CurrentUser;
106
import bitnagil.bitnagil_backend.global.errorcode.ErrorCode;
117
import bitnagil.bitnagil_backend.global.response.CustomResponseDto;
128
import bitnagil.bitnagil_backend.global.swagger.ApiErrorCodeExamples;
@@ -45,7 +41,7 @@ public interface RoutineSpec {
4541

4642
@Operation(summary = "루틴 및 서브 루틴을 모두 삭제합니다.")
4743
@ApiErrorCodeExamples({ErrorCode.NOT_FOUND_ROUTINE, ErrorCode.ROUTINE_USER_NOT_MATCHED})
48-
CustomResponseDto<Object> deleteRoutine(User user, UUID routineId);
44+
CustomResponseDto<Object> deleteRoutine(User user, String routineId);
4945

5046
@Operation(summary = "여러 루틴의 완료 여부를 갱신합니다.")
5147
@ApiErrorCodeExamples({

src/main/java/bitnagil/bitnagil_backend/routine/service/RoutineService.java

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
import bitnagil.bitnagil_backend.routine.request.RegisterRoutineRequest;
4141
import bitnagil.bitnagil_backend.routine.request.SubRoutineInfo;
4242
import bitnagil.bitnagil_backend.routine.request.UpdateRoutineRequest;
43+
import bitnagil.bitnagil_backend.routineInfoV2.domain.RoutineInfoV2;
44+
import bitnagil.bitnagil_backend.routineInfoV2.repository.RoutineInfoV2Repository;
45+
import bitnagil.bitnagil_backend.routineV2.domain.RoutineV2;
46+
import bitnagil.bitnagil_backend.routineV2.repository.RoutineV2Repository;
4347
import bitnagil.bitnagil_backend.user.domain.User;
4448
import lombok.RequiredArgsConstructor;
4549
import lombok.extern.slf4j.Slf4j;
@@ -63,6 +67,8 @@ public class RoutineService {
6367
private final RoutineFactory routineFactory;
6468
private final RoutineMapper routineMapper;
6569
private final ChangedRoutineFactory changedRoutineFactory;
70+
private final RoutineV2Repository routineV2Repository;
71+
private final RoutineInfoV2Repository routineInfoV2Repository;
6672

6773
// 루틴, 세부루틴을 함께 저장하는 루틴 등록 메서드
6874
@Transactional
@@ -157,24 +163,20 @@ public void updateRoutine(User user, UpdateRoutineRequest request) {
157163
}
158164
}
159165

160-
// 루틴, 세부 루틴을 삭제하는 메서드
166+
// 루틴을 모든 날짜에서 삭제하는 메서드
161167
@Transactional
162-
public void deleteRoutine(User user, UUID routineId) {
168+
public void deleteRoutine(User user, String routineId) {
169+
LocalDate today = LocalDate.now();
163170
LocalDateTime now = LocalDateTime.now();
164171

165-
Routine routine = routineValidator.validateRoutineOwnership(routineId, user, now);
166-
167-
// 기존 루틴, 서브 루틴의 이력 종료일시 및 deleteAt 갱신
168-
routine.updateHistoryEndDateTime(now);
169-
routine.setDeleteAt(now);
170-
171-
// 서브 루틴을 순회하면서 이력 종료일시 및 deleteAt 갱신
172-
subRoutineRepository.findByRoutineId(routineId)
173-
.forEach(subRoutine -> {
174-
subRoutine.updateHistoryEndDateTime(now);
175-
subRoutine.setDeleteAt(now);
176-
});
177-
172+
if (routineId.length() == 36) { // (v1) routineId의 타입이 UUID인 경우
173+
UUID v1RoutineId = UUID.fromString(routineId);
174+
deleteV1Routine(user, v1RoutineId, now);
175+
}
176+
else { // (v2) routineId의 타입이 Long인 경우
177+
Long v2RoutineId = Long.valueOf(routineId);
178+
deleteV2Routine(user, v2RoutineId, today);
179+
}
178180
}
179181

180182
// 유저가 선택한 요일(당일)만 루틴, 서브 루틴을 삭제하는 메서드
@@ -273,6 +275,43 @@ public void updateRoutineCompletionStatus(User user, UpdateRoutineCompletionRequ
273275
}
274276
}
275277

278+
// v2에서 사용하는 루틴 삭제 메서드
279+
private void deleteV2Routine(User user, Long v2RoutineId, LocalDate today) {
280+
RoutineV2 routineV2 = routineV2Repository.findByUserAndRoutineId(user, v2RoutineId)
281+
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_ROUTINE));
282+
283+
RoutineInfoV2 routineInfoV2 = routineV2.getRoutineInfo();
284+
285+
// 오늘 이후 루틴 내역 모두 삭제 (Hard Delete)
286+
List<RoutineV2> routinesV2AfterToday = routineV2Repository
287+
.findByRoutineInfoAndRoutineDateAfter(routineInfoV2, today);
288+
289+
List<Long> routineIds = routinesV2AfterToday.stream()
290+
.map(RoutineV2::getRoutineId)
291+
.toList();
292+
293+
routineV2Repository.deleteAllPhysicallyByIds(routineIds); // 물리 삭제
294+
295+
routineInfoV2.updateRoutineEndDate(today); // 종료 일자를 삭제 당일로 변경
296+
routineInfoV2.updateRoutineDeletedYn(true); // 루틴 삭제 여부 갱신
297+
}
298+
299+
// v1에서 사용하는 루틴 삭제 메서드
300+
private void deleteV1Routine(User user, UUID v1RoutineId, LocalDateTime now) {
301+
Routine routine = routineValidator.validateRoutineOwnership(v1RoutineId, user, now);
302+
303+
// 기존 루틴, 서브 루틴의 이력 종료일시 및 deleteAt 갱신
304+
routine.updateHistoryEndDateTime(now);
305+
routine.setDeleteAt(now);
306+
307+
// 서브 루틴을 순회하면서 이력 종료일시 및 deleteAt 갱신
308+
subRoutineRepository.findByRoutineId(v1RoutineId)
309+
.forEach(subRoutine -> {
310+
subRoutine.updateHistoryEndDateTime(now);
311+
subRoutine.setDeleteAt(now);
312+
});
313+
}
314+
276315
// routineCompletionId에 해당하는 완료 여부 데이터 삭제
277316
private void deleteRoutineCompletionIfRoutineIdMatches(Long routineCompletionId, UUID routineId) {
278317

src/main/java/bitnagil/bitnagil_backend/routineInfoV2/domain/RoutineInfoV2.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
@Getter
3131
@NoArgsConstructor(access = AccessLevel.PROTECTED)
3232
@Entity
33-
@SQLDelete(sql = "UPDATE routine_info_v2 SET deleted_at = NOW() WHERE routine_info_id = ?")
33+
@SQLDelete(sql = "UPDATE routine_infov2 SET deleted_at = NOW() WHERE routine_info_id = ?")
3434
@Where(clause = "deleted_at IS NULL")
3535
public class RoutineInfoV2 extends BaseTimeEntity {
3636
@Id
@@ -53,18 +53,30 @@ public class RoutineInfoV2 extends BaseTimeEntity {
5353
@NotNull
5454
private LocalDate routineEndDate; // 루틴 종료 일자
5555

56+
@NotNull
57+
private Boolean routineDeletedYn; // 루틴 삭제 여부
58+
5659
@ManyToOne(fetch = FetchType.LAZY)
5760
@JoinColumn(name = "user_id")
5861
private User user; // 루틴의 주체인 유저
5962

6063
@Builder
6164
public RoutineInfoV2(String routineName, List<DayOfWeek> routineRepeatDay, LocalTime routineExecutionTime,
62-
LocalDate routineStartDate, LocalDate routineEndDate, User user) {
65+
LocalDate routineStartDate, LocalDate routineEndDate, Boolean routineDeletedYn, User user) {
6366
this.routineName = routineName;
6467
this.routineRepeatDay = routineRepeatDay;
6568
this.routineExecutionTime = routineExecutionTime;
6669
this.routineStartDate = routineStartDate;
6770
this.routineEndDate = routineEndDate;
71+
this.routineDeletedYn = routineDeletedYn;
6872
this.user = user;
6973
}
74+
75+
public void updateRoutineEndDate(LocalDate routineEndDate) {
76+
this.routineEndDate = routineEndDate;
77+
}
78+
79+
public void updateRoutineDeletedYn(Boolean routineDeletedYn) {
80+
this.routineDeletedYn = routineDeletedYn;
81+
}
7082
}

src/main/java/bitnagil/bitnagil_backend/routineInfoV2/service/RoutineInfoV2Factory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public RoutineInfoV2 createNewRoutineInfo(String routineName, List<DayOfWeek> ro
2525
.routineExecutionTime(routineExecutionTime)
2626
.routineStartDate(routineStartDate)
2727
.routineEndDate(routineEndDate)
28+
.routineDeletedYn(false)
2829
.user(user)
2930
.build();
3031
}

src/main/java/bitnagil/bitnagil_backend/routineV2/repository/RoutineV2Repository.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package bitnagil.bitnagil_backend.routineV2.repository;
22

3+
import bitnagil.bitnagil_backend.routineInfoV2.domain.RoutineInfoV2;
34
import bitnagil.bitnagil_backend.routineV2.domain.RoutineV2;
45
import bitnagil.bitnagil_backend.user.domain.User;
56
import org.springframework.data.jpa.repository.JpaRepository;
7+
import org.springframework.data.jpa.repository.Modifying;
68
import org.springframework.data.jpa.repository.Query;
79
import org.springframework.data.repository.query.Param;
810
import org.springframework.stereotype.Repository;
@@ -43,4 +45,15 @@ Optional<RoutineV2> findByUserAndRoutineId(
4345
@Param("user") User user,
4446
@Param("routineId") Long routineId
4547
);
48+
49+
// 오늘 이후의 루틴 정보에 맞는 루틴 내역을 조회
50+
List<RoutineV2> findByRoutineInfoAndRoutineDateAfter(RoutineInfoV2 routineInfoV2, LocalDate date);
51+
52+
/**
53+
* 루틴을 물리 삭제하기 위한 JPQL
54+
* 메모리 로드 비용 줄이기 위해 routineIds를 파라미터로 채택
55+
*/
56+
@Modifying
57+
@Query("DELETE FROM RoutineV2 r WHERE r.routineId IN :ids")
58+
void deleteAllPhysicallyByIds(@Param("ids") List<Long> routineIds);
4659
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE routine_infov2
2+
ADD COLUMN routine_deleted_yn BIT(1) NOT NULL DEFAULT b'0'

0 commit comments

Comments
 (0)