购物车
需求分析
交互过程
代码开发-准备工作
添加购物车
流程:
- 根据前端发送过来的json数据可以发现,用户id并没有传送过来,所以先获取用户的id,并设置userid
- 查询添加购物车的数据是菜品还是套餐
- 判断购物车中是否已经存在目前要加入的菜品或菜单
- 如果已经存在,则在购物车中,在原来商品的基础上,数量+1
- 如果不存在,则保存到购物车数据库中。
请求方式:
代码为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
@PostMapping("/add") public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
Long userId = BaseContext.getId(); shoppingCart.setUserId(userId);
Long dishId = shoppingCart.getDishId(); LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(ShoppingCart::getUserId,userId); if (dishId!=null){ lambdaQueryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId()); }else { lambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId()); }
ShoppingCart shoppingCartServiceOne = shoppingCartService.getOne(lambdaQueryWrapper); if (shoppingCartServiceOne!=null){ Integer number = shoppingCartServiceOne.getNumber(); shoppingCartServiceOne.setNumber(number+1); shoppingCartService.updateById(shoppingCartServiceOne); }else { shoppingCart.setNumber(1); shoppingCartService.save(shoppingCart); shoppingCartServiceOne = shoppingCart; }
return R.success(shoppingCartServiceOne); }
|
查看购物车
添加购物车功能已经实现完成,所以将之前手写的哪个cartData.json
改回原来的访问地址,进行功能的实现
ShoppingCartController.java代码为:
1 2 3 4 5 6 7 8 9 10
| @GetMapping("/list") public R<List<ShoppingCart>> list(){ Long userId = BaseContext.getId(); LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(ShoppingCart::getUserId,userId); List<ShoppingCart> list = shoppingCartService.list(lambdaQueryWrapper); return R.success(list); }
|
减少购物车中商品的数量
减少购物车中商品的数量所发出的请求为:
代码为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
@PostMapping("/sub") public R<String> sub(@RequestBody ShoppingCart shoppingCart){ Long userId = BaseContext.getId(); Long dishId = shoppingCart.getDishId(); LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(ShoppingCart::getUserId,userId); if (dishId!=null){ lambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);
}else { lambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId()); } ShoppingCart shoppingCartServiceOne = shoppingCartService.getOne(lambdaQueryWrapper);
Integer number = shoppingCartServiceOne.getNumber(); if (number>1){ Integer number1 = shoppingCartServiceOne.getNumber(); shoppingCartServiceOne.setNumber(number1-1); shoppingCartService.updateById(shoppingCartServiceOne); }else { shoppingCartService.removeById(shoppingCartServiceOne); } return R.success("删除成功"); }
|
清空购物车
清空购物车发出的请求为:
代码为:
1 2 3 4 5 6 7 8 9
| @DeleteMapping("/clean") public R<String> clean(){ Long userId = BaseContext.getId(); LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(ShoppingCart::getUserId,userId); shoppingCartService.remove(lambdaQueryWrapper); return R.success("购物车已清空"); }
|