Skip to content

Commit

Permalink
[feat] 리뷰 생성하는 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
syyling committed Mar 6, 2024
1 parent be04fd8 commit 9e1b9d4
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.mookive.mookive_backend.keyword.application.mapper;

import com.mookive.mookive_backend.keyword.domain.entity.Keyword;
import com.mookive.mookive_backend.review.domain.entity.Review;

public class KeywordMapper {

public static Keyword mapToKeyword(Review review, String word) {
return Keyword.builder()
.review(review)
.word(word)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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 lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class KeywordSaveService {

private final KeywordRepository keywordRepository;

public void saveKeyword(Keyword keyword) {
keywordRepository.save(keyword);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ public class MovieQueryService {
public Movie findByTitle(String title) {
return movieRepository.findByTitle(title);
}

public Movie findById(Long id) {
return movieRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Movie not found"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.mookive.mookive_backend.review.application.dto.request;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

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

public class ReviewRequest {

@Getter
@NoArgsConstructor
public static class ReviewCreateRequest {
private Long movieId;
private Long userId;
private String rating;
private String text;
private LocalDate date;
private ArrayList<String> keywords;


@Builder
public ReviewCreateRequest(Long movieId, Long userId, String rating,
String text, LocalDate date, ArrayList<String> keywords) {
this.movieId = movieId;
this.userId = userId;
this.rating = rating;
this.text = text;
this.date = date;
this.keywords = keywords;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.mookive.mookive_backend.review.application.mapper;

import com.mookive.mookive_backend.movie.domain.entity.Movie;
import com.mookive.mookive_backend.review.application.dto.request.ReviewRequest;
import com.mookive.mookive_backend.review.domain.entity.Review;
import com.mookive.mookive_backend.user.domain.entity.User;

public class ReviewMapper {

public static Review mapToReview(ReviewRequest.ReviewCreateRequest reviewCreateRequest, Movie movie, User user) {
return Review.builder()
.movie(movie)
.user(user)
.rating(reviewCreateRequest.getRating())
.text(reviewCreateRequest.getText())
.date(reviewCreateRequest.getDate())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.mookive.mookive_backend.review.application.service;


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.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.ReviewSaveService;
import com.mookive.mookive_backend.user.domain.entity.User;
import com.mookive.mookive_backend.user.domain.service.UserQueryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
@RequiredArgsConstructor
public class ReviewCreateService {

private final MovieQueryService movieQueryService;
private final UserQueryService userQueryService;
private final ReviewSaveService reviewSaveService;
private final KeywordSaveService keywordSaveService;

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);

ArrayList<String> keywords = reviewCreateRequest.getKeywords();
for(String word : keywords){
Keyword keyword = KeywordMapper.mapToKeyword(review, word);
keywordSaveService.saveKeyword(keyword);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.mookive.mookive_backend.review.domain.service;

import com.mookive.mookive_backend.review.domain.entity.Review;
import com.mookive.mookive_backend.review.domain.repository.ReviewRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ReviewSaveService {

private final ReviewRepository reviewRepository;

public void saveReview(Review review) {
reviewRepository.save(review);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.mookive.mookive_backend.review.presentation;

import com.mookive.mookive_backend.review.application.dto.request.ReviewRequest;
import com.mookive.mookive_backend.review.application.service.ReviewCreateService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class ReviewController {

private final ReviewCreateService reviewCreateService;

@PostMapping("/review")
public void createReview(@RequestBody ReviewRequest.ReviewCreateRequest reviewCreateRequest) {
reviewCreateService.createReview(reviewCreateRequest);
}
}

0 comments on commit 9e1b9d4

Please sign in to comment.