瑞吉外卖(16) - 新增菜品功能开发

272 篇文章 4 订阅
32 篇文章 3 订阅

需求分析

在这里插入图片描述
新增完后在手机端显示
在这里插入图片描述

数据模型

在这里插入图片描述
在这里插入图片描述

dish表

在这里插入图片描述

dis flaow表

在这里插入图片描述

代码开发

在这里插入图片描述

搭建框架

新增dishFlavor实体类

package com.taotao.reggie.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;

/**
菜品口味
 */
@Data
public class DishFlavor implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;


    //菜品id
    private Long dishId;


    //口味名称
    private String name;


    //口味数据list
    private String value;


    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime;


    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;


    @TableField(fill = FieldFill.INSERT)
    private Long createUser;


    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Long updateUser;


    //是否删除
    private Integer isDeleted;

}

新增Mapper接口DishFlavorMapper

package com.taotao.reggie.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.taotao.reggie.entity.DishFlavor;
import org.apache.ibatis.annotations.Mapper;

/**
 * create by 刘鸿涛
 * 2022/9/23 19:32
 */
@SuppressWarnings({"all"})
@Mapper
public interface DishFlavorMapper extends BaseMapper<DishFlavor> {
    
}

新增业务接口DishFlavorService

package com.taotao.reggie.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.taotao.reggie.entity.DishFlavor;

/**
 * create by 刘鸿涛
 * 2022/9/23 19:33
 */
@SuppressWarnings({"all"})
public interface DishFlavorService extends IService<DishFlavor> {
    
}

新增业务层实现类DishFlavorServiceImpl

package com.taotao.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taotao.reggie.entity.DishFlavor;
import com.taotao.reggie.mapper.DishFlavorMapper;
import com.taotao.reggie.service.DishFlavorService;
import org.springframework.stereotype.Service;

/**
 * create by 刘鸿涛
 * 2022/9/23 19:35
 */
@SuppressWarnings({"all"})
@Service
public class DishFlavorServiceImpl extends ServiceImpl<DishFlavorMapper, DishFlavor> implements DishFlavorService {

}


新增控制层DishController

package com.taotao.reggie.controller;

import com.taotao.reggie.service.DishFlavorService;
import com.taotao.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * create by 刘鸿涛
 * 2022/9/23 19:37
 */
@SuppressWarnings({"all"})
/**
 * 菜品管理
 */
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    
}

梳理交互过程

在这里插入图片描述

编辑CategoryControoler

    /**
     * 根据条件查询分类数据
     * @param category
     * @return
     */
    @GetMapping("list")
    public R<List<Category>> list(Category category){
        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.eq(category.getType() != null,Category::getType,category.getType());
        //添加排序条件            根据Category的sort进行排序        根据category的更新时间进行排序
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> list = categoryService.list(queryWrapper);
        return R.success(list);
    }

下拉框功能测试

在这里插入图片描述

导入DTO

在这里插入图片描述
在这里插入图片描述

package com.taotao.reggie.dto;

import com.taotao.reggie.entity.Dish;
import com.taotao.reggie.entity.DishFlavor;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;

@Data
public class DishDto extends Dish {

    private List<DishFlavor> flavors = new ArrayList<>();

    private String categoryName;

    private Integer copies;
}

编写DishController

package com.taotao.reggie.controller;

import com.taotao.reggie.common.R;
import com.taotao.reggie.dto.DishDto;
import com.taotao.reggie.service.DishFlavorService;
import com.taotao.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * create by 刘鸿涛
 * 2022/9/23 19:37
 */
@SuppressWarnings({"all"})
/**
 * 菜品管理
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        log.info(dishDto.toString());
        return null;
    }
}

添加菜品测试

在这里插入图片描述

编写DishService接口

在这里插入图片描述

编写DishServiceImpl

package com.taotao.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taotao.reggie.dto.DishDto;
import com.taotao.reggie.entity.Dish;
import com.taotao.reggie.entity.DishFlavor;
import com.taotao.reggie.mapper.DishMapper;
import com.taotao.reggie.service.DishFlavorService;
import com.taotao.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * create by 刘鸿涛
 * 2022/6/8 8:21
 */
@SuppressWarnings({"all"})
@Slf4j
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    /**
     * 新增菜品,同时保存对应的口味数据
     * @param dishDto
     */
    @Transactional
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表
        this.save(dishDto);

        Long dishId = dishDto.getId();  //菜品id

        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.stream().map((item) ->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(dishDto.getFlavors());
    }
}

编写ReggieApplication

在这里插入图片描述

完善DishController

在这里插入图片描述

功能测试

添加成功
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鬼鬼骑士

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值