Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public FormListResponseDto getParticipatedFormList(SearchDto param, LoginUserDto

public ParticipateSubmissionDto getSelfResponse(Long formId, LoginUserDto loginUser) {
User user = userRepository.getReferenceById(loginUser.id());
Form form = formService.getForm(formId);
Form form = formService.getTempForm(formId);

Submission submission = submissionRepository.findSubmissionByFormIDAndUserId(formId, loginUser.id())
.orElseThrow(() -> new DataNotFoundException(ErrorCode.NO_SUBMISSION_PARTICIPATED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import com.formssafe.domain.form.dto.FormRequest;
import com.formssafe.domain.form.dto.FormResponse.FormIdDto;
import com.formssafe.domain.form.dto.FormResponse.FormWithQuestionDto;
import com.formssafe.domain.form.service.FormCreateService;
import com.formssafe.domain.form.service.FormService;
import com.formssafe.domain.form.service.TempFormUpdateService;
import com.formssafe.domain.user.dto.UserRequest.LoginUserDto;
import com.formssafe.global.error.response.ErrorResponse;
import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -38,8 +36,6 @@
@RequiredArgsConstructor
public class FormController {
private final FormService formService;
private final FormCreateService formCreateService;
private final TempFormUpdateService tempFormUpdateService;

@Operation(summary = "설문 상세 조회", description = "해당 id의 설문을 상세 조회한다.")
@ApiResponse(responseCode = "400", description = "formId가 존재하지 않음",
Expand All @@ -54,7 +50,7 @@ public class FormController {
@ResponseStatus(HttpStatus.OK)
FormWithQuestionDto getForm(@PathVariable Long id,
@AuthenticationPrincipal LoginUserDto loginUser) {
return formService.getForm(id, loginUser);
return formService.getTempForm(id, loginUser);
}

@Operation(summary = "설문 등록", description = "새로운 설문을 등록한다.")
Expand All @@ -66,7 +62,7 @@ FormWithQuestionDto getForm(@PathVariable Long id,
@ResponseStatus(HttpStatus.OK)
FormIdDto createForm(@Valid @RequestBody FormRequest.FormCreateDto request,
@AuthenticationPrincipal LoginUserDto loginUser) {
return formCreateService.execute(request, loginUser);
return formService.createForm(request, loginUser);
}

@Operation(summary = "설문 수동 마감", description = "해당 id의 설문을 수동으로 마감한다.")
Expand Down Expand Up @@ -99,7 +95,7 @@ void closeForm(@PathVariable Long id,
void updateForm(@PathVariable Long id,
@Valid @RequestBody FormRequest.FormUpdateDto request,
@AuthenticationPrincipal LoginUserDto loginUser) {
tempFormUpdateService.execute(id, request, loginUser);
formService.updateTempForm(id, request, loginUser);
}

@Operation(summary = "설문 삭제", description = "해당 id의 설문을 삭제한다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,4 @@ public List<Content> getContentList(Long formId) {
.sorted(Comparator.comparingInt(Content::getPosition))
.toList();
}

public List<Content> getReward(Long formId) {
return formRepository.findContentsById(formId).stream()
.map(contentConverter::convert)
.sorted(Comparator.comparingInt(Content::getPosition))
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.formssafe.domain.content.dto.ContentResponseDto;
import com.formssafe.domain.content.entity.Content;
import com.formssafe.domain.form.dto.FormResponse.FormWithQuestionDto;
import com.formssafe.domain.form.entity.Form;
import com.formssafe.domain.form.dto.FormResponse.FormIdDto;
import com.formssafe.domain.reward.dto.RewardResponse.RewardDto;
import com.formssafe.domain.reward.entity.Reward;
Expand Down Expand Up @@ -49,6 +51,13 @@ public List<UserListDto> toRewardRecipientsListDto(List<RewardRecipient> rewardR
.toList();
}

public FormWithQuestionDto toFormWithQuestionDto(Form form, List<Content> contents) {
return FormWithQuestionDto.from(form,
toContentResponseDto(contents),
toTagListDto(form.getFormTagList()),
toRewardDto(form.getReward()));
}

public FormIdDto toFormIdDto(Long formId) {
return new FormIdDto(formId);
}
Expand Down
37 changes: 27 additions & 10 deletions src/main/java/com/formssafe/domain/form/service/FormService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.formssafe.domain.content.entity.Content;
import com.formssafe.domain.content.question.entity.DescriptiveQuestion;
import com.formssafe.domain.content.question.entity.ObjectiveQuestion;
import com.formssafe.domain.form.dto.FormRequest.FormCreateDto;
import com.formssafe.domain.form.dto.FormRequest.FormUpdateDto;
import com.formssafe.domain.form.dto.FormResponse.FormIdDto;
import com.formssafe.domain.form.dto.FormResponse.FormResultDto;
import com.formssafe.domain.form.dto.FormResponse.FormWithQuestionDto;
import com.formssafe.domain.form.entity.Form;
Expand All @@ -11,6 +14,7 @@
import com.formssafe.domain.reward.entity.RewardRecipient;
import com.formssafe.domain.reward.service.RewardRecipientsSelectService;
import com.formssafe.domain.tag.entity.FormTag;
import com.formssafe.domain.tag.service.TagService;
import com.formssafe.domain.user.dto.UserRequest.LoginUserDto;
import com.formssafe.domain.user.entity.User;
import java.util.List;
Expand All @@ -28,22 +32,19 @@ public class FormService {
private final FormValidateService formValidateService;
private final RewardRecipientsSelectService rewardRecipientsSelectService;
private final FormFinishService formFinishService;
private final FormRepository formRepository;
private final FormCreateService formCreateService;
private final TempFormUpdateService tempFormUpdateService;
private final FormResponseMapper formResponseMapper;
private final FormRepository formRepository;
private final TagService tagService;

public FormWithQuestionDto getForm(Long formId, LoginUserDto loginUser) {
public FormWithQuestionDto getTempForm(Long formId, LoginUserDto loginUser) {
Form form = formReadService.findFormWithUserAndTag(formId);
formValidateService.validAuthor(form, loginUser.id());
formValidateService.validTempForm(form);

List<Content> contents = formReadService.getContentList(formId);
List<FormTag> formTags = form.getFormTagList();
Reward reward = form.getReward();

return FormWithQuestionDto.from(form,
formResponseMapper.toContentResponseDto(contents),
formResponseMapper.toTagListDto(formTags),
formResponseMapper.toRewardDto(reward));
return formResponseMapper.toFormWithQuestionDto(form, contents);
}

public FormResultDto getFormResult(Long formId, LoginUserDto loginUser) {
Expand All @@ -66,7 +67,7 @@ public FormResultDto getFormResult(Long formId, LoginUserDto loginUser) {
formResponseMapper.toRewardRecipientsListDto(rewardRecipients));
}

public Form getForm(Long id) {
public Form getTempForm(Long id) {
return formReadService.findForm(id);
}

Expand All @@ -85,6 +86,21 @@ public int getRequiredQuestionCnt(Form form) {
return requiredQuestionCnt;
}

@Transactional
public FormIdDto createForm(FormCreateDto request, LoginUserDto loginUser) {
log.debug("Create Form: contents: {}, loginUser: {}", request, loginUser.id());

return formCreateService.execute(request, loginUser);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

execute에서 리턴을 받은 후 formResponseMapper로 변환하는 것이 어떠신가요?

}

@Transactional
public void updateTempForm(Long formId, FormUpdateDto request, LoginUserDto loginUser) {
log.debug("Update Temp Form: id: {}, contents: {}, loginUser: {}", formId, request, loginUser.id());

tempFormUpdateService.execute(formId, request, loginUser);
}

@Transactional
public void deleteFormByUser(User user) {
formRepository.deleteFormByUserId(user.getId());
}
Expand All @@ -97,6 +113,7 @@ public void deleteForm(Long id, LoginUserDto loginUser) {
formValidateService.validAuthor(form, loginUser.id());

form.delete();
tagService.decreaseTagCount(form);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private void updateToTempForm(FormUpdateDto request, Form form, LocalDateTime no

private void clearFormRelatedData(Form form) {
contentService.deleteContents(form);
tagService.decreaseCount(form);
tagService.deleteTags(form);
if (form.getReward() != null) {
rewardService.deleteReward(form);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class ResultService {
public ResultResponseDto getTotalResult(LoginUserDto loginUser, Long formId) {
User user = userService.getUserById(loginUser.id());

Form form = formService.getForm(formId);
Form form = formService.getTempForm(formId);

if (user.getId() != form.getUser().getId()) {
throw new BadRequestException(ErrorCode.INVALID_AUTHOR_TO_CHECK_RESULT, "자신이 작성한 설문만 설문 결과를 확인할 수 있습니다.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class SubmissionService {
public void create(long formId, SubmissionCreateDto request, LoginUserDto loginUser) {
User user = userService.getUserById(loginUser.id());

Form form = formService.getForm(formId);
Form form = formService.getTempForm(formId);

if (getSubmissionByUserAndForm(user, form) != null) {
throw new BadRequestException(ErrorCode.ONLY_ONE_SUBMISSION_ALLOWED,
Expand All @@ -79,7 +79,7 @@ public void create(long formId, SubmissionCreateDto request, LoginUserDto loginU
public void modify(long formId, SubmissionCreateDto request, LoginUserDto loginUser) {
User user = userService.getUserById(loginUser.id());

Form form = formService.getForm(formId);
Form form = formService.getTempForm(formId);

Submission submission = getSubmissionByUserAndForm(user, form);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,13 @@ public void createOrUpdateTags(List<String> tags, Form form) {
}

@Transactional
public void decreaseCount(Form form) {
public void deleteTags(Form form) {
tagRepository.decreaseCountByForm(form);
formTagRepository.deleteAllByForm(form);
}

@Transactional
public void decreaseTagCount(Form form) {
tagRepository.decreaseCountByForm(form);
}
}
17 changes: 17 additions & 0 deletions src/test/java/com/formssafe/config/ControllerTestConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.formssafe.config;

import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;


@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@Transactional
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@AutoConfigureMockMvc
@SpringBootTest
public class ControllerTestConfig {
}
7 changes: 5 additions & 2 deletions src/test/java/com/formssafe/config/IntegrationTestConfig.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.formssafe.config;

import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;

@SpringBootTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@DisplayNameGeneration(ReplaceUnderscores.class)
@Transactional
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@SpringBootTest
public class IntegrationTestConfig {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,62 @@
import com.formssafe.config.IntegrationTestConfig;
import com.formssafe.domain.form.entity.Form;
import com.formssafe.domain.form.entity.FormStatus;
import com.formssafe.domain.form.repository.FormRepository;
import com.formssafe.domain.user.entity.User;
import com.formssafe.domain.user.repository.UserRepository;
import com.formssafe.util.EntityManagerUtil;
import com.formssafe.util.Fixture;
import jakarta.persistence.EntityManager;
import java.time.LocalDateTime;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

@SuppressWarnings("NonAsciiCharacters")
@DisplayName("[설문 배치 테스트]")
class FormBatchServiceTest extends IntegrationTestConfig {
@Autowired
private FormBatchService formBatchService;
@Autowired
private FormRepository formRepository;
@Autowired
private UserRepository userRepository;
private final FormBatchService formBatchService;
private final EntityManager em;

private User testUser;

@Autowired
public FormBatchServiceTest(FormBatchService formBatchService,
EntityManager em) {
this.formBatchService = formBatchService;
this.em = em;
}

@BeforeEach
void setUp() {
testUser = userRepository.findById(1L).orElseThrow(IllegalStateException::new);
testUser = em.find(User.class, 1L);
}

@Nested
class 설문_마감 {
class 설문_자동_마감 {

@Test
void 마감해야하는_설문을_가져와_마감한다() {
void 마감해야_하는_설문을_가져와_마감한다() {
//given
LocalDateTime endDate = LocalDateTime.of(2024, 3, 2, 0, 0, 0);
Form form1 = Fixture.createFormWithEndDate(testUser, "test1", "detail1", endDate, FormStatus.PROGRESS);
Form form2 = Fixture.createFormWithEndDate(testUser, "test2", "detail2", endDate, FormStatus.PROGRESS);
formRepository.saveAll(List.of(form1, form2));
em.persist(form1);
em.persist(form2);
EntityManagerUtil.flushAndClear(em);
//when
formBatchService.endForm(endDate);
//then
assertThat(formRepository.findAll())
.extracting("status")
.containsExactly(FormStatus.DONE, FormStatus.DONE);
em.createQuery("select f from Form f", Form.class)
.getResultList()
.forEach(form -> {
assertThat(form.getStatus()).isEqualTo(FormStatus.DONE);
});
}

@Test
void 진행중이_아닌_설문은_스킵하고_진행중인_설문만_마감한다() {
void 진행_중이_아닌_설문은_스킵하고_진행_중인_설문만_마감한다() {
//given
LocalDateTime endDate = LocalDateTime.of(2024, 3, 2, 0, 0, 0);
Form form1 = Fixture.createFormWithEndDate(testUser, "test1", "detail1", endDate, FormStatus.PROGRESS);
Expand All @@ -59,11 +69,19 @@ class 설문_마감 {
Form form4 = Fixture.createFormWithEndDate(testUser, "test4", "detail2", endDate, FormStatus.NOT_STARTED);
Form form5 = Fixture.createFormWithEndDate(testUser, "test5", "detail2", endDate, FormStatus.PROGRESS);
Form form6 = Fixture.createFormWithEndDate(testUser, "test6", "detail2", endDate, FormStatus.REWARDED);
formRepository.saveAll(List.of(form1, form2, form3, form4, form5, form6));
em.persist(form1);
em.persist(form2);
em.persist(form3);
em.persist(form4);
em.persist(form5);
em.persist(form6);
EntityManagerUtil.flushAndClear(em);
//when
formBatchService.endForm(endDate);
//then
assertThat(formRepository.findAll())
List<Form> resultFormList = em.createQuery("select f from Form f", Form.class)
.getResultList();
assertThat(resultFormList)
.extracting("status")
.containsExactly(FormStatus.DONE, FormStatus.DONE, FormStatus.DONE, FormStatus.NOT_STARTED,
FormStatus.DONE, FormStatus.REWARDED);
Expand All @@ -76,11 +94,15 @@ class 설문_마감 {
Form form1 = Fixture.createFormWithEndDate(testUser, "test1", "detail1", endDate, FormStatus.PROGRESS);
Form form2 = Fixture.createFormWithEndDate(testUser, "test2", "detail2", endDate, FormStatus.PROGRESS);
form2.delete();
formRepository.saveAll(List.of(form1, form2));
em.persist(form1);
em.persist(form2);
EntityManagerUtil.flushAndClear(em);
//when
formBatchService.endForm(endDate);
//then
assertThat(formRepository.findAll())
List<Form> resultFormList = em.createQuery("select f from Form f", Form.class)
.getResultList();
assertThat(resultFormList)
.extracting("status")
.containsExactly(FormStatus.DONE, FormStatus.PROGRESS);
}
Expand Down
Loading