Browse Source

feat: 完成增加库存与获取商店下商品接口

Gudge-ZL 2 years ago
parent
commit
5a001ccccd

+ 16 - 0
src/main/java/com/seecoder/BlueWhale/Controller/ProductController.java

@@ -3,10 +3,16 @@ package com.seecoder.BlueWhale.Controller;
 import com.seecoder.BlueWhale.Service.ProductService;
 import com.seecoder.BlueWhale.VO.ProductVO;
 import com.seecoder.BlueWhale.VO.ResultVO;
+
+import java.util.List;
+
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
 @RestController
@@ -20,4 +26,14 @@ public class ProductController {
     public ResultVO<Boolean> create(@RequestBody ProductVO productVO){
         return ResultVO.buildSuccess(productService.create(productVO));
     }
+
+    @PostMapping("/stock/{id}")
+    public ResultVO<Boolean> stock(@PathVariable(value = "id") Integer id){
+        return ResultVO.buildSuccess(productService.addStock(id));
+    }
+
+    @GetMapping("/all")
+    public ResultVO<List<ProductVO>> getAllProducts(@RequestParam("storeId") Integer storeId){
+        return ResultVO.buildSuccess(productService.getAllProducts(storeId));
+    }
 }

+ 4 - 0
src/main/java/com/seecoder/BlueWhale/Exception/BlueWhaleException.java

@@ -33,4 +33,8 @@ public class BlueWhaleException extends RuntimeException{
     public static BlueWhaleException storeNotExists(){
         return new BlueWhaleException("店铺不存在!");
     }
+
+    public static BlueWhaleException productNotExists(){
+        return new BlueWhaleException("商品不存在!");
+    }
 }

+ 3 - 0
src/main/java/com/seecoder/BlueWhale/Repository/ProductRepository.java

@@ -1,7 +1,10 @@
 package com.seecoder.BlueWhale.Repository;
 
 import com.seecoder.BlueWhale.PO.Product;
+
+import java.util.List;
 import org.springframework.data.jpa.repository.JpaRepository;
 
 public interface ProductRepository extends JpaRepository<Product, Integer> {
+    List<Product> findAllByStoreId(Integer storeId);
 }

+ 4 - 0
src/main/java/com/seecoder/BlueWhale/Service/ProductService.java

@@ -1,7 +1,11 @@
 package com.seecoder.BlueWhale.Service;
 
+import java.util.List;
+
 import com.seecoder.BlueWhale.VO.ProductVO;
 
 public interface ProductService {
     Boolean create(ProductVO productVO);
+    Boolean addStock(Integer id);
+    List<ProductVO> getAllProducts(Integer storeId);
 }

+ 30 - 0
src/main/java/com/seecoder/BlueWhale/ServiceImpl/ProductServiceImpl.java

@@ -1,9 +1,16 @@
 package com.seecoder.BlueWhale.ServiceImpl;
 
 import com.seecoder.BlueWhale.Repository.ProductRepository;
+import com.seecoder.BlueWhale.Repository.StoreRepository;
+import com.seecoder.BlueWhale.Exception.BlueWhaleException;
 import com.seecoder.BlueWhale.PO.Product;
+import com.seecoder.BlueWhale.PO.Store;
 import com.seecoder.BlueWhale.Service.ProductService;
 import com.seecoder.BlueWhale.VO.ProductVO;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -13,6 +20,9 @@ public class ProductServiceImpl implements ProductService {
     @Autowired
     ProductRepository productRepository;
 
+    @Autowired
+    StoreRepository storeRepository;
+
     @Override
     public Boolean create(ProductVO productVO) {
         Product product=productVO.toPO();
@@ -23,4 +33,24 @@ public class ProductServiceImpl implements ProductService {
         productRepository.save(product);
         return true;
     }
+
+    @Override
+    public Boolean addStock(Integer id) {
+        Product product=productRepository.findById(id).orElse(null);
+        if(product==null){
+            throw BlueWhaleException.productNotExists();
+        }
+        product.setStock(product.getStock()+1);
+        productRepository.save(product);
+        return true;
+    }
+
+    @Override
+    public List<ProductVO> getAllProducts(Integer storeId) {
+        Store store = storeRepository.findById(storeId).orElse(null);
+        if (store == null) {
+            throw BlueWhaleException.storeNotExists();
+        }
+        return productRepository.findAllByStoreId(storeId).stream().map(Product::toVO).collect(Collectors.toList());
+    }
 }