根据id删除分类
1 CategoryController中添加deleteById方法,接收前端请求,并调用categoryService.deleteById处理
@DeleteMapping
@ApiOperation("根据id删除分类")
public Result<String> deleteById(Long id) {
log.info("根据id删除分类:{}", id);
categoryService.deleteById(id);
return Result.success();
}
2 categoryService接口中添加deleteById方法
public void deleteById(Long id) {
3 categoryService的实现类CategoryServiceImpl实现deleteById方法
public void deleteById(Long id) {
Integer count = dishMapper.countByCategoryId(id);
if(count > 0) {
throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
}
count = SetmealMapper.countByCategoryId(id);
if(count > 0) {
throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
}
categoryMapper.deleteById(id);
}
4 dishMapper中添加根据分类id查询countByCategoryId(id)
@Select("select count(id) from dish where category_id = #{categoryId}")
Integer countByCategoryId(Long categoryId);
5 SetmealMapper中添加根据分类id查询countByCategoryId(id)
@Select("select count(id) from setmeal where category_id = #{categoryId}")
Integer countByCategoryId(Long id);
6 categoryMapper中添加根据id删除deleteById
@Delete("delete from category where id = #{id}")
void deleteById(Long id);