1
0
Prechádzať zdrojové kódy

fix: 修改了商品的逻辑

DingXiaoYu 2 rokov pred
rodič
commit
ed8fe8cb35

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

@@ -27,9 +27,9 @@ public class ProductController {
         return ResultVO.buildSuccess(productService.create(productVO));
     }
 
-    @PostMapping("/stock/{id}")
-    public ResultVO<Boolean> stock(@PathVariable(value = "id") Integer id){
-        return ResultVO.buildSuccess(productService.addStock(id));
+    @PostMapping("/stock")
+    public ResultVO<Boolean> addStock(@RequestParam("id") Integer id,@RequestParam("number") Integer number){
+        return ResultVO.buildSuccess(productService.addStock(id,number));
     }
 
     @GetMapping("/all")

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

@@ -7,4 +7,6 @@ import org.springframework.data.jpa.repository.JpaRepository;
 
 public interface ProductRepository extends JpaRepository<Product, Integer> {
     List<Product> findAllByStoreId(Integer storeId);
+
+    Product findByStoreIdAndName(Integer storeId,String name);
 }

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

@@ -6,6 +6,6 @@ import com.seecoder.BlueWhale.VO.ProductVO;
 
 public interface ProductService {
     Boolean create(ProductVO productVO);
-    Boolean addStock(Integer id);
+    Boolean addStock(Integer id, Integer number);
     List<ProductVO> getAllProducts(Integer storeId);
 }

+ 5 - 2
src/main/java/com/seecoder/BlueWhale/ServiceImpl/ProductServiceImpl.java

@@ -25,6 +25,9 @@ public class ProductServiceImpl implements ProductService {
 
     @Override
     public Boolean create(ProductVO productVO) {
+        if (productRepository.findByStoreIdAndName(productVO.getStoreId(),productVO.getName())==null){
+            throw BlueWhaleException.nameAlreadyExists();
+        }
         Product product=productVO.toPO();
         product.setSalesAmount(0);
         product.setStock(0);
@@ -35,12 +38,12 @@ public class ProductServiceImpl implements ProductService {
     }
 
     @Override
-    public Boolean addStock(Integer id) {
+    public Boolean addStock(Integer id, Integer number) {
         Product product=productRepository.findById(id).orElse(null);
         if(product==null){
             throw BlueWhaleException.productNotExists();
         }
-        product.setStock(product.getStock()+1);
+        product.setStock(product.getStock()+number);
         productRepository.save(product);
         return true;
     }