Skip to content

Commit

Permalink
[#56] refactor: 요청 사항 command에 모아서 내려주도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
NaMinhyeok committed Feb 6, 2025
1 parent dd73ebf commit f8b84e1
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package org.nexters.jaknaesocore.domain.survey.dto;

public record SurveySubmissionCommand(Long optionId, String comment) {}
import lombok.Builder;

@Builder
public record SurveySubmissionCommand(
Long optionId, Long surveyId, Long memberId, String comment) {}
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,19 @@ private SurveyHistoryResponse getNextBundleHistory(Long currentBundleId) {
}

@Transactional
public void submitSurvey(
Long surveyId, Long memberId, SurveySubmissionCommand request, LocalDateTime submittedAt) {
public void submitSurvey(SurveySubmissionCommand command, LocalDateTime submittedAt) {
Survey survey =
surveyRepository.findById(surveyId).orElseThrow(() -> CustomException.SURVEY_NOT_FOUND);
SurveyOption surveyOption = survey.getOptionById(request.optionId());
surveyRepository
.findById(command.surveyId())
.orElseThrow(() -> CustomException.SURVEY_NOT_FOUND);
SurveyOption surveyOption = survey.getOptionById(command.optionId());
Member member =
memberRepository
.findByIdAndDeletedAtIsNull(memberId)
.findByIdAndDeletedAtIsNull(command.memberId())
.orElseThrow(() -> CustomException.MEMBER_NOT_FOUND);

SurveySubmission surveySubmission =
SurveySubmission.create(member, survey, surveyOption, request.comment(), submittedAt);
SurveySubmission.create(member, survey, surveyOption, command.comment(), submittedAt);

surveySubmissionRepository.save(surveySubmission);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,12 @@ void throwMemberNotFoundException() {
SurveyOption.builder().survey(balanceSurvey).scores(scores).content("질문 옵션 내용").build();
surveyOptionRepository.save(option);

SurveySubmissionCommand request =
new SurveySubmissionCommand(option.getId(), "나는 행복한게 좋으니까");
SurveySubmissionCommand command =
new SurveySubmissionCommand(option.getId(), balanceSurvey.getId(), 0L, "나는 행복한게 좋으니까");

// when
// then
thenThrownBy(
() ->
surveyService.submitSurvey(
balanceSurvey.getId(), 0L, request, LocalDateTime.now()))
thenThrownBy(() -> surveyService.submitSurvey(command, LocalDateTime.now()))
.isEqualTo(CustomException.MEMBER_NOT_FOUND);
}
}
Expand All @@ -134,12 +131,12 @@ class shouldSubmitted {
SurveyOption.builder().survey(balanceSurvey).scores(scores).content("질문 옵션 내용").build();
surveyOptionRepository.save(option);

SurveySubmissionCommand request =
new SurveySubmissionCommand(option.getId(), "나는 행복한게 좋으니까");
SurveySubmissionCommand command =
new SurveySubmissionCommand(
option.getId(), balanceSurvey.getId(), member.getId(), "나는 행복한게 좋으니까");

// when
surveyService.submitSurvey(
balanceSurvey.getId(), member.getId(), request, LocalDateTime.now());
surveyService.submitSurvey(command, LocalDateTime.now());
// then
List<SurveySubmission> submissions = surveySubmissionRepository.findAll();
then(submissions).hasSize(1);
Expand Down Expand Up @@ -170,13 +167,12 @@ void throwSurveyNotFoundException() {
SurveyOption.builder().survey(balanceSurvey).scores(scores).content("질문 옵션 내용").build();
surveyOptionRepository.save(option);

SurveySubmissionCommand request =
new SurveySubmissionCommand(option.getId(), "나는 행복한게 좋으니까");
SurveySubmissionCommand command =
new SurveySubmissionCommand(0L, member.getId(), option.getId(), "나는 행복한게 좋으니까");

// when
// then
thenThrownBy(
() -> surveyService.submitSurvey(0L, member.getId(), request, LocalDateTime.now()))
thenThrownBy(() -> surveyService.submitSurvey(command, LocalDateTime.now()))
.isEqualTo(CustomException.SURVEY_NOT_FOUND);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.nexters.jaknaesocore.common.support.response.ApiResponse;
import org.nexters.jaknaesocore.domain.survey.dto.SurveyHistoryResponse;
import org.nexters.jaknaesocore.domain.survey.dto.SurveyResponse;
import org.nexters.jaknaesocore.domain.survey.dto.SurveySubmissionCommand;
import org.nexters.jaknaesocore.domain.survey.service.SurveyService;
import org.nexters.jaknaesoserver.domain.auth.model.CustomUserDetails;
import org.nexters.jaknaesoserver.domain.survey.controller.dto.SurveySubmissionRequest;
Expand Down Expand Up @@ -39,8 +40,14 @@ public ApiResponse<?> submitSurvey(
@PathVariable Long surveyId,
@Valid @RequestBody SurveySubmissionRequest request) {
LocalDateTime submittedAt = LocalDateTime.now();
surveyService.submitSurvey(
surveyId, member.getMemberId(), request.toServiceRequest(), submittedAt);
SurveySubmissionCommand command =
SurveySubmissionCommand.builder()
.surveyId(surveyId)
.memberId(member.getMemberId())
.optionId(request.optionId())
.comment(request.comment())
.build();
surveyService.submitSurvey(command, submittedAt);
return ApiResponse.success();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package org.nexters.jaknaesoserver.domain.survey.controller.dto;

import jakarta.validation.constraints.NotNull;
import org.nexters.jaknaesocore.domain.survey.dto.SurveySubmissionCommand;

public record SurveySubmissionRequest(@NotNull Long optionId, String comment) {
public SurveySubmissionCommand toServiceRequest() {
return new SurveySubmissionCommand(optionId(), comment());
}
}
public record SurveySubmissionRequest(@NotNull Long optionId, String comment) {}
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ void getSurveyHistory() throws Exception {
SurveySubmissionRequest request = new SurveySubmissionRequest(1L, "나는 행복해요");
willDoNothing()
.given(surveyService)
.submitSurvey(
anyLong(), anyLong(), any(SurveySubmissionCommand.class), any(LocalDateTime.class));
.submitSurvey(any(SurveySubmissionCommand.class), any(LocalDateTime.class));

mockMvc
.perform(
Expand Down

0 comments on commit f8b84e1

Please sign in to comment.