三. 导入商品浏览功能代码
由于user的Controller与admin的相同,记得修改RestController注释
1. 查询分类
CategoryController
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 {@Autowiredprivate 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
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 {@Autowiredprivate 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
/**
* 根据菜品id查询菜品
*/
List<DishVO> listWithFlavor(Dish dish);
Impl
/*** 根据菜品id查询菜品*/@Overridepublic 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;}