瑞吉外卖项目剩余功能补充
目录
- 菜品启售和停售
- 菜品批量启售和批量停售
- 菜品的批量删除
这个是自己基于学习B站 黑马瑞吉外卖项目,补充一些视频里面没有定义的功能或者是需要自己实现的功能。仅供学习参考,本人可能代码不太规范,但是功能自己测试是没有问题的。
引用站外地址
黑马程序员Java项目实战《瑞吉外卖》
轻松掌握springboot + mybatis plus开发核心技术的真java实战项目_哔哩哔哩_bilibili
菜品启售和停售
前端发过来的请求(使用的是post方式):http://localhost:8080/dish/status/1?ids=1516568538387079169
后端接受的请求:
1 2 3 4 5 6
| @PostMapping("/status/{status}") public R<String> status(@PathVariable("status") Integer status,Long ids){ log.info("status:{}",status); log.info("ids:{}",ids); return null; }
|
发现可以接收到前端参数后,开始补全controller层代码:在DishController中添加下面的接口代码;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
@PostMapping("/status/{status}") public R<String> status(@PathVariable("status") Integer status,Long ids){ log.info("status:{}",status); log.info("ids:{}",ids); Dish dish = dishService.getById(ids); if (dish != null){ dish.setStatus(status); dishService.updateById(dish); return R.success("开始启售"); } return R.error("售卖状态设置异常"); }
|
菜品批量启售和批量停售
把上面对单个菜品的售卖状态的方法进行修改;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
@PostMapping("/status/{status}")
public R<String> status(@PathVariable("status") Integer status,@RequestParam List<Long> ids){ LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper(); queryWrapper.in(ids !=null,Dish::getId,ids); List<Dish> list = dishService.list(queryWrapper); for (Dish dish : list) { if (dish != null){ dish.setStatus(status); dishService.updateById(dish); } } return R.success("售卖状态修改成功"); }
|
**注意:controller层的代码是不可以直接写业务的,建议把它抽离到service层,controller调用一下service的方法就行;**下面的批量删除功能是抽离的,controller没有写业务代码;
菜品的批量删除
前端发来的请求:
http://localhost:8080/dish?ids=1516568538387079169,1516568381817905154
在DishController中添加接口:
在DishFlavor实体类中,在private Integer isDeleted;字段上加上@TableLogic注解,表示删除是逻辑删除,由mybatis-plus提供的;
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
@DeleteMapping public R<String> delete(@RequestParam("ids") List<Long> ids){ dishService.deleteByIds(ids); LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.in(DishFlavor::getDishId,ids); dishFlavorService.remove(queryWrapper); return R.success("菜品删除成功"); }
|
DishService中添加相关的方法:
1 2
| void deleteByIds(List<Long> ids);
|
在实现类实现该方法:
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
|
@Override @Transactional public void deleteByIds(List<Long> ids) { LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.in(ids!=null,Dish::getId,ids); List<Dish> list = this.list(queryWrapper); for (Dish dish : list) { Integer status = dish.getStatus(); if (status == 0){ this.removeById(dish.getId()); }else { throw new CustomException("删除菜品中有正在售卖菜品,无法全部删除"); } } }
|
功能测试:单个删除,批量删除,批量删除中有启售的…
测试成功!