Skip to content

Commit

Permalink
feat: 심리 테스트 생성 후 조회, 삭제 구현(#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
shinheekim committed Aug 3, 2024
1 parent 2b69894 commit 4e4f18a
Show file tree
Hide file tree
Showing 13 changed files with 245 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/*
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
Expand All @@ -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;
}
}
*/
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/*
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<SelectColor, Long> {
}
*/
Original file line number Diff line number Diff line change
@@ -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);
}*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
행복한, 괴로운, 암울한, 감동적인, 신나는, 편안한, 두려운, 흥미로운, 긴장한, 불안한, 기쁜, 걱정되는, 설레는, 무기력한, 답답한, 상쾌한
}
Original file line number Diff line number Diff line change
@@ -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<SurveyResDto> createSurvey(@RequestBody SurveySaveReqDto reqDto, Principal principal) {
SurveyResDto surveyResDto = surveyService.save(reqDto, principal);
return new ResponseEntity<>(surveyResDto, HttpStatus.CREATED);
}

@DeleteMapping
public ResponseEntity<String> deleteSurvey(Principal principal) {
surveyService.delete(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);
}
@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
@@ -0,0 +1,10 @@
package net.skhu.likelion12thteam03be.survey.api.request;

import java.util.List;

public record SurveySaveReqDto(
Long emotionId,
List<Long> colorIds,
int score
) {
}
Original file line number Diff line number Diff line change
@@ -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<String> colorComments, // request 받을 때: List<Long> colorIds,
List<Post> postList
) {
public static SurveyResDto from(Survey survey, List<String> colorComments) {
return SurveyResDto.builder()
.animalPic(survey.getEmotion().getAnimalPic())
.type(survey.getEmotion().getType())
.colorComments(colorComments)
.postList(survey.getUser().getPosts())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -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<Color> colors = colorRepository.findAllById(surveySaveReqDto.colorIds());

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)
.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<Color> colors = colorRepository.findAllById(surveySaveReqDto.colorIds());
survey.update(surveySaveReqDto.score(), emotion, colors);
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,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;

Expand All @@ -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<SelectColor> selectColorList;
@ManyToMany
@JoinTable(
name = "survey_colors",
joinColumns = @JoinColumn(name = "survey_id"),
inverseJoinColumns = @JoinColumn(name = "color_id")
)
private List<Color> colors;

private int score;

@Builder
public Survey(Long id, User user, Emotion emotion, List<SelectColor> selectColorList, int score) {
this.id = id;
this.user = user;
public Survey(int score, Emotion emotion, List<Color> 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<Color> colors) {
this.score = score;
this.emotion = emotion;
this.colors = colors;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
import org.springframework.data.jpa.repository.JpaRepository;

public interface SurveyRepository extends JpaRepository<Survey, Long> {
boolean existsByUserLoginId(String loginId);

Survey findByUserLoginId(String loginId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.skhu.likelion12thteam03be.survey.exception;

public class SurveyAlreadyExistsException extends RuntimeException {
public SurveyAlreadyExistsException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class User {
private Role role;

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Survey> surveys;
private List<Survey> surveys = new ArrayList<>();

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Post> posts = new ArrayList<>();
Expand Down

0 comments on commit 4e4f18a

Please sign in to comment.