个人名片:
博主:酒徒ᝰ.
专栏:瑞吉外卖
个人简介:沉醉在酒中,借着一股酒劲,去拼搏一个未来。
本篇励志:一份耕耘,一份收获。眼要看远,脚要近迈。在强者的眼中,没有最好,只有更好。向竞争挑战,向压力挑战,更要向自己挑战时间就是一切。
本项目基于B站黑马程序员Java项目实战《瑞吉外卖》,轻松掌握springboot + mybatis plus开发核心技术的真java实战项目。
视频链接【黑马程序员Java项目实战《瑞吉外卖》,轻松掌握springboot + mybatis
plus开发核心技术的真java实战项目】 https://www.bilibili.com/video/BV13a411q753?
点击观看
1.显示购物车
在手机端显示中已经完成。、
2.加入购物车
分析:shoppingCart地址,POST方式,add地址
/**
* 添加菜品或套餐
* @param shoppingCart
* @param session
* @return
*/
@PostMapping("/add")
public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart, HttpSession session){
//log.info("shoppingCart:{}", shoppingCart);//shoppingCart:ShoppingCart(id=null, name=邵阳猪血丸子, image=2a50628e-7758-4c51-9fbb-d37c61cdacad.jpg, userId=null, dishId=1397851370462687234, setmealId=null, dishFlavor=多冰,不要葱,不辣, number=null, amount=138, createTime=null)
//设置用户id
shoppingCart.setUserId((Long) session.getAttribute("user"));
//设置该购物车创建时间
shoppingCart.setCreateTime(LocalDateTime.now());
//需要判断菜品或套餐是否在购物车里
//1.区分添加的是菜品还是套餐
LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
Long dishId = shoppingCart.getDishId();
if (dishId != null){
//菜品
queryWrapper.eq(ShoppingCart::getDishId, dishId);
}else {
//套餐
queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
}
//2.查看购物车内该菜品或套餐是否为空
ShoppingCart shopping = shoppingCartService.getOne(queryWrapper);
if (shopping == null){
shoppingCart.setNumber(1);
shoppingCartService.save(shoppingCart);
shopping = shoppingCart;
}else {
shopping.setNumber(shopping.getNumber() + 1);
shoppingCartService.updateById(shopping);
}
return R.success(shopping);
}
3.清空购物车
分析:shoppingCart地址,DELETE方式,clean地址
/**
* 清空购物车
* @param session
* @return
*/
@DeleteMapping("/clean")
public R<String> clean(HttpSession session){
//根据用户id清空购物车
LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(ShoppingCart::getUserId, (Long)session.getAttribute("user"));
shoppingCartService.remove(queryWrapper);
return R.success("清空购物车成功");
}
4.购物车内减少菜品或套餐
分析:shoppingCart地址,POST方式,sub地址
这里与添加菜品与套餐类似,直接复制过来进行简单修改即可。注意购物车内菜品或套餐数量一旦为0,直接删除该菜品或套餐。
/**
* 减少菜品或套餐
* @param shoppingCart
* @param session
* @return
*/
@PostMapping("/sub")
public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart, HttpSession session){
//log.info("shoppingCart:{}", shoppingCart);//shoppingCart:ShoppingCart(id=null, name=邵阳猪血丸子, image=2a50628e-7758-4c51-9fbb-d37c61cdacad.jpg, userId=null, dishId=1397851370462687234, setmealId=null, dishFlavor=多冰,不要葱,不辣, number=null, amount=138, createTime=null)
//需要判断菜品或套餐是否在购物车里
//1.区分减少的是菜品还是套餐
LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
Long dishId = shoppingCart.getDishId();
if (dishId != null){
//菜品
queryWrapper.eq(ShoppingCart::getDishId, dishId);
}else {
//套餐
queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
}
//2.查看购物车内该菜品或套餐数量是否大于等于1
ShoppingCart shopping = shoppingCartService.getOne(queryWrapper);
if (shopping.getNumber() > 1){
shopping.setNumber(shopping.getNumber() - 1);
shoppingCartService.updateById(shopping);
}else if (shopping.getNumber() == 1){
shopping.setNumber(shopping.getNumber() - 1);
shoppingCartService.removeById(shopping);
}
return R.success(shopping);
}