Skip to content

Commit

Permalink
[feat] 리뷰 생성 로직에서 기존 리뷰가 있으면 수정하는 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
syyling committed Mar 28, 2024
1 parent 5b7152d commit 51f9ae6
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

public class KeywordMapper {

public static Keyword mapToKeyword(Review review, String word) {
public static Keyword mapToKeyword(Review review, String word, Long movieId) {
return Keyword.builder()
.review(review)
.word(word)
.movieId(movieId)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
public interface KeywordRepository extends JpaRepository<Keyword, Long> {

List<Keyword> findByReviewId(Long reviewId);

void deleteAllByReviewId(Long reviewId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.mookive.mookive_backend.keyword.domain.service;

import com.mookive.mookive_backend.keyword.domain.entity.Keyword;
import com.mookive.mookive_backend.keyword.domain.repository.KeywordRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@Transactional
public class KeywordDeleteService {

private final KeywordRepository keywordRepository;

public void deleteAllByReviewId(Long reviewId) {
keywordRepository.deleteAllByReviewId(reviewId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@

import com.mookive.mookive_backend.keyword.application.mapper.KeywordMapper;
import com.mookive.mookive_backend.keyword.domain.entity.Keyword;
import com.mookive.mookive_backend.keyword.domain.repository.KeywordRepository;
import com.mookive.mookive_backend.keyword.domain.service.KeywordDeleteService;
import com.mookive.mookive_backend.keyword.domain.service.KeywordSaveService;
import com.mookive.mookive_backend.movie.domain.entity.Movie;
import com.mookive.mookive_backend.movie.domain.service.MovieQueryService;
import com.mookive.mookive_backend.review.application.dto.request.ReviewRequest;
import com.mookive.mookive_backend.review.application.mapper.ReviewMapper;
import com.mookive.mookive_backend.review.domain.entity.Review;
import com.mookive.mookive_backend.review.domain.service.ReviewQueryService;
import com.mookive.mookive_backend.review.domain.service.ReviewSaveService;
import com.mookive.mookive_backend.user.domain.entity.User;
import com.mookive.mookive_backend.user.domain.service.UserQueryService;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Optional;

@Service
@RequiredArgsConstructor
Expand All @@ -27,18 +32,36 @@ public class ReviewCreateService {
private final UserQueryService userQueryService;
private final ReviewSaveService reviewSaveService;
private final KeywordSaveService keywordSaveService;
private final KeywordDeleteService keywordDeleteService;
private final ReviewQueryService reviewQueryService;

public void createReview(ReviewRequest.ReviewCreateRequest reviewCreateRequest) {
Long movieId = reviewCreateRequest.getMovieId();
Movie movie = movieQueryService.findById(movieId);
Long userId = reviewCreateRequest.getUserId();
User user = userQueryService.findById(userId);
Review review = ReviewMapper.mapToReview(reviewCreateRequest, movie, user);
reviewSaveService.saveReview(review);
Optional<Review> optionalReview = reviewQueryService.findByUserIdAndMovieId(userId, movieId);
if(optionalReview.isPresent()){
Review review = optionalReview.get();
LocalDate date = reviewCreateRequest.getDate();
String rating = reviewCreateRequest.getRating();
String text = reviewCreateRequest.getText();
ArrayList<String> keywords = reviewCreateRequest.getKeywords();
review.updateReview(rating, text, date);
keywordDeleteService.deleteAllByReviewId(review.getId());
saveKeywords(keywords, review, movieId);
}
else {
Movie movie = movieQueryService.findById(movieId);
User user = userQueryService.findById(userId);
Review review = ReviewMapper.mapToReview(reviewCreateRequest, movie, user);
reviewSaveService.saveReview(review);
ArrayList<String> keywords = reviewCreateRequest.getKeywords();
saveKeywords(keywords, review, movieId);
}
}

ArrayList<String> keywords = reviewCreateRequest.getKeywords();
private void saveKeywords(ArrayList<String> keywords, Review review, Long movieId) {
for(String word : keywords){
Keyword keyword = KeywordMapper.mapToKeyword(review, word);
Keyword keyword = KeywordMapper.mapToKeyword(review, word, movieId);
keywordSaveService.saveKeyword(keyword);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,10 @@ public Review(String rating, String text, LocalDate date, User user, Movie movie
this.user = user;
this.movie = movie;
}

public void updateReview(String rating, String text, LocalDate date) {
this.rating = rating;
this.text = text;
this.date = date;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.mookive.mookive_backend.review.domain.entity.Review;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface ReviewRepository extends JpaRepository<Review, Long> {

Review findByUserIdAndMovieId(Long userId, Long movieId);
Optional<Review> findByUserIdAndMovieId(Long userId, Long movieId);;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
@RequiredArgsConstructor
@Transactional
Expand All @@ -18,7 +20,8 @@ public Review findById(Long reviewId) {
.orElseThrow(() -> new IllegalArgumentException("Review not found"));
}

public Review findByUserIdAndMovieId(Long userId, Long movieId) {
public Optional<Review> findByUserIdAndMovieId(Long userId, Long movieId) {
return reviewRepository.findByUserIdAndMovieId(userId, movieId);
}

}

0 comments on commit 51f9ae6

Please sign in to comment.