瑞吉外卖-移动端购物车功能

购物车

需求分析

image-20220817163444795

交互过程

image-20220817165222123

代码开发-准备工作

image-20220817165249842

添加购物车

流程:

  • 根据前端发送过来的json数据可以发现,用户id并没有传送过来,所以先获取用户的id,并设置userid
  • 查询添加购物车的数据是菜品还是套餐
  • 判断购物车中是否已经存在目前要加入的菜品或菜单
    • 如果已经存在,则在购物车中,在原来商品的基础上,数量+1
    • 如果不存在,则保存到购物车数据库中。

请求方式:

image-20220818095409805

代码为:

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
/**
* 添加购物车
* @param shoppingCart
* @return
*/
@PostMapping("/add")
public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){

//设置用户id,指定当前哪个用户的购物车数据
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){
//如果有,则数量+1
Integer number = shoppingCartServiceOne.getNumber();
shoppingCartServiceOne.setNumber(number+1);
shoppingCartService.updateById(shoppingCartServiceOne);
}else {
//如果没有,则添加购物车
shoppingCart.setNumber(1);
shoppingCartService.save(shoppingCart);
//因为数据库中该菜品或套餐,所以shoppingCartServiceOne为null,为了方便返回,可以将shoppingCart赋值给shoppingCartServiceOne
shoppingCartServiceOne = shoppingCart;
}

return R.success(shoppingCartServiceOne);
}

查看购物车

添加购物车功能已经实现完成,所以将之前手写的哪个cartData.json改回原来的访问地址,进行功能的实现

image-20220818091346849

ShoppingCartController.java代码为:

1
2
3
4
5
6
7
8
9
10
@GetMapping("/list")
public R<List<ShoppingCart>> list(){
//获取用户ID
Long userId = BaseContext.getId();
//根据用户ID查询购物车数据
LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(ShoppingCart::getUserId,userId);
List<ShoppingCart> list = shoppingCartService.list(lambdaQueryWrapper);
return R.success(list);
}

减少购物车中商品的数量

减少购物车中商品的数量所发出的请求为:

image-20220818094805125

代码为:

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){
//获取用户id
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){
//如果大于1,则数量-1
Integer number1 = shoppingCartServiceOne.getNumber();
shoppingCartServiceOne.setNumber(number1-1);
shoppingCartService.updateById(shoppingCartServiceOne);
}else {
//如果是1,则从购物车中删除,
shoppingCartService.removeById(shoppingCartServiceOne);
}
return R.success("删除成功");
}

清空购物车

清空购物车发出的请求为:

image-20220818092525048

代码为:

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);
//delete from shoppingcart where userid = ?;
shoppingCartService.remove(lambdaQueryWrapper);
return R.success("购物车已清空");
}

瑞吉外卖-移动端购物车功能
http://example.com/2022/08/17/瑞吉外卖-day05/
作者
zlw
发布于
2022年8月17日
许可协议