diff --git a/src/main/java/com/onnoff/onnoff/apiPayload/code/status/ErrorStatus.java b/src/main/java/com/onnoff/onnoff/apiPayload/code/status/ErrorStatus.java index 463ef50..2b605bd 100644 --- a/src/main/java/com/onnoff/onnoff/apiPayload/code/status/ErrorStatus.java +++ b/src/main/java/com/onnoff/onnoff/apiPayload/code/status/ErrorStatus.java @@ -34,6 +34,7 @@ public enum ErrorStatus implements BaseErrorCode { // 회고 답변 관련 에러 ANSWER_NOT_FOUND(HttpStatus.BAD_REQUEST, "ANSWER4001", "해당하는 회고 답변이 없습니다."), + ANSWER_EXIST(HttpStatus.BAD_REQUEST, "ANSWER4002", "이미 해당 질문으로 작성된 회고 답변이 있습니다."), // 이모티콘 관련 에러 EMOTICON_NOT_FOUND(HttpStatus.BAD_REQUEST, "EMOTICON4001", "해당하는 이모티콘이 없습니다."), diff --git a/src/main/java/com/onnoff/onnoff/domain/off/memoir/entity/Memoir.java b/src/main/java/com/onnoff/onnoff/domain/off/memoir/entity/Memoir.java index bb45994..7c4badf 100644 --- a/src/main/java/com/onnoff/onnoff/domain/off/memoir/entity/Memoir.java +++ b/src/main/java/com/onnoff/onnoff/domain/off/memoir/entity/Memoir.java @@ -43,12 +43,4 @@ public void setEmoticon(Emoticon emoticon) { public void setIsBookmarked(Boolean isBookmarked) { this.isBookmarked = isBookmarked; } - - public void setUser(User user) { - this.user = user; - } - - public void setMemoirAnswerList(List memoirAnswerList) { - this.memoirAnswerList = memoirAnswerList; - } } diff --git a/src/main/java/com/onnoff/onnoff/domain/off/memoir/entity/MemoirAnswer.java b/src/main/java/com/onnoff/onnoff/domain/off/memoir/entity/MemoirAnswer.java index 6fc56a0..13e21b3 100644 --- a/src/main/java/com/onnoff/onnoff/domain/off/memoir/entity/MemoirAnswer.java +++ b/src/main/java/com/onnoff/onnoff/domain/off/memoir/entity/MemoirAnswer.java @@ -29,4 +29,9 @@ public class MemoirAnswer extends BaseEntity { public void setAnswer(String answer) { this.answer = answer; } + + public void setMemoir(Memoir memoir) { + this.memoir = memoir; + memoir.getMemoirAnswerList().add(this); + } } diff --git a/src/main/java/com/onnoff/onnoff/domain/off/memoir/service/MemoirServiceImpl.java b/src/main/java/com/onnoff/onnoff/domain/off/memoir/service/MemoirServiceImpl.java index e6d4181..c4dfb4c 100644 --- a/src/main/java/com/onnoff/onnoff/domain/off/memoir/service/MemoirServiceImpl.java +++ b/src/main/java/com/onnoff/onnoff/domain/off/memoir/service/MemoirServiceImpl.java @@ -37,6 +37,7 @@ public class MemoirServiceImpl implements MemoirService { @Transactional public Memoir writeMemoir(MemoirRequestDTO.MemoirWriteDTO request) { User user = UserContext.getUser(); + if (memoirRepository.findByUserAndDate(user, request.getDate()).isPresent()) { throw new MemoirHandler(ErrorStatus.MEMOIR_EXIST); } @@ -45,20 +46,29 @@ public Memoir writeMemoir(MemoirRequestDTO.MemoirWriteDTO request) { .date(request.getDate()) .emoticon(emoticonRepository.findById(request.getEmoticonId()).orElseThrow(() -> new MemoirHandler(ErrorStatus.EMOTICON_NOT_FOUND))) .isBookmarked(false) + .user(user) + .memoirAnswerList(new ArrayList<>()) .build(); - List newMemoirAnswerList = request.getMemoirAnswerList().stream() - .map(memoirAnswer -> MemoirAnswer.builder() - .answer(memoirAnswer.getAnswer().trim()) - .memoirQuestion(memoirQuestionRepository.findById(memoirAnswer.getQuestionId()).orElseThrow(() -> new MemoirHandler(ErrorStatus.QUESTION_NOT_FOUND))) - .memoir(newMemoir) - .build()) - .collect(Collectors.toList()); + memoirRepository.save(newMemoir); + + for (MemoirRequestDTO.MemoirAnswerDTO memoirAnswer : request.getMemoirAnswerList()) { + MemoirQuestion memoirQuestion = memoirQuestionRepository.findById(memoirAnswer.getQuestionId()).orElseThrow(() -> new MemoirHandler(ErrorStatus.QUESTION_NOT_FOUND)); - newMemoir.setUser(user); - newMemoir.setMemoirAnswerList(newMemoirAnswerList); + if (memoirAnswerRepository.findByMemoirAndMemoirQuestion(newMemoir, memoirQuestion).isPresent()) { + throw new MemoirHandler(ErrorStatus.ANSWER_EXIST); + } + + MemoirAnswer newMemoirAnswer = MemoirAnswer.builder() + .answer(memoirAnswer.getAnswer().trim()) + .memoirQuestion(memoirQuestion) + .build(); + + newMemoirAnswer.setMemoir(newMemoir); + memoirAnswerRepository.save(newMemoirAnswer); + } - return memoirRepository.save(newMemoir); + return newMemoir; } @Override