| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- package com.etotem.cfc.controller.cart;
- import com.etotem.cfc.common.Result;
- import com.etotem.cfc.dto.CartItemDTO;
- import com.etotem.cfc.dto.CartListResultDTO;
- import com.etotem.cfc.service.CartService;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestAttribute;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import javax.annotation.Resource;
- import java.util.List;
- import java.util.Map;
- import com.etotem.cfc.util.ParamUtils;
- @RestController
- @RequestMapping("/api/cart")
- public class CartController {
- @Resource
- private CartService cartService;
- @PostMapping("/add")
- public Result<CartItemDTO> add(@RequestBody Map<String, Object> params,
- @RequestAttribute("userId") Long userId) {
- Long productId = ParamUtils.getLong(params.get("productId"));
- Integer quantity = params.containsKey("quantity") ? Integer.valueOf(params.get("quantity").toString()) : 1;
- Long skuId = params.containsKey("skuId") ? ParamUtils.getLong(params.get("skuId")) : null;
- CartItemDTO dto = cartService.addToCart(userId, productId, quantity, skuId);
- if (dto == null) {
- return Result.error("商品不存在");
- }
- return Result.success(dto);
- }
- @PostMapping("/list")
- public Result<CartListResultDTO> list(@RequestAttribute("userId") Long userId) {
- CartListResultDTO list = cartService.getCartList(userId);
- return Result.success(list);
- }
- @PostMapping("/update")
- public Result<Void> update(@RequestBody Map<String, Object> params,
- @RequestAttribute("userId") Long userId) {
- Long productId = ParamUtils.getLong(params.get("productId"));
- Integer quantity = Integer.valueOf(params.get("quantity").toString());
- cartService.updateQuantity(userId, productId, quantity);
- return Result.success();
- }
- @PostMapping("/remove")
- public Result<Void> remove(@RequestBody Map<String, Object> params,
- @RequestAttribute("userId") Long userId) {
- Long productId = ParamUtils.getLong(params.get("productId"));
- cartService.removeItem(userId, productId);
- return Result.success();
- }
- @PostMapping("/count")
- public Result<Integer> count(@RequestAttribute("userId") Long userId) {
- int count = cartService.getCartCount(userId);
- return Result.success(count);
- }
- }
|