Skip to content

Commit

Permalink
refactor-be: 지원서 제출 시 응답 존재 유무 검증 추가 (#556)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: HyungHoKim00 <hkim1109@naver.com>
  • Loading branch information
github-actions[bot] and HyungHoKim00 authored Aug 21, 2024
1 parent d190970 commit 99e53e9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.cruru.answer.dto.AnswerResponse;
import com.cruru.applicant.domain.Applicant;
import com.cruru.applyform.controller.dto.AnswerCreateRequest;
import com.cruru.applyform.exception.badrequest.ReplyNotExistsException;
import com.cruru.question.domain.Question;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -20,7 +21,11 @@ public class AnswerService {

@Transactional
public void saveAnswerReplies(AnswerCreateRequest answerCreateRequest, Question question, Applicant applicant) {
for (String reply : answerCreateRequest.replies()) {
List<String> replies = answerCreateRequest.replies();
if (question.getRequired() && replies.isEmpty()) {
throw new ReplyNotExistsException();
}
for (String reply : replies) {
Answer answer = new Answer(reply, question, applicant);
answerRepository.save(answer);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.cruru.applyform.exception.badrequest;

import com.cruru.advice.badrequest.BadRequestException;

public class ReplyNotExistsException extends BadRequestException {

private static final String MESSAGE = "응답하지 않은 필수 질문이 존재합니다.";

public ReplyNotExistsException() {
super(MESSAGE);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cruru.answer.service;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertAll;

import com.cruru.answer.domain.Answer;
Expand All @@ -9,6 +10,7 @@
import com.cruru.applicant.domain.Applicant;
import com.cruru.applicant.domain.repository.ApplicantRepository;
import com.cruru.applyform.controller.dto.AnswerCreateRequest;
import com.cruru.applyform.exception.badrequest.ReplyNotExistsException;
import com.cruru.question.domain.Question;
import com.cruru.question.domain.repository.QuestionRepository;
import com.cruru.util.ServiceTest;
Expand Down Expand Up @@ -65,6 +67,19 @@ void savedAnswerReplies() {
);
}

@DisplayName("질문에 대한 지원자의 답변이 존재하지 않으면 예외가 발생한다.")
@Test
void savedAnswerReplies_replyNotExists() {
// given
List<String> replies = List.of();
Question question = questionRepository.save(QuestionFixture.required(null));
AnswerCreateRequest request = new AnswerCreateRequest(question.getId(), replies);

// when&then
assertThatThrownBy(() -> answerService.saveAnswerReplies(request, question, applicant))
.isInstanceOf(ReplyNotExistsException.class);
}

@DisplayName("지원자의 응답을 조회한다.")
@Test
void findAllByApplicant() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@ public static List<Question> nonChoiceType(ApplyForm applyForm) {
longAnswerType(applyForm)
);
}

public static Question required(ApplyForm applyForm) {
return new Question(SHORT_ANSWER, "주관식 단답형", 1, true, applyForm);
}
}

0 comments on commit 99e53e9

Please sign in to comment.