diff --git a/src/main/java/com/kusithm/hdmedi_server/domain/survey/controller/SurveyController.java b/src/main/java/com/kusithm/hdmedi_server/domain/survey/controller/SurveyController.java index ddb72f0..5644c7d 100644 --- a/src/main/java/com/kusithm/hdmedi_server/domain/survey/controller/SurveyController.java +++ b/src/main/java/com/kusithm/hdmedi_server/domain/survey/controller/SurveyController.java @@ -1,6 +1,7 @@ package com.kusithm.hdmedi_server.domain.survey.controller; import com.kusithm.hdmedi_server.domain.survey.dto.request.CreateSurveyDto; +import com.kusithm.hdmedi_server.domain.survey.dto.response.SurveyDetailResultDto; import com.kusithm.hdmedi_server.domain.survey.service.SurveyService; import com.kusithm.hdmedi_server.global.common.BaseResponse; import com.kusithm.hdmedi_server.global.common.HDmediUser; @@ -43,10 +44,11 @@ public ResponseEntity> getAllSurveys() { } @GetMapping("/result") - public ResponseEntity> getSurveyResult(@RequestParam final Long surveyId) { - + public ResponseEntity> getSurveyResult(@AuthenticatedUserId final HDmediUser hDmediUser, + @RequestParam final Long surveyId) { + final SurveyDetailResultDto responseDto = surveyService.getDetailResult(hDmediUser, surveyId); return ResponseEntity.status(HttpStatus.OK) - .body(BaseResponse.of(SuccessCode.OK, null)); + .body(BaseResponse.of(SuccessCode.OK, responseDto)); } @GetMapping("/detail") diff --git a/src/main/java/com/kusithm/hdmedi_server/domain/survey/domain/EachSurvey.java b/src/main/java/com/kusithm/hdmedi_server/domain/survey/domain/EachSurvey.java index f9b3d3f..c682674 100644 --- a/src/main/java/com/kusithm/hdmedi_server/domain/survey/domain/EachSurvey.java +++ b/src/main/java/com/kusithm/hdmedi_server/domain/survey/domain/EachSurvey.java @@ -1,15 +1,26 @@ package com.kusithm.hdmedi_server.domain.survey.domain; import com.kusithm.hdmedi_server.global.common.BaseTimeEntity; +import lombok.Builder; import lombok.Data; +import lombok.Getter; import java.util.ArrayList; import java.util.List; +@Builder +@Getter @Data public class EachSurvey { - private List parentsSurveyList = new ArrayList<>(); - private List teacherSurveyList = new ArrayList<>(); + private List parentsSurveyList; + private List teacherSurveyList; + + public static EachSurvey createEachSurvey(){ + return EachSurvey.builder() + .parentsSurveyList(new ArrayList<>()) + .teacherSurveyList(new ArrayList<>()) + .build(); + } public void addParentsSurveyList(Respondent parentsSurvey){ this.parentsSurveyList.add(parentsSurvey); diff --git a/src/main/java/com/kusithm/hdmedi_server/domain/survey/domain/Respondent.java b/src/main/java/com/kusithm/hdmedi_server/domain/survey/domain/Respondent.java index 6f4ceab..2b8cf01 100644 --- a/src/main/java/com/kusithm/hdmedi_server/domain/survey/domain/Respondent.java +++ b/src/main/java/com/kusithm/hdmedi_server/domain/survey/domain/Respondent.java @@ -10,7 +10,7 @@ @Builder @Data public class Respondent extends BaseTimeEntity { - private List baseSurveyList = new ArrayList<>(); + private List baseSurveyList; private int totalScore; public static Respondent createRespondent(List baseSurveyList, int totalScore){ diff --git a/src/main/java/com/kusithm/hdmedi_server/domain/survey/domain/Survey.java b/src/main/java/com/kusithm/hdmedi_server/domain/survey/domain/Survey.java index 3bb1888..d659b94 100644 --- a/src/main/java/com/kusithm/hdmedi_server/domain/survey/domain/Survey.java +++ b/src/main/java/com/kusithm/hdmedi_server/domain/survey/domain/Survey.java @@ -16,4 +16,12 @@ public class Survey { private String id; private Long userId; private EachSurvey eachSurvey; + + public static Survey createSurvey(Long userId){ + EachSurvey createdEachSurvey = EachSurvey.createEachSurvey(); + return Survey.builder() + .userId(userId) + .eachSurvey(createdEachSurvey) + .build(); + } } diff --git a/src/main/java/com/kusithm/hdmedi_server/domain/survey/dto/response/SurveyDetailResultDto.java b/src/main/java/com/kusithm/hdmedi_server/domain/survey/dto/response/SurveyDetailResultDto.java new file mode 100644 index 0000000..4e49ddc --- /dev/null +++ b/src/main/java/com/kusithm/hdmedi_server/domain/survey/dto/response/SurveyDetailResultDto.java @@ -0,0 +1,22 @@ +package com.kusithm.hdmedi_server.domain.survey.dto.response; + +import lombok.Builder; +import lombok.Getter; + +@Builder +@Getter +public class SurveyDetailResultDto { + private int parentsScore; + private String parentsMessage; + private int teacherScore; + private String teacherMessage; + + public static SurveyDetailResultDto of(int parentsScore, String parentsMessage, int teacherScore, String teacherMessage){ + return SurveyDetailResultDto.builder() + .parentsScore(parentsScore) + .parentsMessage(parentsMessage) + .teacherScore(teacherScore) + .teacherMessage(teacherMessage) + .build(); + } +} diff --git a/src/main/java/com/kusithm/hdmedi_server/domain/survey/service/SurveyService.java b/src/main/java/com/kusithm/hdmedi_server/domain/survey/service/SurveyService.java index dee4b50..4b9a884 100644 --- a/src/main/java/com/kusithm/hdmedi_server/domain/survey/service/SurveyService.java +++ b/src/main/java/com/kusithm/hdmedi_server/domain/survey/service/SurveyService.java @@ -5,8 +5,10 @@ import com.kusithm.hdmedi_server.domain.survey.domain.Survey; import com.kusithm.hdmedi_server.domain.survey.dto.request.BaseSurveyDto; import com.kusithm.hdmedi_server.domain.survey.dto.request.CreateSurveyDto; +import com.kusithm.hdmedi_server.domain.survey.dto.response.SurveyDetailResultDto; import com.kusithm.hdmedi_server.domain.survey.repository.SurveyRepository; import com.kusithm.hdmedi_server.global.common.HDmediUser; +import com.kusithm.hdmedi_server.global.common.MessageSourceProvider; import com.kusithm.hdmedi_server.global.error.exception.EntityNotFoundException; import com.kusithm.hdmedi_server.global.error.exception.ErrorCode; import jakarta.transaction.Transactional; @@ -15,6 +17,7 @@ import org.springframework.stereotype.Service; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; @RequiredArgsConstructor @@ -22,16 +25,44 @@ @Service public class SurveyService { private final SurveyRepository surveyRepository; + private final MessageSourceProvider messageSourceProvider; public void processSurvey(HDmediUser hDmediUser, CreateSurveyDto createSurveyDto) { List baseSurveyList = createBaseSurvey(createSurveyDto.getQuestionList()); Respondent respondent = Respondent.createRespondent(baseSurveyList, createSurveyDto.getTotalScore()); - Survey currentSurvey = surveyRepository.findByUserId(hDmediUser.getId()).orElseThrow(() -> new EntityNotFoundException(ErrorCode.USER_NOT_FOUND)); - if(hDmediUser.getIsGuest()){ - currentSurvey.getEachSurvey().addTeacherSurveyList(respondent); - }else { - currentSurvey.getEachSurvey().addParentsSurveyList(respondent); - } + Survey currentSurvey = findSurvey(hDmediUser.getId()); + addRespondentSurvey(hDmediUser.getIsGuest(), currentSurvey, respondent); + saveSurvey(currentSurvey); + } + + public SurveyDetailResultDto getDetailResult(HDmediUser hDmediUser, Long surveyId){ + Survey currentSurvey = findSurvey(hDmediUser.getId()); + Respondent parentsRespondent = getParentsRespondent(currentSurvey, surveyId.intValue()); + Respondent teacherRespondent = getTeacherRespondent(currentSurvey, surveyId.intValue()); + String parentsMessage = getParentMessage(parentsRespondent.getTotalScore()); + String teacherMessage = getTeacherRespondent(parentsRespondent.getTotalScore()); + return SurveyDetailResultDto.of(parentsRespondent.getTotalScore(), parentsMessage, teacherRespondent.getTotalScore(), teacherMessage); + } + + private String getParentMessage(int totalScore){ + if(totalScore >= 19) return messageSourceProvider.getEmergencyContent(); + return messageSourceProvider.getNonEmergencyContent(); + } + + private String getTeacherRespondent(int totalScore){ + if(totalScore == -1) return messageSourceProvider.getNonSurveyContent(); + if(totalScore >= 19) return messageSourceProvider.getEmergencyContent(); + return messageSourceProvider.getNonEmergencyContent(); + } + + private Respondent getParentsRespondent(Survey currentSurvey, int id){ + return currentSurvey.getEachSurvey().getParentsSurveyList().get(id); + } + + private Respondent getTeacherRespondent(Survey currentSurvey, int id){ + List teacherSurveyList = currentSurvey.getEachSurvey().getTeacherSurveyList(); + if(teacherSurveyList.size() <= id) return Respondent.createRespondent(null, -1); + return teacherSurveyList.get(id); } private List createBaseSurvey(List questionList){ @@ -41,6 +72,24 @@ private List createBaseSurvey(List questionList){ .collect(Collectors.toList()); } + private Survey findSurvey(Long userId){ + return surveyRepository.findByUserId(userId) + .orElseGet(() -> Survey.createSurvey(userId)); + } + + private void addRespondentSurvey(Boolean isGuest, Survey currentSurvey, Respondent respondent){ + if(isGuest) + currentSurvey.getEachSurvey().addTeacherSurveyList(respondent); + else + currentSurvey.getEachSurvey().addParentsSurveyList(respondent); + } + + private void saveSurvey(Survey survey){ + surveyRepository.save(survey); + } + + + } diff --git a/src/main/java/com/kusithm/hdmedi_server/global/common/MessageSourceProvider.java b/src/main/java/com/kusithm/hdmedi_server/global/common/MessageSourceProvider.java new file mode 100644 index 0000000..9e866c6 --- /dev/null +++ b/src/main/java/com/kusithm/hdmedi_server/global/common/MessageSourceProvider.java @@ -0,0 +1,27 @@ +package com.kusithm.hdmedi_server.global.common; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.MessageSource; +import org.springframework.stereotype.Component; + +import java.util.Locale; + +@Slf4j +@RequiredArgsConstructor +@Component +public class MessageSourceProvider { + private final MessageSource messageSource; + + public String getEmergencyContent() { + return messageSource.getMessage("emergency.content", null, Locale.KOREA); + } + + public String getNonEmergencyContent() { + return messageSource.getMessage("non.emergency.content", null, Locale.KOREA); + } + + public String getNonSurveyContent() { + return messageSource.getMessage("non.Survey.content", null, Locale.KOREA); + } +} diff --git a/src/main/java/com/kusithm/hdmedi_server/global/config/MessageConfig.java b/src/main/java/com/kusithm/hdmedi_server/global/config/MessageConfig.java new file mode 100644 index 0000000..649ad5c --- /dev/null +++ b/src/main/java/com/kusithm/hdmedi_server/global/config/MessageConfig.java @@ -0,0 +1,18 @@ +package com.kusithm.hdmedi_server.global.config; + +import org.springframework.context.MessageSource; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.support.ResourceBundleMessageSource; + +@Configuration +public class MessageConfig { + @Bean + public MessageSource messageSource() { + ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); + messageSource.setBasenames("messages"); + messageSource.setDefaultEncoding("UTF-8"); + + return messageSource; + } +} diff --git a/src/main/resources/messages_ko_KR.properties b/src/main/resources/messages_ko_KR.properties new file mode 100644 index 0000000..82d8164 --- /dev/null +++ b/src/main/resources/messages_ko_KR.properties @@ -0,0 +1,3 @@ +emergency.content = ????? ? ???? ??? ???? ????. ??? ?? ???? ??? ????? ????? ??? ?? ??? ?? ?? ???? ?? ?? ????? ???? ??? ??? ??? ??? ???? ??? ??? ?? ???? ??? ?? ?? ????. +non.emergency.content = ?? ??? ?? ????. ??? ??? ???? ??? ??? ?? ? ???? ???? ??? ??? ?? ?? ??????. ADHD? ?? ? ??? ??? ???? ?? www.adhd.or.kr? ?????! +non.Survey.content = ???? ??? ????? ???? ???? ?????? \ No newline at end of file