Commit 93103ae7a76934faed47d3e350a9ab6c8940c170
1 parent
f38b5817
添加单个产品库存查询功能
在StockApi中添加了通过产品ID查询库存数据的功能。使用了CollUtil类来检查库存列表是否为空,并返回相应的成功或失败消息。这个改动提升了库存查询的便利性。
Showing
1 changed file
with
19 additions
and
0 deletions
etrade-shop/src/main/java/com/diligrp/etrade/shop/api/StockApi.java
1 | package com.diligrp.etrade.shop.api; | 1 | package com.diligrp.etrade.shop.api; |
2 | 2 | ||
3 | +import cn.hutool.core.collection.CollUtil; | ||
3 | import com.diligrp.etrade.core.domain.Message; | 4 | import com.diligrp.etrade.core.domain.Message; |
4 | import com.diligrp.etrade.shop.domain.request.StockAddRequest; | 5 | import com.diligrp.etrade.shop.domain.request.StockAddRequest; |
5 | import com.diligrp.etrade.shop.domain.request.StockDeductRequest; | 6 | import com.diligrp.etrade.shop.domain.request.StockDeductRequest; |
7 | +import com.diligrp.etrade.shop.model.Stock; | ||
6 | import com.diligrp.etrade.shop.service.StockService; | 8 | import com.diligrp.etrade.shop.service.StockService; |
7 | import com.diligrp.idempotent.spring.boot.annotation.Idempotent; | 9 | import com.diligrp.idempotent.spring.boot.annotation.Idempotent; |
8 | import org.springframework.web.bind.annotation.*; | 10 | import org.springframework.web.bind.annotation.*; |
9 | 11 | ||
12 | +import java.util.Collections; | ||
10 | import java.util.List; | 13 | import java.util.List; |
11 | 14 | ||
12 | /** | 15 | /** |
@@ -52,4 +55,20 @@ public class StockApi { | @@ -52,4 +55,20 @@ public class StockApi { | ||
52 | return Message.success(); | 55 | return Message.success(); |
53 | } | 56 | } |
54 | 57 | ||
58 | + | ||
59 | + /** | ||
60 | + * Query stock data for a single product. | ||
61 | + * | ||
62 | + * @param productId The ID of the product to query stock data for. | ||
63 | + * @return Stock data for the specified product. | ||
64 | + */ | ||
65 | + @GetMapping("/{productId}") | ||
66 | + public Message<Stock> getStockByProductId(@PathVariable Long productId) { | ||
67 | + List<Stock> stocks = stockService.listByProductIds(Collections.singletonList(productId)); | ||
68 | + if (CollUtil.isEmpty(stocks)) { | ||
69 | + return Message.failure("Stock data not found for the product ID: " + productId); | ||
70 | + } | ||
71 | + return Message.success(stocks.get(0)); | ||
72 | + } | ||
73 | + | ||
55 | } | 74 | } |