Skip to content

Commit

Permalink
Merge branch 'psy-test'
Browse files Browse the repository at this point in the history
  • Loading branch information
shinheekim committed Aug 5, 2024
2 parents 994eee1 + 12ffc53 commit 96716ad
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 87 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
}
}
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package net.skhu.likelion12thteam03be.SurveyColor;

import org.springframework.data.jpa.repository.JpaRepository;

public interface SurveyColorRepository extends JpaRepository<SurveyColor, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,6 +23,9 @@ public class Color {
private String mood;
private String comment;

@OneToMany(mappedBy = "color")
private List<SurveyColor> colors = new ArrayList<>();

@Builder
public Color(Long id, String name, String mood, String comment) {
this.id = id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@
public class SurveyController {
private final SurveyService surveyService;
@PostMapping
public ResponseEntity<SurveyResDto> createSurvey(@RequestBody SurveySaveReqDto reqDto, Principal principal) {
SurveyResDto surveyResDto = surveyService.save(reqDto, principal);
return new ResponseEntity<>(surveyResDto, HttpStatus.CREATED);
public ResponseEntity<String> createSurvey(@RequestBody SurveySaveReqDto reqDto, Principal principal) {
surveyService.save(reqDto, principal);
return new ResponseEntity<>("설문조사 저장 완료!", HttpStatus.CREATED);
}

@GetMapping
public ResponseEntity<SurveyResDto> findByIdSurvey(Principal principal) {
SurveyResDto data = surveyService.findByLoginId(principal);
return new ResponseEntity<>(data, HttpStatus.OK);
}

@DeleteMapping
Expand All @@ -27,19 +33,10 @@ public ResponseEntity<String> deleteSurvey(Principal principal) {
return new ResponseEntity<>("설문조사 결과가 정상적으로 삭제되었습니다.", HttpStatus.OK);
}

/* @PatchMapping("/{surveyId}")
public ResponseEntity<SurveyResDto> 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<String> updateSurvey(
@RequestBody SurveySaveReqDto surveySaveReqDto, Principal principal) {
surveyService.update(surveySaveReqDto, principal);
return new ResponseEntity<>("설문조사 수정 완료!", HttpStatus.OK);
}
@GetMapping("/{surveyId}")
public ResponseEntity<SurveyResDto> findByIdSurvey(@PathVariable Long surveyId) {
SurveyResDto response = surveyService.findById(surveyId);
return new ResponseEntity<>(response, HttpStatus.OK);
}*/

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -13,12 +14,13 @@ public record SurveyResDto(
List<String> colorComments, // request 받을 때: List<Long> colorIds,
List<Post> postList
) {
public static SurveyResDto from(Survey survey, List<String> colorComments) {
public static SurveyResDto from (Survey survey, List<String> colorComments) {
List<Post> safePostList = Collections.emptyList(); // post는 잠시 비워둘 예정
return SurveyResDto.builder()
.animalPic(survey.getEmotion().getAnimalPic())
.type(survey.getEmotion().getType())
.colorComments(colorComments)
.postList(survey.getUser().getPosts())
.postList(safePostList)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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())
Expand All @@ -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<String> colorComments = colors.stream()
.map(Color::getComment)
List<SurveyColor> 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<SurveyColor> surveyColors = survey.getColors();
List<String> colorComments = surveyColors .stream()
.map(surveyColor -> surveyColor.getColor().getComment())
.toList();

return SurveyResDto.from(survey, colorComments);
Expand All @@ -58,48 +78,33 @@ 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("해당 설문조사를 수정할 권한이 없습니다.");
}

Emotion emotion = emotionRepository.findById(surveySaveReqDto.emotionId())
.orElseThrow(() -> new IllegalArgumentException("해당 감정을 찾을 수 없습니다: " + surveySaveReqDto.emotionId()));
List<Color> colors = colorRepository.findAllById(surveySaveReqDto.colorIds());

survey.update(surveySaveReqDto.score(), emotion, colors);
List<SurveyColor> surveyColors = colors.stream()
.map(color -> SurveyColor.builder()
.survey(survey)
.color(color)
.build())
.toList();
survey.update(surveySaveReqDto.score(), emotion, surveyColors);

surveyRepository.save(survey);
List<String> 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<Color> colors = survey.getColors();
List<String> colorComments = colors.stream()
.map(Color::getComment)
.toList();
return SurveyResDto.from(survey, colorComments);
}*/
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,35 +22,30 @@ public class Survey {
private Long id;

@ManyToOne
@JsonIgnore
@JoinColumn(name = "user_id")
private User user;

@ManyToOne
@JoinColumn(name = "emotion_id")
private Emotion emotion;

@ManyToMany
@JoinTable(
name = "survey_colors",
joinColumns = @JoinColumn(name = "survey_id"),
inverseJoinColumns = @JoinColumn(name = "color_id")
)
private List<Color> colors;
@OneToMany(mappedBy = "survey", cascade = CascadeType.ALL, orphanRemoval = true)
private List<SurveyColor> colors = new ArrayList<>();

private int score;

@Builder
public Survey(int score, Emotion emotion, List<Color> colors, User user) {
public Survey(int score, Emotion emotion, List<SurveyColor> colors, User user) {
this.score = score;
this.emotion = emotion;
this.colors = colors;
this.user = user;
}

public void update(int score, Emotion emotion, List<Color> colors) {
public void update(int score, Emotion emotion, List<SurveyColor> colors) {
this.score = score;
this.emotion = emotion;
this.colors = colors;
this.colors.clear();
this.colors.addAll(colors);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Survey, Long> {
boolean existsByUserLoginId(String loginId);

Survey findByUserLoginId(String loginId);
Optional<Survey> findByUserLoginId(String loginId);
}

0 comments on commit 96716ad

Please sign in to comment.