Skip to content

Commit

Permalink
feat: 심리테스트 수정(#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
shinheekim committed Aug 5, 2024
1 parent c06a6d3 commit b829c1a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@

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

import java.util.List;

public interface SurveyColorRepository extends JpaRepository<SurveyColor, Long> {
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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) {
Expand All @@ -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<Color> colors = colorRepository.findAllById(surveySaveReqDto.colorIds());
List<SurveyColor> surveyColors = colors.stream()
.map(color -> SurveyColor.builder()
.survey(survey)
.color(color)
.build())
.toList();

survey.update(surveySaveReqDto.score(), emotion, surveyColors);
surveyRepository.save(survey);
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -36,21 +34,19 @@ public class Survey {

private int score;

public void setColors(List<SurveyColor> colors) {
this.colors = colors;
}

@Builder
public Survey(int score, Emotion emotion, List<SurveyColor> 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<SurveyColor> colors) {
this.score = score;
this.emotion = emotion;
this.colors = colors;
if (colors != null) {
this.colors.addAll(colors);
}
}
}

0 comments on commit b829c1a

Please sign in to comment.