From 0180bbf8ae1c5b55874945f76a776f3c352cc5f9 Mon Sep 17 00:00:00 2001 From: shinheekim Date: Sun, 4 Aug 2024 21:51:13 +0900 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20=EC=8B=AC=EB=A6=AC=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=83=9D=EC=84=B1,=20=EC=A1=B0=ED=9A=8C?= =?UTF-8?q?=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SelectColor/domain/SelectColor.java | 36 ------------------- .../repository/SelectColorRepository.java | 9 ----- .../survey/api/SurveyController.java | 21 ++++++----- .../survey/api/response/SurveyResDto.java | 6 ++-- .../survey/application/SurveyService.java | 21 +++++------ .../survey/domain/Survey.java | 3 +- 6 files changed, 24 insertions(+), 72 deletions(-) delete mode 100644 src/main/java/net/skhu/likelion12thteam03be/SelectColor/domain/SelectColor.java delete mode 100644 src/main/java/net/skhu/likelion12thteam03be/SelectColor/domain/repository/SelectColorRepository.java diff --git a/src/main/java/net/skhu/likelion12thteam03be/SelectColor/domain/SelectColor.java b/src/main/java/net/skhu/likelion12thteam03be/SelectColor/domain/SelectColor.java deleted file mode 100644 index 5e8b608..0000000 --- a/src/main/java/net/skhu/likelion12thteam03be/SelectColor/domain/SelectColor.java +++ /dev/null @@ -1,36 +0,0 @@ -/* -package net.skhu.likelion12thteam03be.SelectColor.domain; - -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.survey.domain.Survey; - -@Entity -@Getter -@NoArgsConstructor(access = AccessLevel.PROTECTED) -public class SelectColor { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "selectColor_id") - private Long id; - - @ManyToOne - @JoinColumn(name = "survey_id") - private Survey survey; - - @ManyToOne - @JoinColumn(name = "color_id") - private Color color; - - @Builder - public SelectColor(Long id, Survey survey, Color Color) { - this.id = id; - this.survey = survey; - 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 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/survey/api/SurveyController.java b/src/main/java/net/skhu/likelion12thteam03be/survey/api/SurveyController.java index b6f8515..4d8d34b 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,7 +33,7 @@ public ResponseEntity deleteSurvey(Principal principal) { return new ResponseEntity<>("설문조사 결과가 정상적으로 삭제되었습니다.", HttpStatus.OK); } -/* @PatchMapping("/{surveyId}") + @PatchMapping("/{surveyId}") public ResponseEntity updateSurvey( @PathVariable Long surveyId, @RequestBody SurveySaveReqDto surveySaveReqDto, @@ -35,11 +41,4 @@ public ResponseEntity updateSurvey( 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/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..f773db1 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/survey/application/SurveyService.java +++ b/src/main/java/net/skhu/likelion12thteam03be/survey/application/SurveyService.java @@ -29,7 +29,7 @@ public class SurveyService { private final ColorRepository colorRepository; @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); @@ -47,7 +47,13 @@ public SurveyResDto save(SurveySaveReqDto surveySaveReqDto, Principal principal) .user(user) .build(); surveyRepository.save(survey); + } + + public SurveyResDto findByLoginId(Principal principal) { + String loginId = principal.getName(); + Survey survey = surveyRepository.findByUserLoginId(loginId); + List colors = survey.getColors(); List colorComments = colors.stream() .map(Color::getComment) .toList(); @@ -65,7 +71,7 @@ public void delete(Principal principal) { surveyRepository.delete(survey); } -/* @Transactional + @Transactional public SurveyResDto update(Long surveyId, SurveySaveReqDto surveySaveReqDto, Principal principal) { String loginId = principal.getName(); Survey survey = surveyRepository.findById(surveyId) @@ -91,15 +97,4 @@ public SurveyResDto update(Long surveyId, SurveySaveReqDto surveySaveReqDto, Pri 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..c7a3f1d 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/survey/domain/Survey.java +++ b/src/main/java/net/skhu/likelion12thteam03be/survey/domain/Survey.java @@ -10,6 +10,7 @@ import net.skhu.likelion12thteam03be.emotion.domain.Emotion; import net.skhu.likelion12thteam03be.user.domain.User; +import java.util.ArrayList; import java.util.List; @Entity @@ -36,7 +37,7 @@ public class Survey { joinColumns = @JoinColumn(name = "survey_id"), inverseJoinColumns = @JoinColumn(name = "color_id") ) - private List colors; + private List colors = new ArrayList<>(); private int score; From c06a6d3ded0bde947c50d0085b092ee247ae28d8 Mon Sep 17 00:00:00 2001 From: shinheekim Date: Mon, 5 Aug 2024 22:29:21 +0900 Subject: [PATCH 2/4] =?UTF-8?q?feat:=20=EC=8B=AC=EB=A6=AC=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=83=9D=EC=84=B1,=20=EC=A1=B0=ED=9A=8C,?= =?UTF-8?q?=20=EC=82=AD=EC=A0=9C=20=EC=B5=9C=EC=A2=85=20=EC=99=84=EB=A3=8C?= =?UTF-8?q?(#6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SurveyColor/SurveyColor.java | 29 ++++++++++ .../SurveyColor/SurveyColorRepository.java | 8 +++ .../color/domian/Color.java | 8 +++ .../survey/api/SurveyController.java | 4 +- .../survey/application/SurveyService.java | 58 +++++++------------ .../survey/domain/Survey.java | 19 +++--- .../domain/repository/SurveyRepository.java | 4 +- 7 files changed, 81 insertions(+), 49 deletions(-) create mode 100644 src/main/java/net/skhu/likelion12thteam03be/SurveyColor/SurveyColor.java create mode 100644 src/main/java/net/skhu/likelion12thteam03be/SurveyColor/SurveyColorRepository.java diff --git a/src/main/java/net/skhu/likelion12thteam03be/SurveyColor/SurveyColor.java b/src/main/java/net/skhu/likelion12thteam03be/SurveyColor/SurveyColor.java new file mode 100644 index 0000000..928903d --- /dev/null +++ b/src/main/java/net/skhu/likelion12thteam03be/SurveyColor/SurveyColor.java @@ -0,0 +1,29 @@ +package net.skhu.likelion12thteam03be.SurveyColor; + +import jakarta.persistence.*; +import lombok.*; +import net.skhu.likelion12thteam03be.color.domian.Color; +import net.skhu.likelion12thteam03be.survey.domain.Survey; + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class SurveyColor { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "survey_id") + private Survey survey; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "color_id") + private Color color; + + @Builder + 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..c10c73a --- /dev/null +++ b/src/main/java/net/skhu/likelion12thteam03be/SurveyColor/SurveyColorRepository.java @@ -0,0 +1,8 @@ +package net.skhu.likelion12thteam03be.SurveyColor; + +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +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..805f21b 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/color/domian/Color.java +++ b/src/main/java/net/skhu/likelion12thteam03be/color/domian/Color.java @@ -1,10 +1,15 @@ package net.skhu.likelion12thteam03be.color.domian; +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.SurveyColor.SurveyColor; + +import java.util.ArrayList; +import java.util.List; @Entity @Getter @@ -19,6 +24,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 4d8d34b..82b0937 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/survey/api/SurveyController.java +++ b/src/main/java/net/skhu/likelion12thteam03be/survey/api/SurveyController.java @@ -33,12 +33,12 @@ public ResponseEntity deleteSurvey(Principal principal) { return new ResponseEntity<>("설문조사 결과가 정상적으로 삭제되었습니다.", HttpStatus.OK); } - @PatchMapping("/{surveyId}") +/* @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); - } + }*/ } \ No newline at end of file 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 f773db1..d17a4b2 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,6 +30,7 @@ public class SurveyService { private final UserRepository userRepository; private final EmotionRepository emotionRepository; private final ColorRepository colorRepository; + private final SurveyColorRepository surveyColorRepository; @Transactional public void save(SurveySaveReqDto surveySaveReqDto, Principal principal) { @@ -43,19 +47,30 @@ public void save(SurveySaveReqDto surveySaveReqDto, Principal principal) { Survey survey = Survey.builder() .score(surveySaveReqDto.score()) .emotion(emotion) - .colors(colors) .user(user) .build(); surveyRepository.save(survey); + + List surveyColors = colors.stream() + .map(color -> SurveyColor.builder() + .survey(survey) + .color(color) + .build()) + .toList(); + + surveyColorRepository.saveAll(surveyColors); + survey.setColors(surveyColors); } public SurveyResDto findByLoginId(Principal principal) { String loginId = principal.getName(); - Survey survey = surveyRepository.findByUserLoginId(loginId); + Survey survey = surveyRepository.findByUserLoginId(loginId) + .orElseThrow(() -> new NotFoundException("설문조사 결과를 찾을 수 없습니다.")); + - List colors = survey.getColors(); - List colorComments = colors.stream() - .map(Color::getComment) + List surveyColors = survey.getColors(); + List colorComments = surveyColors .stream() + .map(surveyColor -> surveyColor.getColor().getComment()) .toList(); return SurveyResDto.from(survey, colorComments); @@ -64,37 +79,8 @@ public SurveyResDto findByLoginId(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) { - 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); - } - } 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 c7a3f1d..068723b 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/survey/domain/Survey.java +++ b/src/main/java/net/skhu/likelion12thteam03be/survey/domain/Survey.java @@ -6,6 +6,7 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import net.skhu.likelion12thteam03be.SurveyColor.SurveyColor; import net.skhu.likelion12thteam03be.color.domian.Color; import net.skhu.likelion12thteam03be.emotion.domain.Emotion; import net.skhu.likelion12thteam03be.user.domain.User; @@ -23,7 +24,6 @@ public class Survey { private Long id; @ManyToOne - @JsonIgnore @JoinColumn(name = "user_id") private User user; @@ -31,25 +31,24 @@ 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 = new ArrayList<>(); + @OneToMany(mappedBy = "survey", cascade = CascadeType.ALL, orphanRemoval = true) + private List colors = new ArrayList<>(); private int score; + public void setColors(List colors) { + this.colors = colors; + } + @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; 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); } From b829c1a0f49eb97b1d19974158c7b38326f27ef9 Mon Sep 17 00:00:00 2001 From: shinheekim Date: Mon, 5 Aug 2024 23:56:17 +0900 Subject: [PATCH 3/4] =?UTF-8?q?feat:=20=EC=8B=AC=EB=A6=AC=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=88=98=EC=A0=95(#6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SurveyColor/SurveyColorRepository.java | 2 -- .../color/domian/Color.java | 1 - .../survey/api/SurveyController.java | 14 ++++------ .../survey/application/SurveyService.java | 28 +++++++++++++++++-- .../survey/domain/Survey.java | 12 +++----- 5 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/main/java/net/skhu/likelion12thteam03be/SurveyColor/SurveyColorRepository.java b/src/main/java/net/skhu/likelion12thteam03be/SurveyColor/SurveyColorRepository.java index c10c73a..804523a 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/SurveyColor/SurveyColorRepository.java +++ b/src/main/java/net/skhu/likelion12thteam03be/SurveyColor/SurveyColorRepository.java @@ -2,7 +2,5 @@ import org.springframework.data.jpa.repository.JpaRepository; -import java.util.List; - 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 805f21b..dbf934c 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/color/domian/Color.java +++ b/src/main/java/net/skhu/likelion12thteam03be/color/domian/Color.java @@ -1,6 +1,5 @@ package net.skhu.likelion12thteam03be.color.domian; -import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Builder; 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 82b0937..19dfcd9 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/survey/api/SurveyController.java +++ b/src/main/java/net/skhu/likelion12thteam03be/survey/api/SurveyController.java @@ -33,12 +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); + } } \ No newline at end of file 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 d17a4b2..c8041bb 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/survey/application/SurveyService.java +++ b/src/main/java/net/skhu/likelion12thteam03be/survey/application/SurveyService.java @@ -38,6 +38,7 @@ public void save(SurveySaveReqDto surveySaveReqDto, Principal principal) { if (surveyRepository.existsByUserLoginId(loginId)){ throw new SurveyAlreadyExistsException("이미 설문조사 결과가 존재합니다. 유저 아이디 = " + loginId); } + User user = userRepository.findByLoginId(loginId) .orElseThrow(() -> new IllegalArgumentException("유저 정보를 찾을 수 없습니다: " + loginId)); Emotion emotion = emotionRepository.findById(surveySaveReqDto.emotionId()) @@ -57,9 +58,8 @@ public void save(SurveySaveReqDto surveySaveReqDto, Principal principal) { .color(color) .build()) .toList(); - surveyColorRepository.saveAll(surveyColors); - survey.setColors(surveyColors); + } public SurveyResDto findByLoginId(Principal principal) { @@ -83,4 +83,28 @@ public void delete(Principal principal) { .orElseThrow(() -> new NotFoundException("설문조사 결과를 찾을 수 없습니다.")); surveyRepository.delete(survey); } + + @Transactional + public void update(SurveySaveReqDto surveySaveReqDto, Principal principal) { + String loginId = principal.getName(); + Survey survey = surveyRepository.findByUserLoginId(loginId) + .orElseThrow(() -> new IllegalArgumentException("설문조사 결과를 찾을 수 없습니다: " + loginId)); + + 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()); + List surveyColors = colors.stream() + .map(color -> SurveyColor.builder() + .survey(survey) + .color(color) + .build()) + .toList(); + + survey.update(surveySaveReqDto.score(), emotion, surveyColors); + surveyRepository.save(survey); + } } 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 068723b..8d71aed 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/survey/domain/Survey.java +++ b/src/main/java/net/skhu/likelion12thteam03be/survey/domain/Survey.java @@ -1,13 +1,11 @@ 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.SurveyColor.SurveyColor; -import net.skhu.likelion12thteam03be.color.domian.Color; import net.skhu.likelion12thteam03be.emotion.domain.Emotion; import net.skhu.likelion12thteam03be.user.domain.User; @@ -36,21 +34,19 @@ public class Survey { private int score; - public void setColors(List colors) { - this.colors = colors; - } - @Builder public Survey(int score, Emotion emotion, List colors, User user) { this.score = score; this.emotion = emotion; - this.colors = colors; + this.colors = colors != null ? colors : new ArrayList<>(); this.user = user; } public void update(int score, Emotion emotion, List colors) { this.score = score; this.emotion = emotion; - this.colors = colors; + if (colors != null) { + this.colors.addAll(colors); + } } } From 12ffc53de9d7dad39980cea0bbfe8a6f015e2fde Mon Sep 17 00:00:00 2001 From: shinheekim Date: Tue, 6 Aug 2024 00:33:09 +0900 Subject: [PATCH 4/4] =?UTF-8?q?fix:=20=EC=8B=AC=EB=A6=AC=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=88=98=EC=A0=95->=20colors=EC=AA=BD=20=EC=97=90?= =?UTF-8?q?=EB=9F=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../survey/application/SurveyService.java | 4 ++-- .../skhu/likelion12thteam03be/survey/domain/Survey.java | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) 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 c8041bb..1057b52 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/survey/application/SurveyService.java +++ b/src/main/java/net/skhu/likelion12thteam03be/survey/application/SurveyService.java @@ -59,7 +59,6 @@ public void save(SurveySaveReqDto surveySaveReqDto, Principal principal) { .build()) .toList(); surveyColorRepository.saveAll(surveyColors); - } public SurveyResDto findByLoginId(Principal principal) { @@ -97,14 +96,15 @@ public void update(SurveySaveReqDto surveySaveReqDto, Principal principal) { Emotion emotion = emotionRepository.findById(surveySaveReqDto.emotionId()) .orElseThrow(() -> new IllegalArgumentException("해당 감정을 찾을 수 없습니다: " + surveySaveReqDto.emotionId())); List colors = colorRepository.findAllById(surveySaveReqDto.colorIds()); + List surveyColors = colors.stream() .map(color -> SurveyColor.builder() .survey(survey) .color(color) .build()) .toList(); - survey.update(surveySaveReqDto.score(), emotion, surveyColors); + surveyRepository.save(survey); } } 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 8d71aed..a533632 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/survey/domain/Survey.java +++ b/src/main/java/net/skhu/likelion12thteam03be/survey/domain/Survey.java @@ -38,15 +38,14 @@ public class Survey { public Survey(int score, Emotion emotion, List colors, User user) { this.score = score; this.emotion = emotion; - this.colors = colors != null ? colors : new ArrayList<>(); + this.colors = colors; this.user = user; } public void update(int score, Emotion emotion, List colors) { this.score = score; this.emotion = emotion; - if (colors != null) { - this.colors.addAll(colors); - } + this.colors.clear(); + this.colors.addAll(colors); } }