迁移 cart 的 web api

解决 promotion-service 模块的启动
This commit is contained in:
YunaiV
2020-08-07 19:12:00 +08:00
parent 5714ddcbe8
commit 84cc2728bd
22 changed files with 348 additions and 130 deletions

View File

@@ -0,0 +1,43 @@
package cn.iocoder.mall.shopweb.controller.order;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.security.user.core.context.UserSecurityContextHolder;
import cn.iocoder.mall.shopweb.manager.order.cart.CartManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import static cn.iocoder.common.framework.vo.CommonResult.success;
@Api(tags = "购物车 API")
@RestController
@RequestMapping("/cart")
@Validated
public class CartController {
@Autowired
private CartManager cartManager;
@PostMapping("add")
@ApiOperation("添加商品到购物车")
@ApiImplicitParams({
@ApiImplicitParam(name = "skuId", value = "商品 SKU 编号", required = true, example = "1"),
@ApiImplicitParam(name = "quantity", value = "增加数量", required = true, example = "1024")
})
public CommonResult<Boolean> addCartItem(@RequestParam("skuId") Integer skuId,
@RequestParam("quantity") Integer quantity) {
cartManager.addCartItem(UserSecurityContextHolder.getUserId(), skuId, quantity);
return success(true);
}
@GetMapping("sum-quantity")
@ApiOperation("查询用户在购物车中的商品数量")
public CommonResult<Integer> sumCartItemQuantity() {
return success(cartManager.sumCartItemQuantity(UserSecurityContextHolder.getUserId()));
}
}

View File

@@ -0,0 +1,43 @@
package cn.iocoder.mall.shopweb.manager.order.cart;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.orderservice.rpc.cart.CartRpc;
import cn.iocoder.mall.orderservice.rpc.cart.dto.CartItemAddReqDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
/**
* 购物车 Manager
*/
@Service
public class CartManager {
@DubboReference(version = "${dubbo.consumer.ProductCategoryRpc.version}")
private CartRpc cartRpc;
/**
* 添加商品到购物车
*
* @param userId 用户编号
* @param skuId 商品 SKU 编号
* @param quantity 增加数量
*/
public void addCartItem(Integer userId, Integer skuId, Integer quantity) {
CommonResult<Boolean> addCartItemResult = cartRpc.addCartItem(new CartItemAddReqDTO().setUserId(userId)
.setSkuId(skuId).setQuantity(quantity));
addCartItemResult.checkError();
}
/**
* 查询用户在购物车中的商品数量
*
* @param userId 用户编号
* @return 商品数量
*/
public Integer sumCartItemQuantity(Integer userId) {
CommonResult<Integer> sumCartItemQuantityResult = cartRpc.sumCartItemQuantity(userId);
sumCartItemQuantityResult.checkError();
return sumCartItemQuantityResult.getData();
}
}

View File

@@ -0,0 +1 @@
package cn.iocoder.mall.shopweb.manager.order;

View File

@@ -7,7 +7,7 @@ import cn.iocoder.mall.productservice.rpc.category.dto.ProductCategoryListQueryR
import cn.iocoder.mall.productservice.rpc.category.dto.ProductCategoryRespDTO;
import cn.iocoder.mall.shopweb.controller.product.vo.category.ProductCategoryRespVO;
import cn.iocoder.mall.shopweb.convert.product.ProductCategoryConvert;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
@@ -20,7 +20,7 @@ import java.util.List;
@Validated
public class ProductCategoryManager {
@Reference(version = "${dubbo.consumer.ProductCategoryRpc.version}")
@DubboReference(version = "${dubbo.consumer.ProductCategoryRpc.version}")
private ProductCategoryRpc productCategoryRpc;
public List<ProductCategoryRespVO> listProductCategories(Integer pid) {