三. 导入商品浏览功能代码
由于user的Controller与admin的相同,记得修改RestController注释
1. 查询分类
CategoryController
java
package com.sky.controller.user;
import com.sky.entity.Category;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author Jie.
* @description: TODO
* @date 2024/9/24
* @version: 1.0
*/
@RestController("userCategoryController")
@RequestMapping("/user/category")
@Api(tags = "分类管理")
@Slf4j
public class CategoryController {
@Autowired
private CategoryService categoryService;
@GetMapping("/list")
@ApiOperation("条件查询")
public Result<List<Category>> listCategory(Integer type) {
log.info("条件查询:type={}", type);
List<Category> list = categoryService.listCategory(type);
return Result.success(list);
}
}
业务层方法与admin的相同,使用同一个
2. 根据分类ID查询菜品
Controller
java
package com.sky.controller.user;
import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author Jie.
* @description: TODO
* @date 2024/9/25
* @version: 1.0
*/
@RestController("userDishController")
@RequestMapping("/user/dish")
@Api(tags = "菜品管理")
@Slf4j
public class DishController {
@Autowired
private DishService dishService;
/**
* 根据分类id查询菜品
*/
@GetMapping("/list")
@ApiOperation("根据分类id查询菜品")
public Result<List<DishVO>> list(Long categoryId) {
Dish dish = new Dish();
dish.setCategoryId(categoryId);
dish.setStatus(StatusConstant.ENABLE);
List<DishVO> list = dishService.listWithFlavor(dish);
return Result.success(list);
}
}
Service
java
/**
* 根据菜品id查询菜品
*/
List<DishVO> listWithFlavor(Dish dish);
Impl
java
/**
* 根据菜品id查询菜品
*/
@Override
public List<DishVO> listWithFlavor(Dish dish) {
// 查询菜品
LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Dish::getCategoryId, dish.getCategoryId());
queryWrapper.eq(Dish::getStatus, StatusConstant.ENABLE);
List<Dish> dishes = dishMapper.selectList(queryWrapper);
List<DishVO> dishVOList = new ArrayList<>();
for (Dish d : dishes) {
DishVO dishVO = new DishVO();
BeanUtils.copyProperties(d, dishVO);
// 查询口味
LambdaQueryWrapper<DishFlavor> flavorQueryWrapper = new LambdaQueryWrapper<>();
flavorQueryWrapper.eq(DishFlavor::getDishId, d.getId());
List<DishFlavor> flavors = dishFlavorMapper.selectList(flavorQueryWrapper);
// 封装结果
dishVO.setFlavors(flavors);
dishVOList.add(dishVO);
}
return dishVOList;
}