리뷰기능 추가하기
리뷰 엔티티 작성
@Entity
@Getter
@Setter
public class Review {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String content;
private int rating;
@ManyToOne
private Product product;
}
@Entity
@Getter
@Setter
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private double price;
private String description;
@ManyToOne
private Category category;
@OneToMany(mappedBy = "product" , cascade = CascadeType.REMOVE)
private List<Review> reviewList;
}
리뷰 레포지토리 작성
public interface ReviewRepository extends JpaRepository<Review,Long> {
List<Review> findByProduct(Product product);
}
리뷰 서비스 작성
@Service
@RequiredArgsConstructor
public class ReviewService {
private final ReviewRepository reviewRepository;
private final ProductRepository productRepository;
public Review getReviewById(@PathVariable("id") Long id){
return reviewRepository.findById(id).get();
}
public Review createReview(@RequestParam String content , @RequestParam int rating ,@RequestParam Long productId){
Review review = new Review();
review.setContent(content);
review.setRating(rating);
Product product = productRepository.findById(productId).orElseThrow();
review.setProduct(product);
product.getReviewList().add(review);
return reviewRepository.save(review);
}
public void deleteReview(@RequestParam Long id){
reviewRepository.deleteById(id);
}
public List<Review> getReviewByProduct(Product product){
return reviewRepository.findByProduct(product);
}
}
리뷰 컨트롤러 작성
@Controller
@RequiredArgsConstructor
@RequestMapping("/review")
public class ReviewController {
private final ReviewService reviewService;
private final ProductService productService;
@GetMapping("/create/{productId}")
public String createReview(Model model ,@PathVariable Long productId) {
model.addAttribute("productId" , productId);
return "review_form";
}
@PostMapping("/create")
public String createReview(@RequestParam String content, @RequestParam int rating, @RequestParam("productId") Long productId) {
reviewService.createReview(content, rating, productId);
return String.format("redirect:/product/%d", productId);
}
@PostMapping("/delete/{reviewId}")
public String deleteReview(@PathVariable Long reviewId){
Long productId = reviewService.getReviewById(reviewId).getProduct().getId();
reviewService.deleteReview(reviewId);
return String.format("redirect:/product/%d", productId);
}
}
리뷰 생성 폼 html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:action="@{/review/create}" method="post">
<input type="hidden" name="productId" th:value="${productId}"/>
<label for="content">리뷰 내용:</label><br>
<textarea name="content" id="content" cols="30" rows="10"></textarea><br>
<label for="rating">리뷰 평점:</label>
<input type="number" name="rating" id="rating" min="1" max="5"/><br>
<input type="submit" value="리뷰 작성">
</form>
</body>
</html>
리뷰 삭제 html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>리뷰 삭제하기</title>
</head>
<body>
<h2>리뷰 삭제하기</h2>
<form th:action="@{/review/delete}" method="post">
<input type="hidden" name="productId" th:value="${productId}"/>
<label for="reviewId">삭제할 리뷰 아이디:</label><br>
<input type="text" name="reviewId" id="reviewId">
<input type="submit" value="리뷰 삭제">
</form>
</body>
</html>
상품 상세보기 html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>상품 상세보기</title>
</head>
<body>
<h2>상품 상세보기</h2>
<ul>
<li th:each="product : ${productById}">
<span th:text="${product.description}"></span>
</li>
</ul>
<ul>
<li th:each="review : ${reviewList}">
<span th:text="${review.content}"></span>
<span th:text="${review.rating}"></span>
<!-- <a th:href="@{|/review/delete/${productById.id}|}">리뷰 삭제하기</a>-->
<form th:action="@{|/review/delete/${review.id}|}" method="post">
<input type="submit" value="리뷰 삭제하기">
</form>
</li>
</ul>
<div> <a th:href="@{|/review/create/${productById.id}|}">리뷰 작성하기</a> </div>
<div> <a th:href="@{/product/list}">목록으로 돌아가기</a> </div>
</body>
</html>