Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deploy: Answer 중복 저장 문제 CASCADE로 해결 #88

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/com/gamsa/review/domain/Answer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@Builder
public class Answer {
private Long answerId;
private Long reviewId;
private double score;
private Question question;
}
17 changes: 7 additions & 10 deletions src/main/java/com/gamsa/review/service/ReviewService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.gamsa.review.domain.Review;
import com.gamsa.review.dto.ReviewResponse;
import com.gamsa.review.dto.ReviewSaveRequest;
import com.gamsa.review.repository.AnswerRepository;
import com.gamsa.review.repository.QuestionRepository;
import com.gamsa.review.repository.ReviewRepository;
import com.gamsa.user.repository.UserRepository;
Expand All @@ -28,7 +27,6 @@ public class ReviewService {
private final UserRepository userRepository;
private final QuestionRepository questionRepository;
private final ReviewRepository reviewRepository;
private final AnswerRepository answerRepository;
private final HistoryRepository historyRepository;

@Transactional
Expand All @@ -51,17 +49,16 @@ public void saveReview(Long userId, ReviewSaveRequest saveRequest) {
List<Answer> answers = saveRequest.getAnswers().stream()
.map(answer -> {
Question question = questionRepository.findById(answer.getQuestionId())
.orElseThrow(() -> new NoSuchElementException("존재하지 않는 질문"));
.orElseThrow(() -> new NoSuchElementException("존재하지 않는 질문"));

return Answer.builder()
.question(question)
.score(answer.getScore())
.build();
.question(question)
.reviewId(null)
.score(answer.getScore())
.build();
})
.toList();

answers.forEach(answerRepository::save);

// save review
Review review = Review.builder()
.activity(activity)
Expand All @@ -73,7 +70,7 @@ public void saveReview(Long userId, ReviewSaveRequest saveRequest) {
reviewRepository.save(review);
}

public BigDecimal getAverageScore(long instituteId, int questionId) {
public BigDecimal getAverageScore(Long instituteId, int questionId) {
OptionalDouble averageScore = reviewRepository.findReviews(instituteId, questionId)
.stream()
.flatMap(review -> review.getAnswers().stream())
Expand All @@ -88,7 +85,7 @@ public BigDecimal getAverageScore(long instituteId, int questionId) {
}
}

public List<ReviewResponse> getReview(long historyId) {
public List<ReviewResponse> getReview(Long historyId) {

return reviewRepository.findHistoryReview(historyId)
.orElseThrow(NoSuchElementException::new)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.gamsa.activity.service;

import static org.assertj.core.api.Assertions.assertThat;

import com.gamsa.activity.constant.ActivityErrorCode;
import com.gamsa.activity.dto.ActivityDetailResponse;
import com.gamsa.activity.dto.ActivityFindSliceResponse;
Expand All @@ -9,7 +11,6 @@
import com.gamsa.history.stub.StubHistoryRepository;
import com.gamsa.review.service.QuestionService;
import com.gamsa.review.service.ReviewService;
import com.gamsa.review.stub.StubAnswerRepository;
import com.gamsa.review.stub.StubQuestionRepository;
import com.gamsa.review.stub.StubReviewRepository;
import com.gamsa.user.stub.StubExistsUserRepository;
Expand All @@ -18,8 +19,6 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;

import static org.assertj.core.api.Assertions.assertThat;

class ActivityServiceTest {


Expand All @@ -35,7 +34,6 @@ class ActivityServiceTest {
new StubExistsUserRepository(),
new StubQuestionRepository(),
new StubReviewRepository(),
new StubAnswerRepository(),
new StubHistoryRepository()
)
);
Expand All @@ -60,7 +58,6 @@ class ActivityServiceTest {
new StubExistsUserRepository(),
new StubQuestionRepository(),
new StubReviewRepository(),
new StubAnswerRepository(),
new StubHistoryRepository()
)
);
Expand All @@ -84,7 +81,6 @@ class ActivityServiceTest {
new StubExistsUserRepository(),
new StubQuestionRepository(),
new StubReviewRepository(),
new StubAnswerRepository(),
new StubHistoryRepository()
)
);
Expand Down
Loading