Skip to content

Commit

Permalink
fix: 지원 시작 이후에는 모집 공고의 질문을 수정할 수 없도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
HyungHoKim00 committed Aug 23, 2024
1 parent 6dcc75b commit 5d817cb
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Objects;
import lombok.AccessLevel;
Expand Down Expand Up @@ -89,6 +90,10 @@ public ApplyForm(
this.dashboard = dashboard;
}

public boolean hasStarted() {
return !startDate.toLocalDate().isAfter(LocalDate.now());
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.cruru.question.exception;

import com.cruru.advice.badrequest.BadRequestException;

public class QuestionUnmodifiableException extends BadRequestException {

private static final String MESSAGE = "진행중인 모집 공고는 질문을 수정할 수 없습니다.";

public QuestionUnmodifiableException() {
super(MESSAGE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.cruru.applyform.service.ApplyFormService;
import com.cruru.question.controller.dto.QuestionCreateRequest;
import com.cruru.question.controller.dto.QuestionUpdateRequests;
import com.cruru.question.exception.QuestionUnmodifiableException;
import com.cruru.question.service.QuestionService;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -21,9 +22,16 @@ public class QuestionFacade {
@Transactional
public void update(QuestionUpdateRequests request, long applyFormId) {
ApplyForm applyForm = applyFormService.findById(applyFormId);
validateRecruitmentStarted(applyForm);
questionService.deleteAllByApplyForm(applyForm);
List<QuestionCreateRequest> newQuestions = request.questions();

newQuestions.forEach(question -> questionService.create(question, applyForm));
}

private void validateRecruitmentStarted(ApplyForm applyForm) {
if (applyForm.hasStarted()) {
throw new QuestionUnmodifiableException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

import com.cruru.applyform.domain.ApplyForm;
import com.cruru.applyform.domain.repository.ApplyFormRepository;
import com.cruru.dashboard.domain.Dashboard;
import com.cruru.dashboard.domain.repository.DashboardRepository;
import com.cruru.question.controller.dto.QuestionCreateRequest;
import com.cruru.question.controller.dto.QuestionUpdateRequests;
import com.cruru.question.domain.QuestionType;
import com.cruru.question.domain.repository.QuestionRepository;
import com.cruru.util.ControllerTest;
import com.cruru.util.fixture.ApplyFormFixture;
import com.cruru.util.fixture.DashboardFixture;
import com.cruru.util.fixture.QuestionFixture;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
Expand All @@ -22,9 +19,6 @@
@DisplayName("질문 컨트롤러 테스트")
class QuestionControllerTest extends ControllerTest {

@Autowired
private DashboardRepository dashboardRepository;

@Autowired
private QuestionRepository questionRepository;

Expand All @@ -35,8 +29,7 @@ class QuestionControllerTest extends ControllerTest {
@Test
void update() {
// given
Dashboard dashboard = dashboardRepository.save(DashboardFixture.backend(defaultClub));
ApplyForm applyForm = applyFormRepository.save(ApplyFormFixture.backend(dashboard));
ApplyForm applyForm = applyFormRepository.save(ApplyFormFixture.notStarted());
questionRepository.save(QuestionFixture.shortAnswerType(applyForm));
QuestionUpdateRequests questionUpdateRequests = new QuestionUpdateRequests(List.of(
new QuestionCreateRequest(QuestionType.LONG_ANSWER.name(), "new", List.of(), 0, true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cruru.question.service.facade;

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.applyform.domain.ApplyForm;
Expand All @@ -12,6 +13,7 @@
import com.cruru.question.controller.dto.QuestionUpdateRequests;
import com.cruru.question.domain.Question;
import com.cruru.question.domain.repository.QuestionRepository;
import com.cruru.question.exception.QuestionUnmodifiableException;
import com.cruru.util.ServiceTest;
import com.cruru.util.fixture.ApplyFormFixture;
import com.cruru.util.fixture.ChoiceFixture;
Expand Down Expand Up @@ -40,7 +42,7 @@ class QuestionFacadeTest extends ServiceTest {
@Test
void update() {
// given
ApplyForm applyForm = applyFormRepository.save(ApplyFormFixture.backend());
ApplyForm applyForm = applyFormRepository.save(ApplyFormFixture.notStarted());
Question question = questionRepository.save(QuestionFixture.multipleChoiceType(applyForm));
choiceRepository.save(ChoiceFixture.first(question));
choiceRepository.save(ChoiceFixture.second(question));
Expand Down Expand Up @@ -80,4 +82,29 @@ void update() {
() -> assertThat(actualChoice.getSequence()).isEqualTo(newChoice.getSequence())
);
}

@DisplayName("모집 공고가 시작된 이후이면 질문을 수정할 수 없다..")
@Test
void update_ApplyFormInProgress() {
// given
ApplyForm applyForm = applyFormRepository.save(ApplyFormFixture.backend());
Question question = questionRepository.save(QuestionFixture.multipleChoiceType(applyForm));
choiceRepository.save(ChoiceFixture.first(question));
choiceRepository.save(ChoiceFixture.second(question));

Question newQuestion = QuestionFixture.shortAnswerType(applyForm);

QuestionUpdateRequests questionUpdateRequest = new QuestionUpdateRequests(List.of(
new QuestionCreateRequest(
newQuestion.getQuestionType().name(),
newQuestion.getContent(),
List.of(),
newQuestion.getSequence(),
newQuestion.isRequired()
)));

// when&then
assertThatThrownBy(() -> questionFacade.update(questionUpdateRequest, applyForm.getId()))
.isInstanceOf(QuestionUnmodifiableException.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static ApplyForm backend() {
"크루루 백엔드 모집 공고",
"# 모집공고 설명1 ## 이렇게 지원하세요",
"www.cruru.kr/post/" + 1,
LocalDateTime.of(2099, 11, 30, 23, 59, 59),
LocalDateTime.of(2000, 11, 30, 23, 59, 59),
LocalDateTime.of(2099, 12, 31, 23, 59, 59),
null
);
Expand All @@ -38,4 +38,15 @@ public static ApplyForm frontend(Dashboard frontendDashboard) {
frontendDashboard
);
}

public static ApplyForm notStarted() {
return new ApplyForm(
"크루루 프론트엔드 모집 공고",
"# 모집공고 설명2 ## 이렇게 지원하세요",
"www.cruru.kr/post/" + 1,
LocalDateTime.of(2099, 12, 11, 23, 59, 59),
LocalDateTime.of(2099, 12, 21, 23, 59, 59),
null
);
}
}

0 comments on commit 5d817cb

Please sign in to comment.