Commit 9980b12551e9cd5ce59b5e497b6754cf8209654f
1 parent
f7fe9ad7
调整购物车接口,支持 add 商品时,适配前端可能要删除的情况
Showing
2 changed files
with
7 additions
and
3 deletions
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/cart/vo/AppTradeCartAddReqVO.java
| ... | ... | @@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.trade.controller.app.cart.vo; |
| 3 | 3 | import io.swagger.v3.oas.annotations.media.Schema; |
| 4 | 4 | import lombok.Data; |
| 5 | 5 | |
| 6 | -import javax.validation.constraints.Min; | |
| 7 | 6 | import javax.validation.constraints.NotNull; |
| 8 | 7 | |
| 9 | 8 | @Schema(description = "用户 App - 购物车添加购物项 Request VO") |
| ... | ... | @@ -16,7 +15,6 @@ public class AppTradeCartAddReqVO { |
| 16 | 15 | |
| 17 | 16 | @Schema(description = "新增商品数量", required = true, example = "1") |
| 18 | 17 | @NotNull(message = "数量不能为空") |
| 19 | - @Min(message = "数量必须大于 0", value = 1L) | |
| 20 | 18 | private Integer count; |
| 21 | 19 | |
| 22 | 20 | @Schema(description = "是否添加到购物车", required = true, example = "true") | ... | ... |
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/cart/TradeCartServiceImpl.java
| ... | ... | @@ -58,9 +58,15 @@ public class TradeCartServiceImpl implements TradeCartService { |
| 58 | 58 | cart.getCount() + addReqVO.getCount() : addReqVO.getCount(); |
| 59 | 59 | ProductSkuRespDTO sku = checkProductSku(addReqVO.getSkuId(), count); |
| 60 | 60 | |
| 61 | + // 情况零:特殊,count 小于等于 0,说明前端项目删除 | |
| 61 | 62 | // 情况一:存在,则进行数量更新 |
| 62 | 63 | if (cart != null) { |
| 63 | - cartMapper.updateById(new TradeCartDO().setId(cart.getId()).setCount(count)); | |
| 64 | + // 特殊情况,如果 count 小于等于 0,说明前端想要删除 | |
| 65 | + if (count <= 0) { | |
| 66 | + cartMapper.deleteById(cart.getId()); | |
| 67 | + } else { | |
| 68 | + cartMapper.updateById(new TradeCartDO().setId(cart.getId()).setCount(count)); | |
| 69 | + } | |
| 64 | 70 | return cart.getId(); |
| 65 | 71 | // 情况二:不存在,则进行插入 |
| 66 | 72 | } else { | ... | ... |