|
|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
package com.ywp.shoppingcartbackend.controller;
|
|
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
|
import com.ywp.shoppingcartbackend.domain.CartItem;
|
|
|
|
|
import com.ywp.shoppingcartbackend.service.CartItemService;
|
|
|
|
|
@ -25,6 +26,19 @@ public class CartController {
|
|
|
|
|
return ResponseEntity.ok(cartItem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping("/decrease/{productId}")
|
|
|
|
|
public ResponseEntity<CartItem> decreaseQuantity(@PathVariable Integer userId, @PathVariable Integer productId,
|
|
|
|
|
@RequestParam Integer quantity) {
|
|
|
|
|
CartItem cartItem = cartItemService.decreaseQuantity(userId, productId, quantity);
|
|
|
|
|
if (cartItem == null) {
|
|
|
|
|
// 如果 cartItem 为 null,表示商品数量已经减少到 0,返回 HTTP 204 No Content 响应
|
|
|
|
|
return ResponseEntity.noContent().build();
|
|
|
|
|
} else {
|
|
|
|
|
// 否则,返回包含更新后的 cartItem 的 200 OK 响应
|
|
|
|
|
return ResponseEntity.ok(cartItem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping("/remove/{productId}")
|
|
|
|
|
public void removeFromCart(@PathVariable Integer userId, @PathVariable Integer productId) {
|
|
|
|
|
cartItemService.removeFromCart(userId, productId);
|
|
|
|
|
@ -34,10 +48,8 @@ public class CartController {
|
|
|
|
|
public ResponseEntity<List<CartItem>> getCartItems(@PathVariable Integer userId,
|
|
|
|
|
@RequestParam(required = false, defaultValue = "0") Integer pageNum,
|
|
|
|
|
@RequestParam(required = false, defaultValue = "10") Integer pageSize) {
|
|
|
|
|
Page<CartItem> page = new Page<>(pageNum, pageSize);
|
|
|
|
|
QueryWrapper<CartItem> queryWrapper = new QueryWrapper<>();
|
|
|
|
|
queryWrapper.eq("user_id", userId);
|
|
|
|
|
Page<CartItem> result = cartItemService.page(page, queryWrapper);
|
|
|
|
|
return ResponseEntity.ok(result.getRecords());
|
|
|
|
|
|
|
|
|
|
IPage<CartItem> cartItems = cartItemService.getCartItems(userId, pageNum, pageSize);
|
|
|
|
|
return ResponseEntity.ok(cartItems.getRecords());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|