黑馬程序員Java項目實戰(zhàn)《瑞吉外賣》,輕松掌握springboot + ...

Day6老師跳過的,減少購物車商品數(shù)量的響應,為節(jié)省篇幅,我寫在controller里:
??@PostMapping("/sub")
??public R<String> sub(@RequestBody SetmealDish setmealDish){
????Long userId=BaseContext.getCurrentId();
????LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
????if (setmealDish.getSetmealId() != null) {
??????Long setmealId = setmealDish.getSetmealId();
??????queryWrapper.eq(ShoppingCart::getSetmealId, setmealId).eq(ShoppingCart::getUserId, userId);
????} else {
??????Long dishId = setmealDish.getDishId();
??????queryWrapper.eq(ShoppingCart::getDishId, dishId).eq(ShoppingCart::getUserId, userId);
????}
????ShoppingCart shoppingCart = shoppingCartService.getOne(queryWrapper);
????if (shoppingCart != null) {
??????if (shoppingCart.getNumber() == 1) {
????????shoppingCartService.removeById(shoppingCart);
??????} else {
????????shoppingCart.setNumber(shoppingCart.getNumber() - 1);
????????shoppingCartService.updateById(shoppingCart);
??????}
??????return R.success("成功");
????} else {
??????return R.error("購物車中不存在該商品");
????}
??}