diff --git a/src/main/java/com/ogjg/daitgym/common/exception/ErrorCode.java b/src/main/java/com/ogjg/daitgym/common/exception/ErrorCode.java index 49c5706d..3b9e8938 100644 --- a/src/main/java/com/ogjg/daitgym/common/exception/ErrorCode.java +++ b/src/main/java/com/ogjg/daitgym/common/exception/ErrorCode.java @@ -34,6 +34,8 @@ public enum ErrorCode implements ErrorType { NOT_FOUND_USER(HttpStatus.NOT_FOUND, "404", "유저를 찾을 수 없습니다"), //BAD_REQUEST + NOT_COMPLETED_EXERCISE_JOURNAL(HttpStatus.BAD_REQUEST,"400","운동일지가 완료되지 않았습니다"), + NOT_COMPLETED_EXERCISE_HISTORY(HttpStatus.BAD_REQUEST,"400","완료되지 않은 운동기록이 존재합니다"), ALREADY_FOLLOW_USER(HttpStatus.BAD_REQUEST, "400", "이미 팔로우한 유저입니다"), ALREADY_EXIST_FEED_COLLECTION(HttpStatus.BAD_REQUEST, "400", "이미 스크랩한 피드 운동일지 게시물입니다"), ALREADY_SHARED_JOURNAL(HttpStatus.BAD_REQUEST, "400", "이미 공유된 운동일지입니다"), diff --git a/src/main/java/com/ogjg/daitgym/common/exception/journal/NotCompletedExerciseHistory.java b/src/main/java/com/ogjg/daitgym/common/exception/journal/NotCompletedExerciseHistory.java new file mode 100644 index 00000000..640412dc --- /dev/null +++ b/src/main/java/com/ogjg/daitgym/common/exception/journal/NotCompletedExerciseHistory.java @@ -0,0 +1,20 @@ +package com.ogjg.daitgym.common.exception.journal; + +import com.ogjg.daitgym.common.exception.CustomException; +import com.ogjg.daitgym.common.exception.ErrorCode; +import com.ogjg.daitgym.common.exception.ErrorData; + +public class NotCompletedExerciseHistory extends CustomException { + + public NotCompletedExerciseHistory() { + super(ErrorCode.NOT_COMPLETED_EXERCISE_HISTORY); + } + + public NotCompletedExerciseHistory(String message) { + super(ErrorCode.NOT_COMPLETED_EXERCISE_HISTORY, message); + } + + public NotCompletedExerciseHistory(ErrorData errorData) { + super(ErrorCode.NOT_COMPLETED_EXERCISE_HISTORY, errorData); + } +} diff --git a/src/main/java/com/ogjg/daitgym/common/exception/journal/NotCompletedExerciseJournal.java b/src/main/java/com/ogjg/daitgym/common/exception/journal/NotCompletedExerciseJournal.java new file mode 100644 index 00000000..388eaf61 --- /dev/null +++ b/src/main/java/com/ogjg/daitgym/common/exception/journal/NotCompletedExerciseJournal.java @@ -0,0 +1,20 @@ +package com.ogjg.daitgym.common.exception.journal; + +import com.ogjg.daitgym.common.exception.CustomException; +import com.ogjg.daitgym.common.exception.ErrorCode; +import com.ogjg.daitgym.common.exception.ErrorData; + +public class NotCompletedExerciseJournal extends CustomException { + + public NotCompletedExerciseJournal() { + super(ErrorCode.NOT_COMPLETED_EXERCISE_JOURNAL); + } + + public NotCompletedExerciseJournal(String message) { + super(ErrorCode.NOT_COMPLETED_EXERCISE_JOURNAL, message); + } + + public NotCompletedExerciseJournal(ErrorData errorData) { + super(ErrorCode.NOT_COMPLETED_EXERCISE_JOURNAL, errorData); + } +} diff --git a/src/main/java/com/ogjg/daitgym/journal/service/ExerciseJournalHelper.java b/src/main/java/com/ogjg/daitgym/journal/service/ExerciseJournalHelper.java index 27d0de29..ca5b81a0 100644 --- a/src/main/java/com/ogjg/daitgym/journal/service/ExerciseJournalHelper.java +++ b/src/main/java/com/ogjg/daitgym/journal/service/ExerciseJournalHelper.java @@ -2,10 +2,7 @@ import com.ogjg.daitgym.comment.feedExerciseJournal.exception.NotFoundExerciseJournal; import com.ogjg.daitgym.comment.feedExerciseJournal.exception.NotFoundUser; -import com.ogjg.daitgym.common.exception.journal.NotFoundExerciseHistory; -import com.ogjg.daitgym.common.exception.journal.NotFoundExerciseList; -import com.ogjg.daitgym.common.exception.journal.NotFoundJournal; -import com.ogjg.daitgym.common.exception.journal.UserNotAuthorizedForJournal; +import com.ogjg.daitgym.common.exception.journal.*; import com.ogjg.daitgym.domain.User; import com.ogjg.daitgym.domain.exercise.Exercise; import com.ogjg.daitgym.domain.journal.ExerciseHistory; @@ -120,6 +117,26 @@ public boolean checkForExistJournalSameDate( .isPresent(); } + /** + * 운동일지의 운동기록들이 모두 완료된 상태인지 확인 + * + * @param exerciseJournal 확인할 운동일지 + */ + public void checkAllExerciseHistoriesCompleted( + ExerciseJournal exerciseJournal + ) { + List exerciseLists = findExerciseListsByJournal(exerciseJournal); + exerciseLists.forEach( + exerciseList -> { + findExerciseHistoriesByExerciseList(exerciseList).forEach( + exerciseHistory -> { + if (!exerciseHistory.isCompleted()) throw new NotCompletedExerciseHistory(); + } + ); + } + ); + } + /** * todo * 이메일로 유저 검색 diff --git a/src/main/java/com/ogjg/daitgym/journal/service/ExerciseJournalService.java b/src/main/java/com/ogjg/daitgym/journal/service/ExerciseJournalService.java index 96d3cb12..671bf658 100644 --- a/src/main/java/com/ogjg/daitgym/journal/service/ExerciseJournalService.java +++ b/src/main/java/com/ogjg/daitgym/journal/service/ExerciseJournalService.java @@ -2,6 +2,7 @@ import com.ogjg.daitgym.common.exception.feed.AlreadyExistFeedJournal; import com.ogjg.daitgym.common.exception.journal.AlreadyExistExerciseJournal; +import com.ogjg.daitgym.common.exception.journal.NotCompletedExerciseJournal; import com.ogjg.daitgym.domain.User; import com.ogjg.daitgym.domain.feed.FeedExerciseJournal; import com.ogjg.daitgym.domain.journal.ExerciseHistory; @@ -18,7 +19,6 @@ import com.ogjg.daitgym.journal.repository.exercisehistory.ExerciseHistoryRepository; import com.ogjg.daitgym.journal.repository.exerciselist.ExerciseListRepository; import com.ogjg.daitgym.journal.repository.journal.ExerciseJournalRepository; -import com.ogjg.daitgym.routine.repository.RoutineRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -83,6 +83,7 @@ public void exerciseJournalComplete( ) { exerciseJournalHelper.isAuthorizedForJournal(email, journalId); ExerciseJournal exerciseJournal = exerciseJournalHelper.findExerciseJournalById(journalId); + exerciseJournalHelper.checkAllExerciseHistoriesCompleted(exerciseJournal); exerciseJournal.journalComplete(exerciseJournalCompleteRequest); } @@ -99,9 +100,11 @@ public void exerciseJournalShare( ) { ExerciseJournal exerciseJournal = exerciseJournalHelper.isAuthorizedForJournal(email, journalId); - if (feedJournalHelper.checkExistFeedExerciseJournalByExerciseJournal(exerciseJournal)) { + if (!exerciseJournal.isCompleted()) + throw new NotCompletedExerciseJournal(); + + if (feedJournalHelper.checkExistFeedExerciseJournalByExerciseJournal(exerciseJournal)) throw new AlreadyExistFeedJournal(); - } exerciseJournal.journalShareToFeed(exerciseJournalShareRequest); @@ -211,10 +214,10 @@ public void replicateExerciseJournalFromRoutine( ) { replicationRoutineRequest.getRoutines() .forEach(replicationRoutineRequestDto -> { - ExerciseJournal replicatedUserJournal = createJournal(email, replicationRoutineRequestDto.getJournalDate()); - exerciseJournalHelper.replicateExerciseListAndHistoryByRoutine(replicatedUserJournal, replicationRoutineRequestDto); - } - ); + ExerciseJournal replicatedUserJournal = createJournal(email, replicationRoutineRequestDto.getJournalDate()); + exerciseJournalHelper.replicateExerciseListAndHistoryByRoutine(replicatedUserJournal, replicationRoutineRequestDto); + } + ); } /**