-
Notifications
You must be signed in to change notification settings - Fork 4
[refactor] 설문조사 enum형식으로 변경 -> 우선 에너지 레벨만 추가 #101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,24 @@ | ||
| package com.arom.with_travel.domain.survey.dto.request; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.NotEmpty; | ||
| import com.arom.with_travel.domain.survey.enums.EnergyLevel; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Getter | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor // <— 모든 필드를 초기화하는 생성자 추가 | ||
| @AllArgsConstructor | ||
| @Builder | ||
| public class SurveyRequestDto { | ||
|
|
||
| @NotEmpty(message = "설문 답변은 최소 1개 이상이어야 합니다.") | ||
| private List<String> answers; | ||
| @NotNull(message = "energyLevel은 필수입니다.") | ||
| private EnergyLevel energyLevel; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 한가지 질문에 대해 여러 답변을 받는 경우엔 어떻게 되나요?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 제가 착각을 했는데 현재 DTO는 단일 선택만 요구하고 있어서 프론트에서 배열로 요청을 보내면(여러 답변을 받으면) 400 에러(Bad Request)가 납니다. |
||
| // @NotNull TravelGoal travelGoal, | ||
| // @NotNull TravelPace travelPace, | ||
| // @NotNull CommStyle commStyle, | ||
| // @NotNull Personality personality, | ||
| // @NotNull CompanionStyle companionStyle, | ||
| // @NotNull SpendPattern spendPattern | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package com.arom.with_travel.domain.survey.enums; | ||
|
|
||
| import com.arom.with_travel.global.exception.BaseException; | ||
| import com.arom.with_travel.global.exception.error.ErrorCode; | ||
| import com.fasterxml.jackson.annotation.JsonCreator; | ||
| import com.fasterxml.jackson.annotation.JsonValue; | ||
| import lombok.AllArgsConstructor; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| @AllArgsConstructor | ||
| public enum EnergyLevel implements SurveyEnum { | ||
|
|
||
| MORNING_PERSON("MORNING_PERSON", "#아침형인간"), | ||
| NIGHT_OWL("NIGHT_OWL", "#밤올빼미"), | ||
| ENERGIZER("ENERGIZER", "#에너자이저"), | ||
| HEALING_MODE("HEALING_MODE", "#힐링모드"); | ||
|
|
||
| private final String code; | ||
| private final String label; | ||
|
|
||
| @Override public String getCode() { return code; } | ||
| @Override public String getLabel() { return label; } | ||
|
|
||
| // 응답으로는 code를 내보내기 | ||
| @JsonValue | ||
| public String json() { return code; } | ||
|
|
||
| // 요청으로는 name/code 둘 다 허용 | ||
| @JsonCreator | ||
| public static EnergyLevel from(String value) { | ||
| return Arrays.stream(values()) | ||
| .filter(v -> v.name().equalsIgnoreCase(value) || v.code.equalsIgnoreCase(value)) | ||
| .findFirst() | ||
| .orElseThrow(() -> BaseException.from(ErrorCode.INVALID_SURVEY_ENERGYLEVEL)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.arom.with_travel.domain.survey.enums; | ||
|
|
||
| public interface SurveyEnum { | ||
|
|
||
| String getCode(); | ||
| String getLabel(); | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,13 +9,10 @@ | |||||||||||||||
| import com.arom.with_travel.domain.survey.repository.SurveyRepository; | ||||||||||||||||
| import com.arom.with_travel.global.exception.BaseException; | ||||||||||||||||
| import com.arom.with_travel.global.exception.error.ErrorCode; | ||||||||||||||||
| import com.arom.with_travel.global.security.domain.AuthenticatedMember; | ||||||||||||||||
| import lombok.RequiredArgsConstructor; | ||||||||||||||||
| import org.springframework.stereotype.Service; | ||||||||||||||||
| import org.springframework.transaction.annotation.Transactional; | ||||||||||||||||
|
|
||||||||||||||||
| import java.util.List; | ||||||||||||||||
|
|
||||||||||||||||
| @Service | ||||||||||||||||
| @RequiredArgsConstructor | ||||||||||||||||
| public class SurveyService { | ||||||||||||||||
|
|
@@ -24,28 +21,45 @@ public class SurveyService { | |||||||||||||||
| private final MemberRepository memberRepository; | ||||||||||||||||
|
|
||||||||||||||||
| @Transactional | ||||||||||||||||
| public void createSurvey(String email, SurveyRequestDto dto) { | ||||||||||||||||
| public void saveSurvey(String email, SurveyRequestDto dto) { | ||||||||||||||||
| Member member = memberRepository.findByEmail(email) | ||||||||||||||||
| .orElseThrow(() -> BaseException.from(ErrorCode.MEMBER_NOT_FOUND)); | ||||||||||||||||
|
|
||||||||||||||||
| Survey survey = Survey.create(member, dto.getAnswers()); | ||||||||||||||||
| surveyRepository.save(survey); | ||||||||||||||||
| } | ||||||||||||||||
| Survey survey = surveyRepository.findByMemberIdAndIsDeletedFalse(member.getId()) | ||||||||||||||||
| .map(existing -> { | ||||||||||||||||
| existing.update( | ||||||||||||||||
| dto.getEnergyLevel() | ||||||||||||||||
| // dto.getTravelGoal(), | ||||||||||||||||
| // dto.getTravelPace(), | ||||||||||||||||
| // dto.getCommStyle(), | ||||||||||||||||
| // dto.getPersonality(), | ||||||||||||||||
| // dto.getCompanionStyle(), | ||||||||||||||||
| // dto.getSpendPattern() | ||||||||||||||||
| ); | ||||||||||||||||
| return existing; | ||||||||||||||||
| }) | ||||||||||||||||
| .orElseGet(() -> Survey.create( | ||||||||||||||||
| member, | ||||||||||||||||
| dto.getEnergyLevel() | ||||||||||||||||
| // dto.getTravelGoal(), | ||||||||||||||||
| // dto.getTravelPace(), | ||||||||||||||||
| // dto.getCommStyle(), | ||||||||||||||||
| // dto.getPersonality(), | ||||||||||||||||
| // dto.getCompanionStyle(), | ||||||||||||||||
| // dto.getSpendPattern() | ||||||||||||||||
|
||||||||||||||||
| // dto.getSpendPattern() | |
| ); | |
| return existing; | |
| }) | |
| .orElseGet(() -> Survey.create( | |
| member, | |
| dto.getEnergyLevel() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The @OnetoOne relationship with
optional = falseandnullable = falsecreates a constraint that every Survey must have a Member, but the Member entity showsoptional = truefor the reverse relationship. This asymmetry could cause issues during entity creation or when a member exists without a survey.