1.新增分类
@Autowired
private CategoryService categoryService;
/**
* 新增分类
* @param category
* @return
*/
@PostMapping
public R<String> save(@RequestBody Category category){
log.info("Category:{}",category.toString());
categoryService.save(category);
return R.success("新增分类成功");
}
2.分页查询
/**
* category分页查询
* @param page
* @param pageSize
* @return
*/
@GetMapping("/page")
public R<Page> page(Integer page,Integer pageSize){
log.info("category 分页查询 page:{} pageSize{}",page,pageSize);
Page<Category> pageInfo = new Page<>(page,pageSize);
//查询条件
LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.orderByAsc(Category::getSort);
categoryService.page(pageInfo,queryWrapper);
return R.success(pageInfo);
}
3.删除分类
这里逻辑较为复杂,需要调用其他接口查询是否关联了菜品分类
因此写在service层
这里是controller层
/**
* 删除菜品分类
* @param ids
* @return
*/
@DeleteMapping
public R<String> delete(Long ids){
log.info("删除菜品分类id:{}",ids);
// categoryService.removeById(ids);
categoryService.remove(ids);
return R.success("分类菜品删除成功");
}
这里是service层
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
@Autowired
private DishService dishService;
@Autowired
private SetMealService setMealService;
/**
* 删除菜品分类
* @param id
*/
@Override
public void remove(Long id) {
//判断是否关联dish表菜品
LambdaQueryWrapper<Dish> dishQueryWrapper = new LambdaQueryWrapper<>();
dishQueryWrapper.eq(Dish::getCategoryId,id);
int count = dishService.count(dishQueryWrapper);
if(count > 0){
//抛出业务异常
throw new BusinessException("当前分类关联了菜品信息,无法删除");
}
//判断是否关联setMeal表菜品
LambdaQueryWrapper<Setmeal> setMealLambdaQueryWrapper = new LambdaQueryWrapper<>();
setMealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
int count1 = setMealService.count(setMealLambdaQueryWrapper);
//判断是否关联dish表菜品
if(count1 > 0){
//抛出业务异常
throw new BusinessException("当前分类关联了套餐信息,无法删除");
}
//执行删除
super.removeById(id);
}
}
自定义业务异常
public class BusinessException extends RuntimeException{
public BusinessException(String message){
super(message);
}
}
在抛出异常后由全局异常处理器捕获,返回给前端信息
/**
* 处理删除菜品分类时已经关联菜品或套餐的异常
* @param exception
* @return
*/
@ExceptionHandler(BusinessException.class)
public R<String> exceptionHandler(BusinessException exception){
log.info(exception.getMessage());
return R.error(exception.getMessage());
}
4.修改分类
/**
* 根据ID修改菜品分类
* @param category
* @return
*/
@PutMapping
public R<String> update(@RequestBody Category category){
log.info("修改菜品分类id:{}",category.getId());
categoryService.updateById(category);
return R.success("修改菜品分类信息成功");
}
5.图片的上传
注意这里前端上传的参数这里必须是MultipartFile file
为了存储图片名字不重复采用UUID随机字符串,为了灵活的存储,在配置文件里配置了存储路径
在存储前判断,不存在则创建该目录。
@Value("${reggie.path}")
private String basePath;
/**
* 上传图片
*
* @param file
* @return
*/
@PostMapping("/upload")
public R<String> upload(MultipartFile file) {
//获取的file临时文件,进行转存
//获取文件原名,截取小数点后的后缀
String originalFilename = file.getOriginalFilename();
//UUID生成不重复的文件名
String s = UUID.randomUUID().toString();
String fileName = s + originalFilename.substring(originalFilename.lastIndexOf("."));
//判断路径是否存在,不存在则创建
File dir = new File(basePath);
if (!dir.exists()) {
dir.mkdir();
}
try {
file.transferTo(new File(basePath + fileName));
log.info("转存图片到:{}", basePath + fileName);
} catch (IOException e) {
throw new RuntimeException(e);
}
return R.success(fileName);
}
截取文件原名后缀测试:
@Test
public void uploadTest(){
String s = UUID.randomUUID().toString().toString();
System.out.println(s);
String picName = "h.sa,.jh.glo.jpg";
String lastFileName = picName.substring(picName.lastIndexOf("."));
System.out.println(lastFileName);
}
d32be8df-eebf-4344-bbd8-5f1a014ff5b3
.jpg
6.图片下载
@GetMapping("/download")
public void download(String name, HttpServletResponse response) {
try {
//从文件读取
FileInputStream fileInputStream = new FileInputStream(new File(basePath + name));
//响应
ServletOutputStream outputStream = response.getOutputStream();
byte bytes[] = new byte[1024];
int len = 0;
while ((len = fileInputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, len);
outputStream.flush();
}
//关闭资源
fileInputStream.close();
outputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}