Browse Source

feat: 完成了返回评论的功能

DingXiaoYu 2 years ago
parent
commit
d9f41ceb39

+ 6 - 1
src/main/java/com/seecoder/BlueWhale/controller/ProductController.java

@@ -1,7 +1,7 @@
 package com.seecoder.BlueWhale.controller;
 
-import com.seecoder.BlueWhale.enums.CategoryEnum;
 import com.seecoder.BlueWhale.service.ProductService;
+import com.seecoder.BlueWhale.vo.CommentVO;
 import com.seecoder.BlueWhale.vo.ProductVO;
 import com.seecoder.BlueWhale.vo.ResultVO;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -40,4 +40,9 @@ public class ProductController {
     public ResultVO<List<ProductVO>> getProductsWithCondition(@RequestBody ProductVO productVO){
         return ResultVO.buildSuccess(productService.getProductsWithCondition(productVO.getName(),productVO.getCategory(),productVO.getPrice()));
     }
+
+    @GetMapping("/comment")
+    public ResultVO<List<CommentVO>> getComments(@RequestParam("productId")Integer productId){
+        return ResultVO.buildSuccess(productService.getComments(productId));
+    }
 }

+ 4 - 0
src/main/java/com/seecoder/BlueWhale/repository/OrderRepository.java

@@ -8,4 +8,8 @@ import com.seecoder.BlueWhale.po.Order;
 
 public interface OrderRepository extends JpaRepository<Order, Integer> {
     List<Order> findByUserId(Integer userId);
+
+    List<Order> findByProductIdAndFinishTimeNotNull(Integer productId);
+
+
 }

+ 3 - 0
src/main/java/com/seecoder/BlueWhale/service/ProductService.java

@@ -3,6 +3,7 @@ package com.seecoder.BlueWhale.service;
 import java.util.List;
 
 import com.seecoder.BlueWhale.enums.CategoryEnum;
+import com.seecoder.BlueWhale.vo.CommentVO;
 import com.seecoder.BlueWhale.vo.ProductVO;
 
 public interface ProductService {
@@ -12,4 +13,6 @@ public interface ProductService {
     ProductVO getProduct(Integer id);
 
     List<ProductVO> getProductsWithCondition(String name, CategoryEnum category, Double price);
+
+    List<CommentVO> getComments(Integer productId);
 }

+ 25 - 0
src/main/java/com/seecoder/BlueWhale/serviceImpl/ProductServiceImpl.java

@@ -2,11 +2,15 @@ package com.seecoder.BlueWhale.serviceImpl;
 
 import com.seecoder.BlueWhale.enums.CategoryEnum;
 import com.seecoder.BlueWhale.exception.BlueWhaleException;
+import com.seecoder.BlueWhale.po.Order;
 import com.seecoder.BlueWhale.po.Product;
 import com.seecoder.BlueWhale.po.Store;
+import com.seecoder.BlueWhale.repository.OrderRepository;
 import com.seecoder.BlueWhale.repository.ProductRepository;
 import com.seecoder.BlueWhale.repository.StoreRepository;
+import com.seecoder.BlueWhale.repository.UserRepository;
 import com.seecoder.BlueWhale.service.ProductService;
+import com.seecoder.BlueWhale.vo.CommentVO;
 import com.seecoder.BlueWhale.vo.ProductVO;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -28,6 +32,12 @@ public class ProductServiceImpl implements ProductService {
     @Autowired
     EntityManager entityManager;
 
+    @Autowired
+    OrderRepository orderRepository;
+
+    @Autowired
+    UserRepository userRepository;
+
     @Override
     public Boolean create(ProductVO productVO) {
         if (productRepository.findByStoreIdAndName(productVO.getStoreId(),productVO.getName())!=null){
@@ -96,4 +106,19 @@ public class ProductServiceImpl implements ProductService {
         List<Product> products=query.getResultList();
         return products.stream().map(Product::toVO).collect(Collectors.toList());
     }
+
+    @Override
+    public List<CommentVO> getComments(Integer productId) {
+        List<Order> orders=orderRepository.findByProductIdAndFinishTimeNotNull(productId);
+        return orders.stream().map(x->{
+            CommentVO commentVO=new CommentVO();
+            commentVO.setOrderId(x.getId());
+            commentVO.setRating(x.getRating());
+            commentVO.setTime(x.getFinishTime());
+            commentVO.setComment(x.getContent());
+            commentVO.setUserId(x.getUserId());
+            commentVO.setUserName(userRepository.findById(x.getUserId()).get().getName());
+            return commentVO;
+        }).collect(Collectors.toList());
+    }
 }

+ 25 - 0
src/main/java/com/seecoder/BlueWhale/vo/CommentVO.java

@@ -0,0 +1,25 @@
+package com.seecoder.BlueWhale.vo;
+
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+
+import java.util.Date;
+
+@Getter
+@Setter
+@NoArgsConstructor
+public class CommentVO {
+
+    Integer userId;
+
+    String userName;
+
+    Integer orderId;
+
+    Integer rating;
+
+    String comment;
+
+    Date time;
+}