Skip to content

Commit e1f2692

Browse files
authored
Merge pull request #46 from YAPP-Github/fix/T3-143
[T3-143] 선택한 요일(당일) 삭제 시 변경된 루틴에 대한 분기처리
2 parents 3953723 + 58c0c9c commit e1f2692

4 files changed

Lines changed: 49 additions & 24 deletions

File tree

src/main/java/bitnagil/bitnagil_backend/changedRoutine/domain/ChangedRoutine.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,8 @@ public ChangedRoutine(HistoryPk changedRoutinePk, String changedRoutineName, Loc
7474
this.routineId = routineId;
7575
}
7676

77+
public void updateChangedDivCode(ChangedDivCode changedDivCode) {
78+
this.changedDivCode = changedDivCode;
79+
}
80+
7781
}

src/main/java/bitnagil/bitnagil_backend/routine/request/DeleteRoutineByDayRequest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.List;
55
import java.util.UUID;
66

7+
import bitnagil.bitnagil_backend.routine.domain.enums.RoutineType;
78
import io.swagger.v3.oas.annotations.media.Schema;
89
import jakarta.validation.constraints.NotNull;
910
import lombok.Getter;
@@ -24,6 +25,12 @@ public class DeleteRoutineByDayRequest {
2425
@NotNull
2526
private UUID routineId;
2627

28+
@Schema(description = "루틴에 대한 타입 값입니다.",
29+
example = "CHANGED_ROUTINE",
30+
required = true)
31+
@NotNull
32+
private RoutineType routineType;
33+
2734
@Schema(description = "세부루틴 완료 여부 정보를 담은 리스트입니다.",
2835
example = "["
2936
+ "{\"routineCompletionId\": 3, \"subRoutineId\": \"4fa85f64-5717-4562-b3fc-2c963f66afa6\"},"

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,23 +163,33 @@ public void deleteRoutine(User user, UUID routineId) {
163163
public void deleteRoutineByDay(User user, DeleteRoutineByDayRequest request) {
164164
LocalDateTime now = LocalDateTime.now();
165165

166-
Routine routine = routineValidator.validateRoutineOwnership(request.getRoutineId(), user, now);
166+
if (request.getRoutineType() == RoutineType.ROUTINE) {
167+
Routine routine = routineValidator.validateRoutine(user, request.getRoutineId(), request.getHistorySeq());
167168

168-
ChangedRoutine changedRoutineForDelete = routineFactory.createChangedRoutineForDelete(request, routine, now);
169-
changedRoutineRepository.save(changedRoutineForDelete);
169+
// 변경 루틴으로 전환
170+
ChangedRoutine changedRoutineForDelete = routineFactory.createChangedRoutineForDelete(request, routine, now);
171+
changedRoutineRepository.save(changedRoutineForDelete);
170172

171-
// routineCompletionId에 해당하는 완료 여부 데이터 삭제
172-
deleteRoutineCompletionIfRoutineIdMatches(request.getRoutineCompletionId(), request.getRoutineId());
173+
List<SubRoutine> subRoutines = subRoutineRepository.findByRoutineId(routine.getRoutinePk().getId());
173174

174-
// 변경 서브루틴으로 전환
175-
List<SubRoutine> subRoutines = subRoutineRepository.findByRoutineId(routine.getRoutinePk().getId());
175+
// 변경 서브루틴으로 전환
176+
for (SubRoutine subRoutine : subRoutines) {
177+
ChangedSubRoutine changedSubRoutineForDelete =
178+
routineFactory.createChangedSubRoutineForDelete(subRoutine, now, changedRoutineForDelete);
179+
changedSubRoutineRepository.save(changedSubRoutineForDelete);
180+
}
181+
}
182+
else if (request.getRoutineType() == RoutineType.CHANGED_ROUTINE) {
183+
ChangedRoutine changedRoutine = routineValidator.validateChangedRoutine(user, request.getRoutineId(),
184+
request.getHistorySeq());
176185

177-
for (SubRoutine subRoutine : subRoutines) {
178-
ChangedSubRoutine changedSubRoutineForDelete =
179-
routineFactory.createChangedSubRoutineForDelete(subRoutine, now, changedRoutineForDelete);
180-
changedSubRoutineRepository.save(changedSubRoutineForDelete);
186+
// 기존 변경 루틴의 결정 코드를 "오늘만 루틴 삭제"로 변경
187+
changedRoutine.updateChangedDivCode(ChangedDivCode.TODAY_DELETE);
181188
}
182189

190+
// routineCompletionId에 해당하는 완료 여부 데이터 삭제
191+
deleteRoutineCompletionIfRoutineIdMatches(request.getRoutineCompletionId(), request.getRoutineId());
192+
183193
// routineCompletionId에 해당하는 완료 여부 데이터 삭제
184194
for (SubRoutineInfoForDelete info : request.getSubRoutineInfosForDelete()) {
185195
deleteRoutineCompletionIfRoutineIdMatches(info.getRoutineCompletionId(), info.getSubRoutineId());

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
import bitnagil.bitnagil_backend.global.exception.CustomException;
1616
import bitnagil.bitnagil_backend.routine.domain.Routine;
1717
import bitnagil.bitnagil_backend.routine.domain.SubRoutine;
18-
import bitnagil.bitnagil_backend.routine.domain.enums.RoutineType;
1918
import bitnagil.bitnagil_backend.routine.repository.RoutineRepository;
2019
import bitnagil.bitnagil_backend.routine.repository.SubRoutineRepository;
20+
import bitnagil.bitnagil_backend.routine.request.DeleteRoutineByDayRequest;
2121
import bitnagil.bitnagil_backend.routine.request.RoutineCompletionInfo;
2222
import bitnagil.bitnagil_backend.user.domain.User;
2323
import lombok.RequiredArgsConstructor;
@@ -38,16 +38,16 @@ public class RoutineValidator {
3838
public void validateRoutineOwnership(User user, RoutineCompletionInfo info) {
3939
switch (info.getRoutineType()) {
4040
case ROUTINE:
41-
validateRoutine(user, info);
41+
validateRoutine(user, info.getRoutineId(), info.getHistorySeq());
4242
break;
4343
case SUB_ROUTINE:
44-
validateSubRoutine(user, info);
44+
validateSubRoutine(user, info.getRoutineId(), info.getHistorySeq());
4545
break;
4646
case CHANGED_ROUTINE:
47-
validateChangedRoutine(user, info);
47+
validateChangedRoutine(user, info.getRoutineId(), info.getHistorySeq());
4848
break;
4949
case CHANGED_SUB_ROUTINE:
50-
validateChangedSubRoutine(user, info);
50+
validateChangedSubRoutine(user, info.getRoutineId(), info.getHistorySeq());
5151
break;
5252
}
5353
}
@@ -67,19 +67,21 @@ public Routine validateRoutineOwnership(UUID routineId, User user, LocalDateTime
6767
return routine;
6868
}
6969

70-
private void validateRoutine(User user, RoutineCompletionInfo info) {
70+
public Routine validateRoutine(User user, UUID routineId, Long historySeq) {
7171
Routine routine = routineRepository
72-
.findByRoutinePk(new HistoryPk(info.getRoutineId(), info.getHistorySeq()))
72+
.findByRoutinePk(new HistoryPk(routineId, historySeq))
7373
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_ROUTINE));
7474

7575
if (!user.getUserPk().getId().equals(routine.getUserId())) {
7676
throw new CustomException(ErrorCode.ROUTINE_USER_NOT_MATCHED);
7777
}
78+
79+
return routine;
7880
}
7981

80-
private void validateSubRoutine(User user, RoutineCompletionInfo info) {
82+
private void validateSubRoutine(User user, UUID routineId, Long historySeq) {
8183
SubRoutine subRoutine = subRoutineRepository
82-
.findBySubRoutinePk(new HistoryPk(info.getRoutineId(), info.getHistorySeq()))
84+
.findBySubRoutinePk(new HistoryPk(routineId, historySeq))
8385
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_SUB_ROUTINE));
8486

8587
// 추후 성능 이슈가 발생할 수 있는 부분
@@ -90,19 +92,21 @@ private void validateSubRoutine(User user, RoutineCompletionInfo info) {
9092
}
9193
}
9294

93-
private void validateChangedRoutine(User user, RoutineCompletionInfo info) {
95+
public ChangedRoutine validateChangedRoutine(User user, UUID routineId, Long historySeq) {
9496
ChangedRoutine changedRoutine = changedRoutineRepository
95-
.findByChangedRoutinePk(new HistoryPk(info.getRoutineId(), info.getHistorySeq()))
97+
.findByChangedRoutinePk(new HistoryPk(routineId, historySeq))
9698
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_CHANGED_ROUTINE));
9799

98100
if (!user.getUserPk().getId().equals(changedRoutine.getUserId())) {
99101
throw new CustomException(ErrorCode.CHANGED_ROUTINE_USER_NOT_MATCHED);
100102
}
103+
104+
return changedRoutine;
101105
}
102106

103-
private void validateChangedSubRoutine(User user, RoutineCompletionInfo info) {
107+
private void validateChangedSubRoutine(User user, UUID routineId, Long historySeq) {
104108
ChangedSubRoutine changedSubRoutine = changedSubRoutineRepository
105-
.findByChangedSubRoutinePk(new HistoryPk(info.getRoutineId(), info.getHistorySeq()))
109+
.findByChangedSubRoutinePk(new HistoryPk(routineId, historySeq))
106110
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_CHANGED_SUB_ROUTINE));
107111

108112
List<ChangedRoutine> changedRoutines = changedRoutineRepository.findByChangedRoutinePk_Id(

0 commit comments

Comments
 (0)