Skip to content

Commit

Permalink
Merge pull request #32 from KusitmsHDmedi/feature/30-survey
Browse files Browse the repository at this point in the history
[feat] 설문조사 세부 내용 api 구현
  • Loading branch information
RyuKwanKon authored Sep 16, 2023
2 parents 3a0d67e + 917d367 commit 54c7435
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -43,10 +44,11 @@ public ResponseEntity<BaseResponse<?>> getAllSurveys() {
}

@GetMapping("/result")
public ResponseEntity<BaseResponse<?>> getSurveyResult(@RequestParam final Long surveyId) {

public ResponseEntity<BaseResponse<?>> 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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Respondent> parentsSurveyList = new ArrayList<>();
private List<Respondent> teacherSurveyList = new ArrayList<>();
private List<Respondent> parentsSurveyList;
private List<Respondent> teacherSurveyList;

public static EachSurvey createEachSurvey(){
return EachSurvey.builder()
.parentsSurveyList(new ArrayList<>())
.teacherSurveyList(new ArrayList<>())
.build();
}

public void addParentsSurveyList(Respondent parentsSurvey){
this.parentsSurveyList.add(parentsSurvey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@Builder
@Data
public class Respondent extends BaseTimeEntity {
private List<BaseSurvey> baseSurveyList = new ArrayList<>();
private List<BaseSurvey> baseSurveyList;
private int totalScore;

public static Respondent createRespondent(List<BaseSurvey> baseSurveyList, int totalScore){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,23 +17,52 @@
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Transactional
@Service
public class SurveyService {
private final SurveyRepository surveyRepository;
private final MessageSourceProvider messageSourceProvider;

public void processSurvey(HDmediUser hDmediUser, CreateSurveyDto createSurveyDto) {
List<BaseSurvey> 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<Respondent> teacherSurveyList = currentSurvey.getEachSurvey().getTeacherSurveyList();
if(teacherSurveyList.size() <= id) return Respondent.createRespondent(null, -1);
return teacherSurveyList.get(id);
}

private List<BaseSurvey> createBaseSurvey(List<BaseSurveyDto> questionList){
Expand All @@ -41,6 +72,24 @@ private List<BaseSurvey> createBaseSurvey(List<BaseSurveyDto> 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);
}



}


Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
3 changes: 3 additions & 0 deletions src/main/resources/messages_ko_KR.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
emergency.content = ????? ? ???? ??? ???? ????. ??? ?? ???? ??? ????? ????? ??? ?? ??? ?? ?? ???? ?? ?? ????? ???? ??? ??? ??? ??? ???? ??? ??? ?? ???? ??? ?? ?? ????.
non.emergency.content = ?? ??? ?? ????. ??? ??? ???? ??? ??? ?? ? ???? ???? ??? ??? ?? ?? ??????. ADHD? ?? ? ??? ??? ???? ?? www.adhd.or.kr? ?????!
non.Survey.content = ???? ??? ????? ???? ???? ??????

0 comments on commit 54c7435

Please sign in to comment.