diff --git a/src/main/java/net/skhu/likelion12thteam03be/SelectColor/domain/SelectColor.java b/src/main/java/net/skhu/likelion12thteam03be/SelectColor/domain/SelectColor.java index 150da26..5e8b608 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/SelectColor/domain/SelectColor.java +++ b/src/main/java/net/skhu/likelion12thteam03be/SelectColor/domain/SelectColor.java @@ -1,3 +1,4 @@ +/* package net.skhu.likelion12thteam03be.SelectColor.domain; import jakarta.persistence.*; @@ -5,6 +6,7 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import net.skhu.likelion12thteam03be.color.domian.Color; import net.skhu.likelion12thteam03be.survey.domain.Survey; @Entity @@ -18,16 +20,17 @@ public class SelectColor { @ManyToOne @JoinColumn(name = "survey_id") - Survey survey; + private Survey survey; @ManyToOne @JoinColumn(name = "color_id") - SelectColor selectColor; + private Color color; @Builder - public SelectColor(Long id, Survey survey, SelectColor selectColor) { + public SelectColor(Long id, Survey survey, Color Color) { this.id = id; this.survey = survey; - this.selectColor = selectColor; + this.color = color; } } +*/ diff --git a/src/main/java/net/skhu/likelion12thteam03be/SelectColor/domain/repository/SelectColorRepository.java b/src/main/java/net/skhu/likelion12thteam03be/SelectColor/domain/repository/SelectColorRepository.java index d27c271..18f218d 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/SelectColor/domain/repository/SelectColorRepository.java +++ b/src/main/java/net/skhu/likelion12thteam03be/SelectColor/domain/repository/SelectColorRepository.java @@ -1,3 +1,4 @@ +/* package net.skhu.likelion12thteam03be.SelectColor.domain.repository; import net.skhu.likelion12thteam03be.SelectColor.domain.SelectColor; @@ -5,3 +6,4 @@ public interface SelectColorRepository extends JpaRepository { } +*/ diff --git a/src/main/java/net/skhu/likelion12thteam03be/emotion/application/EmotionService.java b/src/main/java/net/skhu/likelion12thteam03be/emotion/application/EmotionService.java new file mode 100644 index 0000000..d3419fc --- /dev/null +++ b/src/main/java/net/skhu/likelion12thteam03be/emotion/application/EmotionService.java @@ -0,0 +1,23 @@ +package net.skhu.likelion12thteam03be.emotion.application; + +import lombok.RequiredArgsConstructor; +import net.skhu.likelion12thteam03be.emotion.domain.Emotion; +import net.skhu.likelion12thteam03be.emotion.domain.Emotions; +import net.skhu.likelion12thteam03be.emotion.domain.repository.EmotionRepository; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class EmotionService { + private EmotionRepository emotionRepository; + +/* public void saveEmotions(){ + Emotion emotion = Emotion.builder() + .name(Emotions.행복한) + .furniture("모던 쇼파, 커피 테이블") + .type("돌고래") + .animalPic("~~~~~~~~~~~~~~~~~/post/1_dolphin.png") + .build(); + emotionRepository.save(emotion); + }*/ +} diff --git a/src/main/java/net/skhu/likelion12thteam03be/emotion/domain/Emotion.java b/src/main/java/net/skhu/likelion12thteam03be/emotion/domain/Emotion.java index e5c932f..de70099 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/emotion/domain/Emotion.java +++ b/src/main/java/net/skhu/likelion12thteam03be/emotion/domain/Emotion.java @@ -12,19 +12,19 @@ public class Emotion { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "emotion_id") private Long id; @Enumerated(EnumType.STRING) - Emotions emotions; + private Emotions name; private String furniture; private String type; private String animalPic; @Builder - public Emotion(Long id, Emotions emotions, String furniture, String type, String animalPic) { - this.id = id; - this.emotions = emotions; + public Emotion(Emotions name, String furniture, String type, String animalPic) { + this.name = name; this.furniture = furniture; this.type = type; this.animalPic = animalPic; diff --git a/src/main/java/net/skhu/likelion12thteam03be/emotion/domain/Emotions.java b/src/main/java/net/skhu/likelion12thteam03be/emotion/domain/Emotions.java index c9da5fd..62245c0 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/emotion/domain/Emotions.java +++ b/src/main/java/net/skhu/likelion12thteam03be/emotion/domain/Emotions.java @@ -1,26 +1,5 @@ package net.skhu.likelion12thteam03be.emotion.domain; -public enum Emotions { // converter - HAPPY("행복한"), - HURRIED("괴로운"), - DULL("암울한"), - TOUCHED("감동적인"), - EXCITING("신나는"), - RELAXED("편안한"), - FEARFUL("두려운"), - INTERESTING("흥미로운"), - NERVOUS("긴장한"), - ANXIOUS("불안한"), - JOYFUL("기쁜"), - CONCERNED("걱정되는"), - EXCITED("설레는"), - WEAPON("무기력한"), - FRUSTRATED("답답한"), - FRESH("상쾌한"); - - private final String name; - - Emotions(String name) { - this.name = name; - } +public enum Emotions { + 행복한, 괴로운, 암울한, 감동적인, 신나는, 편안한, 두려운, 흥미로운, 긴장한, 불안한, 기쁜, 걱정되는, 설레는, 무기력한, 답답한, 상쾌한 } diff --git a/src/main/java/net/skhu/likelion12thteam03be/survey/api/SurveyController.java b/src/main/java/net/skhu/likelion12thteam03be/survey/api/SurveyController.java index 7c6a4e6..b6f8515 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/survey/api/SurveyController.java +++ b/src/main/java/net/skhu/likelion12thteam03be/survey/api/SurveyController.java @@ -1,7 +1,45 @@ package net.skhu.likelion12thteam03be.survey.api; -import org.springframework.web.bind.annotation.RestController; +import lombok.RequiredArgsConstructor; +import net.skhu.likelion12thteam03be.survey.api.request.SurveySaveReqDto; +import net.skhu.likelion12thteam03be.survey.api.response.SurveyResDto; +import net.skhu.likelion12thteam03be.survey.application.SurveyService; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.security.Principal; @RestController +@RequestMapping("/psy-test") +@RequiredArgsConstructor public class SurveyController { -} + private final SurveyService surveyService; + @PostMapping + public ResponseEntity createSurvey(@RequestBody SurveySaveReqDto reqDto, Principal principal) { + SurveyResDto surveyResDto = surveyService.save(reqDto, principal); + return new ResponseEntity<>(surveyResDto, HttpStatus.CREATED); + } + + @DeleteMapping + public ResponseEntity deleteSurvey(Principal principal) { + surveyService.delete(principal); + return new ResponseEntity<>("설문조사 결과가 정상적으로 삭제되었습니다.", HttpStatus.OK); + } + +/* @PatchMapping("/{surveyId}") + public ResponseEntity updateSurvey( + @PathVariable Long surveyId, + @RequestBody SurveySaveReqDto surveySaveReqDto, + Principal principal) { + SurveyResDto response = surveyService.update(surveyId, surveySaveReqDto, principal); + return new ResponseEntity<>(response, HttpStatus.OK); + } + + @GetMapping("/{surveyId}") + public ResponseEntity findByIdSurvey(@PathVariable Long surveyId) { + SurveyResDto response = surveyService.findById(surveyId); + return new ResponseEntity<>(response, HttpStatus.OK); + }*/ + +} \ No newline at end of file diff --git a/src/main/java/net/skhu/likelion12thteam03be/survey/api/request/SurveySaveReqDto.java b/src/main/java/net/skhu/likelion12thteam03be/survey/api/request/SurveySaveReqDto.java new file mode 100644 index 0000000..083de85 --- /dev/null +++ b/src/main/java/net/skhu/likelion12thteam03be/survey/api/request/SurveySaveReqDto.java @@ -0,0 +1,10 @@ +package net.skhu.likelion12thteam03be.survey.api.request; + +import java.util.List; + +public record SurveySaveReqDto( + Long emotionId, + List colorIds, + int score +) { +} diff --git a/src/main/java/net/skhu/likelion12thteam03be/survey/api/response/SurveyResDto.java b/src/main/java/net/skhu/likelion12thteam03be/survey/api/response/SurveyResDto.java new file mode 100644 index 0000000..1c3de0d --- /dev/null +++ b/src/main/java/net/skhu/likelion12thteam03be/survey/api/response/SurveyResDto.java @@ -0,0 +1,24 @@ +package net.skhu.likelion12thteam03be.survey.api.response; + +import lombok.Builder; +import net.skhu.likelion12thteam03be.post.domain.Post; +import net.skhu.likelion12thteam03be.survey.domain.Survey; + +import java.util.List; + +@Builder +public record SurveyResDto( + String animalPic, + String type, + List colorComments, // request 받을 때: List colorIds, + List postList +) { + public static SurveyResDto from(Survey survey, List colorComments) { + return SurveyResDto.builder() + .animalPic(survey.getEmotion().getAnimalPic()) + .type(survey.getEmotion().getType()) + .colorComments(colorComments) + .postList(survey.getUser().getPosts()) + .build(); + } +} diff --git a/src/main/java/net/skhu/likelion12thteam03be/survey/application/SurveyService.java b/src/main/java/net/skhu/likelion12thteam03be/survey/application/SurveyService.java index e641927..1a0ba76 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/survey/application/SurveyService.java +++ b/src/main/java/net/skhu/likelion12thteam03be/survey/application/SurveyService.java @@ -1,4 +1,105 @@ package net.skhu.likelion12thteam03be.survey.application; +import lombok.RequiredArgsConstructor; + +import net.skhu.likelion12thteam03be.color.domian.Color; +import net.skhu.likelion12thteam03be.color.domian.repository.ColorRepository; +import net.skhu.likelion12thteam03be.emotion.domain.Emotion; +import net.skhu.likelion12thteam03be.emotion.domain.repository.EmotionRepository; +import net.skhu.likelion12thteam03be.survey.api.request.SurveySaveReqDto; +import net.skhu.likelion12thteam03be.survey.api.response.SurveyResDto; +import net.skhu.likelion12thteam03be.survey.domain.Survey; +import net.skhu.likelion12thteam03be.survey.domain.repository.SurveyRepository; +import net.skhu.likelion12thteam03be.survey.exception.SurveyAlreadyExistsException; +import net.skhu.likelion12thteam03be.user.domain.User; +import net.skhu.likelion12thteam03be.user.domain.repository.UserRepository; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.security.Principal; +import java.util.List; + +@Service +@Transactional(readOnly = true) +@RequiredArgsConstructor public class SurveyService { + private final SurveyRepository surveyRepository; + private final UserRepository userRepository; + private final EmotionRepository emotionRepository; + private final ColorRepository colorRepository; + + @Transactional + public SurveyResDto save(SurveySaveReqDto surveySaveReqDto, Principal principal) { + String loginId = principal.getName(); + if (surveyRepository.existsByUserLoginId(loginId)){ + throw new SurveyAlreadyExistsException("이미 설문조사 결과가 존재합니다. 유저 아이디 = " + loginId); + } + User user = userRepository.findByLoginId(loginId) + .orElseThrow(() -> new IllegalArgumentException("유저 정보를 찾을 수 없습니다: " + loginId)); + Emotion emotion = emotionRepository.findById(surveySaveReqDto.emotionId()) + .orElseThrow(() -> new IllegalArgumentException("해당 감정을 찾을 수 없습니다: " + surveySaveReqDto.emotionId())); + List colors = colorRepository.findAllById(surveySaveReqDto.colorIds()); + + Survey survey = Survey.builder() + .score(surveySaveReqDto.score()) + .emotion(emotion) + .colors(colors) + .user(user) + .build(); + surveyRepository.save(survey); + + List colorComments = colors.stream() + .map(Color::getComment) + .toList(); + + return SurveyResDto.from(survey, colorComments); + } + + @Transactional + public void delete(Principal principal) { + String loginId = principal.getName(); + Survey survey = surveyRepository.findByUserLoginId(loginId); + if (survey == null) { + throw new IllegalArgumentException("삭제할 설문조사가 존재하지 않습니다: " + loginId); + } + surveyRepository.delete(survey); + } + +/* @Transactional + public SurveyResDto update(Long surveyId, SurveySaveReqDto surveySaveReqDto, Principal principal) { + String loginId = principal.getName(); + Survey survey = surveyRepository.findById(surveyId) + .orElseThrow(() -> new IllegalArgumentException("설문조사 결과를 찾을 수 없습니다: " + surveyId)); + + if (!survey.getUser().getLoginId().equals(loginId)) { + throw new IllegalArgumentException("해당 설문조사를 수정할 권한이 없습니다."); + } + + Emotion emotion = emotionRepository.findById(surveySaveReqDto.emotionId()) + .orElseThrow(() -> new IllegalArgumentException("해당 감정을 찾을 수 없습니다: " + surveySaveReqDto.emotionId())); + + List colors = colorRepository.findAllById(surveySaveReqDto.colorIds()); + + + survey.update(surveySaveReqDto.score(), emotion, colors); + + surveyRepository.save(survey); + + List colorComments = colors.stream() + .map(Color::getComment) + .toList(); + return SurveyResDto.from(survey, colorComments); + } + + public SurveyResDto findById(Long surveyId) { + Survey survey = surveyRepository.findById(surveyId) + .orElseThrow(() -> new IllegalArgumentException("설문조사 결과를 찾을 수 없습니다: " + surveyId)); + + List colors = survey.getColors(); + List colorComments = colors.stream() + .map(Color::getComment) + .toList(); + + return SurveyResDto.from(survey, colorComments); + }*/ } diff --git a/src/main/java/net/skhu/likelion12thteam03be/survey/domain/Survey.java b/src/main/java/net/skhu/likelion12thteam03be/survey/domain/Survey.java index 30e02a8..58c2af4 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/survey/domain/Survey.java +++ b/src/main/java/net/skhu/likelion12thteam03be/survey/domain/Survey.java @@ -1,11 +1,12 @@ package net.skhu.likelion12thteam03be.survey.domain; +import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; -import net.skhu.likelion12thteam03be.SelectColor.domain.SelectColor; +import net.skhu.likelion12thteam03be.color.domian.Color; import net.skhu.likelion12thteam03be.emotion.domain.Emotion; import net.skhu.likelion12thteam03be.user.domain.User; @@ -21,24 +22,35 @@ public class Survey { private Long id; @ManyToOne + @JsonIgnore @JoinColumn(name = "user_id") private User user; @ManyToOne - @JoinColumn(name = "emotion_id") // 외래키 join - Emotion emotion; + @JoinColumn(name = "emotion_id") + private Emotion emotion; - @OneToMany(mappedBy = "survey") // color랑 manytomany 중간테이블 생성 - private List selectColorList; + @ManyToMany + @JoinTable( + name = "survey_colors", + joinColumns = @JoinColumn(name = "survey_id"), + inverseJoinColumns = @JoinColumn(name = "color_id") + ) + private List colors; private int score; @Builder - public Survey(Long id, User user, Emotion emotion, List selectColorList, int score) { - this.id = id; - this.user = user; + public Survey(int score, Emotion emotion, List colors, User user) { + this.score = score; this.emotion = emotion; - this.selectColorList = selectColorList; + this.colors = colors; + this.user = user; + } + + public void update(int score, Emotion emotion, List colors) { this.score = score; + this.emotion = emotion; + this.colors = colors; } } diff --git a/src/main/java/net/skhu/likelion12thteam03be/survey/domain/repository/SurveyRepository.java b/src/main/java/net/skhu/likelion12thteam03be/survey/domain/repository/SurveyRepository.java index 82809e4..c9a4bff 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/survey/domain/repository/SurveyRepository.java +++ b/src/main/java/net/skhu/likelion12thteam03be/survey/domain/repository/SurveyRepository.java @@ -4,4 +4,7 @@ import org.springframework.data.jpa.repository.JpaRepository; public interface SurveyRepository extends JpaRepository { + boolean existsByUserLoginId(String loginId); + + Survey findByUserLoginId(String loginId); } diff --git a/src/main/java/net/skhu/likelion12thteam03be/survey/exception/SurveyAlreadyExistsException.java b/src/main/java/net/skhu/likelion12thteam03be/survey/exception/SurveyAlreadyExistsException.java new file mode 100644 index 0000000..8509c65 --- /dev/null +++ b/src/main/java/net/skhu/likelion12thteam03be/survey/exception/SurveyAlreadyExistsException.java @@ -0,0 +1,7 @@ +package net.skhu.likelion12thteam03be.survey.exception; + +public class SurveyAlreadyExistsException extends RuntimeException { + public SurveyAlreadyExistsException(String message) { + super(message); + } +} diff --git a/src/main/java/net/skhu/likelion12thteam03be/user/domain/User.java b/src/main/java/net/skhu/likelion12thteam03be/user/domain/User.java index 863973e..dd3405f 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/user/domain/User.java +++ b/src/main/java/net/skhu/likelion12thteam03be/user/domain/User.java @@ -35,7 +35,7 @@ public class User { private Role role; @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) - private List surveys; + private List surveys = new ArrayList<>(); @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) private List posts = new ArrayList<>();