业务需求:
在小程序用户端购物车页面能查到当前用户的所有菜品或者套餐
代码实现
controller层
@GetMapping("/list")public Result<List<ShoppingCart>> list(){List<ShoppingCart> list = shoppingCartService.shopShoppingCart();return Result.success(list);}
实现类
获取当前用户的userId,组装成一个shoppingcart对象,调用后台sql进行查询,将查询的list返回。
@Overridepublic List<ShoppingCart> shopShoppingCart() {Long userId = BaseContext.getCurrentId();ShoppingCart cart = ShoppingCart.builder().userId(userId).build();List<ShoppingCart> list = shoppingCartMapper.list(cart);return list;}
清空购物车
@DeleteMapping("/clean")public Result clean(){shoppingCartService.deleteShoppingCart();return Result.success();}
@Overridepublic void deleteShoppingCart() {Long userId = BaseContext.getCurrentId();shoppingCartMapper.deleteByUserId(userId);}
@Delete("delete * from shopping_cart where id=#{userId}")void deleteByUserId(Long userId);