diff --git a/build.gradle b/build.gradle index 3b12466..f447aaf 100644 --- a/build.gradle +++ b/build.gradle @@ -55,6 +55,9 @@ dependencies { // redis implementation 'org.springframework.boot:spring-boot-starter-data-redis:2.3.1.RELEASE' + + //mongoDB + implementation 'org.springframework.boot:spring-boot-starter-data-mongodb' } tasks.named('test') { diff --git a/src/main/java/com/kusithm/hdmedi_server/HDmediServerApplication.java b/src/main/java/com/kusithm/hdmedi_server/HDmediServerApplication.java index eb196bc..258aa3a 100644 --- a/src/main/java/com/kusithm/hdmedi_server/HDmediServerApplication.java +++ b/src/main/java/com/kusithm/hdmedi_server/HDmediServerApplication.java @@ -3,8 +3,14 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.context.annotation.EnableAspectJAutoProxy; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.data.mongodb.config.EnableMongoAuditing; +import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; @SpringBootApplication +@EnableMongoAuditing public class HDmediServerApplication { // aws sdk 연동 static { 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 de1e958..ddb72f0 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,54 +1,57 @@ package com.kusithm.hdmedi_server.domain.survey.controller; -import com.kusithm.hdmedi_server.domain.survey.domain.SurveyDto; -import com.kusithm.hdmedi_server.domain.survey.dto.response.SurveyResultResponseDto; -import com.kusithm.hdmedi_server.domain.survey.repository.Survey; +import com.kusithm.hdmedi_server.domain.survey.dto.request.CreateSurveyDto; 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; +import com.kusithm.hdmedi_server.global.common.SuccessCode; +import com.kusithm.hdmedi_server.global.config.auth.AuthenticatedUserId; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import java.util.List; -@RestController -@RequestMapping("/surveys") +@RequiredArgsConstructor +@RequestMapping("/api/survey") +@Controller public class SurveyController { - private final SurveyService surveyService; - public SurveyController(SurveyService surveyService) { - this.surveyService = surveyService; + @PostMapping + public ResponseEntity> submitSurvey(@AuthenticatedUserId final HDmediUser hDmediUser, + @RequestBody final CreateSurveyDto requestDto) { + surveyService.processSurvey(hDmediUser, requestDto); + return ResponseEntity.status(HttpStatus.CREATED) + .body(BaseResponse.of(SuccessCode.CREATED, null)); } - //설문 결과 저장 - @PostMapping("/submit-survey") - public ResponseEntity submitSurvey(@ModelAttribute SurveyDto surveyDTO) { - SurveyResultResponseDto result = surveyService.processSurvey(surveyDTO); - return ResponseEntity.ok(result); - } - // 설문 결과를 전송 - @GetMapping("/get-survey-result") - public ResponseEntity getSurveyResult() { - SurveyResultResponseDto result = surveyService.getSurveyResult(); - return ResponseEntity.ok(result); + @GetMapping + public ResponseEntity> getSurveyResult() { + + return ResponseEntity.status(HttpStatus.OK) + .body(BaseResponse.of(SuccessCode.OK, null)); } - // 모든 설문 데이터를 조회 - @GetMapping("/") - public ResponseEntity> getAllSurveys() { - List surveys = surveyService.getAllSurveys(); - return ResponseEntity.ok(surveys); + + @GetMapping("/all") + public ResponseEntity> getAllSurveys() { + + return ResponseEntity.status(HttpStatus.OK) + .body(BaseResponse.of(SuccessCode.OK, null)); } - // 설문 상세 조회 - 결과 조회 - @GetMapping("/{surveyId}/result") - public ResponseEntity getSurveyResult(@PathVariable Long surveyId) { - SurveyResultResponseDto result = surveyService.getSurveyResultById(surveyId); - return ResponseEntity.ok(result); + @GetMapping("/result") + public ResponseEntity> getSurveyResult(@RequestParam final Long surveyId) { + + return ResponseEntity.status(HttpStatus.OK) + .body(BaseResponse.of(SuccessCode.OK, null)); } - // 설문 상세 조회 - 내용 조회 - @GetMapping("/{surveyId}/content") - public ResponseEntity getSurveyContent(@PathVariable Long surveyId) { - Survey survey = surveyService.getSurveyById(surveyId); - return ResponseEntity.ok(survey); + @GetMapping("/detail") + public ResponseEntity> getSurveyContent(@RequestParam final Long surveyId) { + return ResponseEntity.status(HttpStatus.OK) + .body(BaseResponse.of(SuccessCode.OK, null)); } } \ No newline at end of file diff --git a/src/main/java/com/kusithm/hdmedi_server/domain/survey/domain/BaseSurvey.java b/src/main/java/com/kusithm/hdmedi_server/domain/survey/domain/BaseSurvey.java new file mode 100644 index 0000000..fed115b --- /dev/null +++ b/src/main/java/com/kusithm/hdmedi_server/domain/survey/domain/BaseSurvey.java @@ -0,0 +1,19 @@ +package com.kusithm.hdmedi_server.domain.survey.domain; + +import lombok.Builder; +import lombok.Data; +import org.springframework.data.mongodb.core.mapping.Document; + +@Builder +@Data +public class BaseSurvey { + private String question; + private int score; + + public static BaseSurvey createBaseSurvey(String question, int score){ + return BaseSurvey.builder() + .question(question) + .score(score) + .build(); + } +} 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 new file mode 100644 index 0000000..f9b3d3f --- /dev/null +++ b/src/main/java/com/kusithm/hdmedi_server/domain/survey/domain/EachSurvey.java @@ -0,0 +1,21 @@ +package com.kusithm.hdmedi_server.domain.survey.domain; + +import com.kusithm.hdmedi_server.global.common.BaseTimeEntity; +import lombok.Data; + +import java.util.ArrayList; +import java.util.List; + +@Data +public class EachSurvey { + private List parentsSurveyList = new ArrayList<>(); + private List teacherSurveyList = new ArrayList<>(); + + public void addParentsSurveyList(Respondent parentsSurvey){ + this.parentsSurveyList.add(parentsSurvey); + } + + public void addTeacherSurveyList(Respondent teacherSurvey){ + this.teacherSurveyList.add(teacherSurvey); + } +} 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 new file mode 100644 index 0000000..6f4ceab --- /dev/null +++ b/src/main/java/com/kusithm/hdmedi_server/domain/survey/domain/Respondent.java @@ -0,0 +1,23 @@ +package com.kusithm.hdmedi_server.domain.survey.domain; + +import com.kusithm.hdmedi_server.global.common.BaseTimeEntity; +import lombok.Builder; +import lombok.Data; + +import java.util.ArrayList; +import java.util.List; + +@Builder +@Data +public class Respondent extends BaseTimeEntity { + private List baseSurveyList = new ArrayList<>(); + private int totalScore; + + public static Respondent createRespondent(List baseSurveyList, int totalScore){ + return Respondent.builder() + .baseSurveyList(baseSurveyList) + .totalScore(totalScore) + .build(); + } + +} 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 new file mode 100644 index 0000000..3bb1888 --- /dev/null +++ b/src/main/java/com/kusithm/hdmedi_server/domain/survey/domain/Survey.java @@ -0,0 +1,19 @@ +package com.kusithm.hdmedi_server.domain.survey.domain; + +import jakarta.persistence.*; +import lombok.*; +import org.springframework.data.mongodb.core.mapping.Document; + +import java.util.List; + +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +@Builder +@Getter +@Document(collection = "survey") +public class Survey { + @Id + private String id; + private Long userId; + private EachSurvey eachSurvey; +} diff --git a/src/main/java/com/kusithm/hdmedi_server/domain/survey/domain/SurveyDto.java b/src/main/java/com/kusithm/hdmedi_server/domain/survey/domain/SurveyDto.java deleted file mode 100644 index bc36c60..0000000 --- a/src/main/java/com/kusithm/hdmedi_server/domain/survey/domain/SurveyDto.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.kusithm.hdmedi_server.domain.survey.domain; - -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import lombok.*; - -@NoArgsConstructor(access = AccessLevel.PROTECTED) -@AllArgsConstructor -@Builder -@Getter -@Entity -public class SurveyDto { - - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - private String question; - - private String answer; -} diff --git a/src/main/java/com/kusithm/hdmedi_server/domain/survey/dto/request/BaseSurveyDto.java b/src/main/java/com/kusithm/hdmedi_server/domain/survey/dto/request/BaseSurveyDto.java new file mode 100644 index 0000000..1e14cc5 --- /dev/null +++ b/src/main/java/com/kusithm/hdmedi_server/domain/survey/dto/request/BaseSurveyDto.java @@ -0,0 +1,12 @@ +package com.kusithm.hdmedi_server.domain.survey.dto.request; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@Getter +public class BaseSurveyDto { + private String question; + private int score; +} diff --git a/src/main/java/com/kusithm/hdmedi_server/domain/survey/dto/request/CreateSurveyDto.java b/src/main/java/com/kusithm/hdmedi_server/domain/survey/dto/request/CreateSurveyDto.java new file mode 100644 index 0000000..fe1b1c8 --- /dev/null +++ b/src/main/java/com/kusithm/hdmedi_server/domain/survey/dto/request/CreateSurveyDto.java @@ -0,0 +1,14 @@ +package com.kusithm.hdmedi_server.domain.survey.dto.request; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.List; + +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@Getter +public class CreateSurveyDto { + private List questionList; + private int totalScore; +} diff --git a/src/main/java/com/kusithm/hdmedi_server/domain/survey/dto/response/SurveyResultResponseDto.java b/src/main/java/com/kusithm/hdmedi_server/domain/survey/dto/response/SurveyResultResponseDto.java deleted file mode 100644 index 080ee11..0000000 --- a/src/main/java/com/kusithm/hdmedi_server/domain/survey/dto/response/SurveyResultResponseDto.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.kusithm.hdmedi_server.domain.survey.dto.response; - -import lombok.Builder; -import lombok.Getter; - -@Getter -@Builder -public class SurveyResultResponseDto { - private String message; - private int score; -} diff --git a/src/main/java/com/kusithm/hdmedi_server/domain/survey/repository/Survey.java b/src/main/java/com/kusithm/hdmedi_server/domain/survey/repository/Survey.java deleted file mode 100644 index da25fc1..0000000 --- a/src/main/java/com/kusithm/hdmedi_server/domain/survey/repository/Survey.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.kusithm.hdmedi_server.domain.survey.repository; - - -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import org.springframework.data.annotation.Id; - -@Entity -public class Survey { - @jakarta.persistence.Id - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; // 엔터티의 식별자 - - private String question; - private String answer; - - // Getter와 Setter 메서드 - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public void setQuestion(String question) { - this.question = question; - } - - public void setAnswer(String answer) { - this.answer = answer; - } - -} diff --git a/src/main/java/com/kusithm/hdmedi_server/domain/survey/repository/SurveyRepository.java b/src/main/java/com/kusithm/hdmedi_server/domain/survey/repository/SurveyRepository.java index da788b1..71323f3 100644 --- a/src/main/java/com/kusithm/hdmedi_server/domain/survey/repository/SurveyRepository.java +++ b/src/main/java/com/kusithm/hdmedi_server/domain/survey/repository/SurveyRepository.java @@ -1,7 +1,10 @@ package com.kusithm.hdmedi_server.domain.survey.repository; -import org.springframework.data.jpa.repository.JpaRepository; +import com.kusithm.hdmedi_server.domain.survey.domain.Survey; +import org.springframework.data.mongodb.repository.MongoRepository; +import java.util.Optional; -public interface SurveyRepository extends JpaRepository { -} \ No newline at end of file +public interface SurveyRepository extends MongoRepository { + Optional findByUserId(Long userId); +} diff --git a/src/main/java/com/kusithm/hdmedi_server/domain/survey/service/ResourceNotFoundException.java b/src/main/java/com/kusithm/hdmedi_server/domain/survey/service/ResourceNotFoundException.java deleted file mode 100644 index 216ebe4..0000000 --- a/src/main/java/com/kusithm/hdmedi_server/domain/survey/service/ResourceNotFoundException.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.kusithm.hdmedi_server.domain.survey.service; - -import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.ResponseStatus; - -@ResponseStatus(HttpStatus.NOT_FOUND) -public class ResourceNotFoundException extends RuntimeException { - - public ResourceNotFoundException(String message) { - super(message); - } -} - 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 f9e9e29..dee4b50 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 @@ -1,79 +1,46 @@ package com.kusithm.hdmedi_server.domain.survey.service; -import com.kusithm.hdmedi_server.domain.survey.domain.SurveyDto; -import com.kusithm.hdmedi_server.domain.survey.dto.response.SurveyResultResponseDto; -import com.kusithm.hdmedi_server.domain.survey.repository.Survey; +import com.kusithm.hdmedi_server.domain.survey.domain.BaseSurvey; +import com.kusithm.hdmedi_server.domain.survey.domain.Respondent; +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.repository.SurveyRepository; -import org.springframework.beans.factory.annotation.Autowired; +import com.kusithm.hdmedi_server.global.common.HDmediUser; +import com.kusithm.hdmedi_server.global.error.exception.EntityNotFoundException; +import com.kusithm.hdmedi_server.global.error.exception.ErrorCode; +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.stereotype.Service; import java.util.List; +import java.util.stream.Collectors; +@RequiredArgsConstructor +@Transactional @Service public class SurveyService { - - @Autowired - private SurveyRepository surveyRepository; - - public SurveyResultResponseDto processSurvey(SurveyDto surveyDTO) { - Survey survey = new Survey(); - survey.setQuestion(surveyDTO.getQuestion()); - survey.setAnswer(surveyDTO.getAnswer()); - - surveyRepository.save(survey); - - // 저장된 점수 계산 또는 다른 작업 수행 - int score = calculateScore(survey); - - // SurveyResultResponseDto 빌더를 사용하여 결과 생성 - return SurveyResultResponseDto.builder() - .message("설문 결과가 성공적으로 저장되었습니다.") - .score(score) - .build(); } - - // 설문 결과를 조회하는 메서드 추가 - public SurveyResultResponseDto getSurveyResult() { - // 데이터베이스에서 설문 결과를 조회하고, 결과 메시지와 저장된 점수를 생성하여 반환 - // 여기서는 임의의 값을 반환하도록 가정 - int score = 42; - return SurveyResultResponseDto.builder() - .message("설문 결과 조회가 성공적으로 완료되었습니다.") - .score(score) - .build(); - } - private int calculateScore(Survey survey) { - // 저장된 점수 계산 또는 다른 로직 수행 - // 여기서는 임의의 값을 반환하도록 가정 - return 42; - } - - public List getAllSurveys() { - // 모든 설문 데이터를 조회하여 반환 - return surveyRepository.findAll(); + private final SurveyRepository surveyRepository; + + 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); + } } - public SurveyResultResponseDto getSurveyResultById(Long surveyId) { - // surveyId를 사용하여 해당 설문 결과를 데이터베이스에서 조회 - // 조회한 결과를 SurveyResultResponseDto로 변환하여 반환 - Survey survey = surveyRepository.findById(surveyId) - .orElseThrow(() -> new ResourceNotFoundException("설문 데이터가 없습니다. ID: " + surveyId)); - - // 저장된 점수 계산 또는 다른 작업 수행 - int score = calculateScore(survey); - - // SurveyResultResponseDto 빌더를 사용하여 결과 생성 - return SurveyResultResponseDto.builder() - .message("설문 결과 조회가 성공적으로 완료되었습니다.") - .score(score) - .build(); + private List createBaseSurvey(List questionList){ + return questionList.stream() + .map(baseSurveyDto -> + BaseSurvey.createBaseSurvey(baseSurveyDto.getQuestion(), baseSurveyDto.getScore())) + .collect(Collectors.toList()); } - public Survey getSurveyById(Long surveyId) { - // surveyId를 사용하여 해당 설문 데이터를 데이터베이스에서 조회 - // 만약 해당 설문 데이터가 존재하지 않으면 예외를 던질 수 있습니다. - return surveyRepository.findById(surveyId) - .orElseThrow(() -> new ResourceNotFoundException("설문 데이터가 없습니다. ID: " + surveyId)); - } } diff --git a/src/main/java/com/kusithm/hdmedi_server/global/common/BaseTimeEntity.java b/src/main/java/com/kusithm/hdmedi_server/global/common/BaseTimeEntity.java new file mode 100644 index 0000000..46caf8c --- /dev/null +++ b/src/main/java/com/kusithm/hdmedi_server/global/common/BaseTimeEntity.java @@ -0,0 +1,22 @@ +package com.kusithm.hdmedi_server.global.common; + +import jakarta.persistence.Column; +import jakarta.persistence.EntityListeners; +import jakarta.persistence.MappedSuperclass; +import lombok.Getter; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +import java.time.LocalDateTime; + +@Getter +@EntityListeners(AuditingEntityListener.class) +@MappedSuperclass +public abstract class BaseTimeEntity { + @CreatedDate + @Column(updatable = false) + private LocalDateTime createDate; + @LastModifiedDate + private LocalDateTime lastModifiedDate; +}