01、菜品展示
1、需求分析
根据分类id查询套餐信息
- 请求地址:
http://localhost:8080/setmeal/list?categoryId=分类id&status=1
- 请求类型:
GET
- 请求参数:
categoryId
2、代码实现
02.改造根据条件查询菜品数据
位置:
DishController
/**
* 根据条件查询菜品数据
* @param dish
* @return
*/
@GetMapping("/list")
public R<List<DishDto>> list(Dish dish){
// 构造查询条件
LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
// 查询状态为 1(起售)
queryWrapper.eq(Dish::getStatus, 1);
// 添加排序条件
queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
List<Dish> list = dishService.list(queryWrapper);
List<DishDto> dishDtoList = list.stream().map((item) -> {
DishDto dishDto = new DishDto();
BeanUtils.copyProperties(item, dishDto);
// 分类id
Long categoryId = item.getCategoryId();
// 根据id查询分类对象
Category category = categoryService.getById(categoryId);
if (category != null){
String categoryName = category.getName();
dishDto.setCategoryName(categoryName);
}
// 当前菜品id
Long dishId = item.getId();
LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(DishFlavor::getDishId, dishId);
List<DishFlavor> dishFlavorList = dishFlavorService.list(lambdaQueryWrapper);
dishDto.setFlavors(dishFlavorList);
return dishDto;
}).collect(Collectors.toList());
return R.success(dishDtoList);
}
02.根据分类id查询套餐信息
位置:
SetmealController
/**
* 根据条件查询套餐数据
* @param setmeal
* @return
*/
@GetMapping("/list")
public R<List<Setmeal>> list(Setmeal setmeal){
LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
queryWrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
queryWrapper.orderByDesc(Setmeal::getUpdateTime);
List<Setmeal> list = setmealService.list(queryWrapper);
return R.success(list);
}
3、测试功能
01.临时添加静态数据
cartData.json
目录:
src/main/resources/front/cartData.json
{"code":1,"msg":null,"data":[],"map":{}}
02.临时修改
cartListApi
目录:
src/main/resources/front/api/main.js
//获取购物车内商品的集合
function cartListApi(data) {
return $axios({
// 'url': '/shoppingCart/list',
'url': '/front/cartData.json',
'method': 'get',
params:{...data}
})
}
02、购物车添加
1、需求分析
- 请求地址:
http://localhost:8080/shoppingCart/add
- 请求类型:
POST
2、代码实现
01.ShoppingCart实体类
package cn.mu00.reggie.entity;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
/**
* 购物车
*/
@Data
public class ShoppingCart implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
//名称
private String name;
//用户id
private Long userId;
//菜品id
private Long dishId;
//套餐id
private Long setmealId;
//口味
private String dishFlavor;
//数量
private Integer number;
//金额
private BigDecimal amount;
//图片
private String image;
private LocalDateTime createTime;
}
02.ShoppingCartMapper
package cn.mu00.reggie.mapper;
import cn.mu00.reggie.entity.ShoppingCart;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface ShoppingCartMapper extends BaseMapper<ShoppingCart> {
}
03.ShoppingCartService
package cn.mu00.reggie.service;
import cn.mu00.reggie.entity.ShoppingCart;
import com.baomidou.mybatisplus.extension.service.IService;
public interface ShoppingCartService extends IService<ShoppingCart> {
}
04.ShoppingCartServiceImpl
package cn.mu00.reggie.service.impl;
import cn.mu00.reggie.entity.ShoppingCart;
import cn.mu00.reggie.mapper.ShoppingCartMapper;
import cn.mu00.reggie.service.ShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
}
05.ShoppingCartController
package cn.mu00.reggie.controller;
import cn.mu00.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
@Autowired
private ShoppingCartService shoppingCartService;
}
06.购物车添加
位置:
ShoppingCartController
/**
* 购物车添加
* @param shoppingCart
* @return
*/
@PostMapping("/add")
public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
log.info("购物车数据:{}", shoppingCart);
// 设置用户id
Long currentId = BaseContext.getCurrentId();
shoppingCart.setUserId(currentId);
// 查询当前菜品或者套餐是否在购物车中
Long dishId = shoppingCart.getDishId();
LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(ShoppingCart::getUserId, currentId);
if (dishId != null){
// 存在是菜品
queryWrapper.eq(ShoppingCart::getDishId, dishId);
}else {
// 不存在是套餐
queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
}
ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
if (cartServiceOne != null){
// 存在,则原数量+1
Integer number = cartServiceOne.getNumber();
cartServiceOne.setNumber(number + 1);
shoppingCartService.updateById(cartServiceOne);
}else {
// 不存在,则添加数据
shoppingCart.setNumber(1);
shoppingCart.setCreateTime(LocalDateTime.now());
shoppingCartService.save(shoppingCart);
cartServiceOne = shoppingCart;
}
return R.success(cartServiceOne);
}
03、购物车查询
恢复菜品展示的修改
1、需求分析
- 请求地址:
http://localhost:8080/shoppingCart/list
- 请求类型:
GET
2、代码实现
/**
* 根据用户id查询购物车
* @return
*/
@GetMapping("/list")
public R<List<ShoppingCart>> list(){
// 用户id
Long currentId = BaseContext.getCurrentId();
LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(ShoppingCart::getUserId, currentId);
queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
return R.success(list);
}
03、购物车修改
1、需求分析
- 请求地址:
http://localhost:8080/shoppingCart/sub
- 请求类型:
POST
- 请求参数:
dishId
或setmealId
2、代码实现
/**
* 购物车更新
* @param shoppingCart
* @return
*/
@PostMapping("/sub")
public R<String> sub(@RequestBody ShoppingCart shoppingCart){
Long currentId = BaseContext.getCurrentId();
// 查询当前菜品或者套餐是否在购物车中
Long dishId = shoppingCart.getDishId();
LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(ShoppingCart::getUserId, currentId);
if (dishId != null){
// 存在是菜品
queryWrapper.eq(ShoppingCart::getDishId, dishId);
}else {
// 不存在是套餐
queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
}
ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
Integer number = cartServiceOne.getNumber();
// 判断数量是否为1
if (number == 1){
// 删除
shoppingCartService.remove(queryWrapper);
}else {
// 减少
cartServiceOne.setNumber(number - 1);
shoppingCartService.updateById(cartServiceOne);
}
return R.success("更新成功");
}
03、购物车清空
1、需求分析
- 请求地址:
http://localhost:8080/shoppingCart/clean
- 请求类型:
DELETE
2、代码实现
/**
* 清空购物车
* @return
*/
@DeleteMapping("/clean")
public R<String> clean(){
Long currentId = BaseContext.getCurrentId();
LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(ShoppingCart::getUserId, currentId);
shoppingCartService.remove(queryWrapper);
return R.success("清空购物车成功");
}