Skip to content

Commit

Permalink
fix: 동일한 질문에 중복 답변 불가로 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
kimjm9841 committed Feb 13, 2024
1 parent 210fdd6 commit 587f904
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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", "해당하는 이모티콘이 없습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MemoirAnswer> memoirAnswerList) {
this.memoirAnswerList = memoirAnswerList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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<MemoirAnswer> 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
Expand Down

0 comments on commit 587f904

Please sign in to comment.