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 deleted file mode 100644 index 18f218d..0000000 --- a/src/main/java/net/skhu/likelion12thteam03be/SelectColor/domain/repository/SelectColorRepository.java +++ /dev/null @@ -1,9 +0,0 @@ -/* -package net.skhu.likelion12thteam03be.SelectColor.domain.repository; - -import net.skhu.likelion12thteam03be.SelectColor.domain.SelectColor; -import org.springframework.data.jpa.repository.JpaRepository; - -public interface SelectColorRepository extends JpaRepository { -} -*/ diff --git a/src/main/java/net/skhu/likelion12thteam03be/SelectColor/domain/SelectColor.java b/src/main/java/net/skhu/likelion12thteam03be/SurveyColor/SurveyColor.java similarity index 59% rename from src/main/java/net/skhu/likelion12thteam03be/SelectColor/domain/SelectColor.java rename to src/main/java/net/skhu/likelion12thteam03be/SurveyColor/SurveyColor.java index 5e8b608..928903d 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/SelectColor/domain/SelectColor.java +++ b/src/main/java/net/skhu/likelion12thteam03be/SurveyColor/SurveyColor.java @@ -1,36 +1,29 @@ -/* -package net.skhu.likelion12thteam03be.SelectColor.domain; +package net.skhu.likelion12thteam03be.SurveyColor; import jakarta.persistence.*; -import lombok.AccessLevel; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; +import lombok.*; import net.skhu.likelion12thteam03be.color.domian.Color; import net.skhu.likelion12thteam03be.survey.domain.Survey; @Entity @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) -public class SelectColor { +public class SurveyColor { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "selectColor_id") private Long id; - @ManyToOne + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "survey_id") private Survey survey; - @ManyToOne + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "color_id") private Color color; @Builder - public SelectColor(Long id, Survey survey, Color Color) { - this.id = id; + public SurveyColor(Survey survey, Color color) { this.survey = survey; this.color = color; } } -*/ diff --git a/src/main/java/net/skhu/likelion12thteam03be/SurveyColor/SurveyColorRepository.java b/src/main/java/net/skhu/likelion12thteam03be/SurveyColor/SurveyColorRepository.java new file mode 100644 index 0000000..804523a --- /dev/null +++ b/src/main/java/net/skhu/likelion12thteam03be/SurveyColor/SurveyColorRepository.java @@ -0,0 +1,6 @@ +package net.skhu.likelion12thteam03be.SurveyColor; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface SurveyColorRepository extends JpaRepository { +} diff --git a/src/main/java/net/skhu/likelion12thteam03be/color/domian/Color.java b/src/main/java/net/skhu/likelion12thteam03be/color/domian/Color.java index ba2c4ca..dbf934c 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/color/domian/Color.java +++ b/src/main/java/net/skhu/likelion12thteam03be/color/domian/Color.java @@ -5,6 +5,10 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import net.skhu.likelion12thteam03be.SurveyColor.SurveyColor; + +import java.util.ArrayList; +import java.util.List; @Entity @Getter @@ -19,6 +23,9 @@ public class Color { private String mood; private String comment; + @OneToMany(mappedBy = "color") + private List colors = new ArrayList<>(); + @Builder public Color(Long id, String name, String mood, String comment) { this.id = id; 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 b6f8515..19dfcd9 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/survey/api/SurveyController.java +++ b/src/main/java/net/skhu/likelion12thteam03be/survey/api/SurveyController.java @@ -16,9 +16,15 @@ 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); + public ResponseEntity createSurvey(@RequestBody SurveySaveReqDto reqDto, Principal principal) { + surveyService.save(reqDto, principal); + return new ResponseEntity<>("설문조사 저장 완료!", HttpStatus.CREATED); + } + + @GetMapping + public ResponseEntity findByIdSurvey(Principal principal) { + SurveyResDto data = surveyService.findByLoginId(principal); + return new ResponseEntity<>(data, HttpStatus.OK); } @DeleteMapping @@ -27,19 +33,10 @@ public ResponseEntity deleteSurvey(Principal 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); + @PatchMapping + public ResponseEntity updateSurvey( + @RequestBody SurveySaveReqDto surveySaveReqDto, Principal principal) { + surveyService.update(surveySaveReqDto, principal); + return new ResponseEntity<>("설문조사 수정 완료!", 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/response/SurveyResDto.java b/src/main/java/net/skhu/likelion12thteam03be/survey/api/response/SurveyResDto.java index 1c3de0d..567e06d 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/survey/api/response/SurveyResDto.java +++ b/src/main/java/net/skhu/likelion12thteam03be/survey/api/response/SurveyResDto.java @@ -4,6 +4,7 @@ import net.skhu.likelion12thteam03be.post.domain.Post; import net.skhu.likelion12thteam03be.survey.domain.Survey; +import java.util.Collections; import java.util.List; @Builder @@ -13,12 +14,13 @@ public record SurveyResDto( List colorComments, // request 받을 때: List colorIds, List postList ) { - public static SurveyResDto from(Survey survey, List colorComments) { + public static SurveyResDto from (Survey survey, List colorComments) { + List safePostList = Collections.emptyList(); // post는 잠시 비워둘 예정 return SurveyResDto.builder() .animalPic(survey.getEmotion().getAnimalPic()) .type(survey.getEmotion().getType()) .colorComments(colorComments) - .postList(survey.getUser().getPosts()) + .postList(safePostList) .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 1a0ba76..1057b52 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/survey/application/SurveyService.java +++ b/src/main/java/net/skhu/likelion12thteam03be/survey/application/SurveyService.java @@ -1,7 +1,10 @@ package net.skhu.likelion12thteam03be.survey.application; +import com.amazonaws.services.kms.model.NotFoundException; import lombok.RequiredArgsConstructor; +import net.skhu.likelion12thteam03be.SurveyColor.SurveyColor; +import net.skhu.likelion12thteam03be.SurveyColor.SurveyColorRepository; import net.skhu.likelion12thteam03be.color.domian.Color; import net.skhu.likelion12thteam03be.color.domian.repository.ColorRepository; import net.skhu.likelion12thteam03be.emotion.domain.Emotion; @@ -27,13 +30,15 @@ public class SurveyService { private final UserRepository userRepository; private final EmotionRepository emotionRepository; private final ColorRepository colorRepository; + private final SurveyColorRepository surveyColorRepository; @Transactional - public SurveyResDto save(SurveySaveReqDto surveySaveReqDto, Principal principal) { + public void 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()) @@ -43,13 +48,28 @@ public SurveyResDto save(SurveySaveReqDto surveySaveReqDto, Principal principal) Survey survey = Survey.builder() .score(surveySaveReqDto.score()) .emotion(emotion) - .colors(colors) .user(user) .build(); surveyRepository.save(survey); - List colorComments = colors.stream() - .map(Color::getComment) + List surveyColors = colors.stream() + .map(color -> SurveyColor.builder() + .survey(survey) + .color(color) + .build()) + .toList(); + surveyColorRepository.saveAll(surveyColors); + } + + public SurveyResDto findByLoginId(Principal principal) { + String loginId = principal.getName(); + Survey survey = surveyRepository.findByUserLoginId(loginId) + .orElseThrow(() -> new NotFoundException("설문조사 결과를 찾을 수 없습니다.")); + + + List surveyColors = survey.getColors(); + List colorComments = surveyColors .stream() + .map(surveyColor -> surveyColor.getColor().getComment()) .toList(); return SurveyResDto.from(survey, colorComments); @@ -58,18 +78,16 @@ public SurveyResDto save(SurveySaveReqDto surveySaveReqDto, Principal principal) @Transactional public void delete(Principal principal) { String loginId = principal.getName(); - Survey survey = surveyRepository.findByUserLoginId(loginId); - if (survey == null) { - throw new IllegalArgumentException("삭제할 설문조사가 존재하지 않습니다: " + loginId); - } + Survey survey = surveyRepository.findByUserLoginId(loginId) + .orElseThrow(() -> new NotFoundException("설문조사 결과를 찾을 수 없습니다.")); surveyRepository.delete(survey); } -/* @Transactional - public SurveyResDto update(Long surveyId, SurveySaveReqDto surveySaveReqDto, Principal principal) { + @Transactional + public void update(SurveySaveReqDto surveySaveReqDto, Principal principal) { String loginId = principal.getName(); - Survey survey = surveyRepository.findById(surveyId) - .orElseThrow(() -> new IllegalArgumentException("설문조사 결과를 찾을 수 없습니다: " + surveyId)); + Survey survey = surveyRepository.findByUserLoginId(loginId) + .orElseThrow(() -> new IllegalArgumentException("설문조사 결과를 찾을 수 없습니다: " + loginId)); if (!survey.getUser().getLoginId().equals(loginId)) { throw new IllegalArgumentException("해당 설문조사를 수정할 권한이 없습니다."); @@ -77,29 +95,16 @@ public SurveyResDto update(Long surveyId, SurveySaveReqDto surveySaveReqDto, Pri Emotion emotion = emotionRepository.findById(surveySaveReqDto.emotionId()) .orElseThrow(() -> new IllegalArgumentException("해당 감정을 찾을 수 없습니다: " + surveySaveReqDto.emotionId())); - List colors = colorRepository.findAllById(surveySaveReqDto.colorIds()); - - survey.update(surveySaveReqDto.score(), emotion, colors); + List surveyColors = colors.stream() + .map(color -> SurveyColor.builder() + .survey(survey) + .color(color) + .build()) + .toList(); + survey.update(surveySaveReqDto.score(), emotion, surveyColors); 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 58c2af4..a533632 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/survey/domain/Survey.java +++ b/src/main/java/net/skhu/likelion12thteam03be/survey/domain/Survey.java @@ -1,15 +1,15 @@ 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.color.domian.Color; +import net.skhu.likelion12thteam03be.SurveyColor.SurveyColor; import net.skhu.likelion12thteam03be.emotion.domain.Emotion; import net.skhu.likelion12thteam03be.user.domain.User; +import java.util.ArrayList; import java.util.List; @Entity @@ -22,7 +22,6 @@ public class Survey { private Long id; @ManyToOne - @JsonIgnore @JoinColumn(name = "user_id") private User user; @@ -30,27 +29,23 @@ public class Survey { @JoinColumn(name = "emotion_id") private Emotion emotion; - @ManyToMany - @JoinTable( - name = "survey_colors", - joinColumns = @JoinColumn(name = "survey_id"), - inverseJoinColumns = @JoinColumn(name = "color_id") - ) - private List colors; + @OneToMany(mappedBy = "survey", cascade = CascadeType.ALL, orphanRemoval = true) + private List colors = new ArrayList<>(); private int score; @Builder - public Survey(int score, Emotion emotion, List colors, User user) { + public Survey(int score, Emotion emotion, List colors, User user) { this.score = score; this.emotion = emotion; this.colors = colors; this.user = user; } - public void update(int score, Emotion emotion, List colors) { + public void update(int score, Emotion emotion, List colors) { this.score = score; this.emotion = emotion; - this.colors = colors; + this.colors.clear(); + this.colors.addAll(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 c9a4bff..1bd7b3a 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 @@ -3,8 +3,10 @@ import net.skhu.likelion12thteam03be.survey.domain.Survey; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.Optional; + public interface SurveyRepository extends JpaRepository { boolean existsByUserLoginId(String loginId); - Survey findByUserLoginId(String loginId); + Optional findByUserLoginId(String loginId); }