新增菜品
This commit is contained in:
@@ -0,0 +1,34 @@
|
|||||||
|
package com.sky.controller.admin;
|
||||||
|
|
||||||
|
import com.sky.dto.DishDTO;
|
||||||
|
import com.sky.result.Result;
|
||||||
|
import com.sky.service.DishService;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
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;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/admin/dish")
|
||||||
|
@Slf4j
|
||||||
|
@Api(tags = "菜品相关接口")
|
||||||
|
public class DishController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DishService dishService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新增菜品
|
||||||
|
* @param dishDTO
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@PostMapping
|
||||||
|
public Result save(@RequestBody DishDTO dishDTO){
|
||||||
|
log.info("新增菜品: {}",dishDTO);
|
||||||
|
dishService.saveWithFlavor(dishDTO);
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.sky.mapper;
|
||||||
|
|
||||||
|
import com.sky.entity.DishFlavor;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface DishFlavorMapper {
|
||||||
|
/**
|
||||||
|
* 批量插入口味数据
|
||||||
|
* @param flavors
|
||||||
|
*/
|
||||||
|
void insertBatch(List<DishFlavor> flavors);
|
||||||
|
}
|
||||||
14
sky-server/src/main/java/com/sky/service/DishService.java
Normal file
14
sky-server/src/main/java/com/sky/service/DishService.java
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package com.sky.service;
|
||||||
|
|
||||||
|
import com.sky.dto.DishDTO;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public interface DishService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新增菜品
|
||||||
|
* @param dishDTO
|
||||||
|
*/
|
||||||
|
void saveWithFlavor(DishDTO dishDTO);
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package com.sky.service.impl;
|
||||||
|
|
||||||
|
import com.sky.dto.DishDTO;
|
||||||
|
import com.sky.entity.Dish;
|
||||||
|
import com.sky.entity.DishFlavor;
|
||||||
|
import com.sky.mapper.DishFlavorMapper;
|
||||||
|
import com.sky.mapper.DishMapper;
|
||||||
|
import com.sky.service.DishService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.BeanUtils;
|
||||||
|
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.function.Consumer;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class DishServiceImpl implements DishService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DishMapper dishMapper;
|
||||||
|
@Autowired
|
||||||
|
private DishFlavorMapper dishFlavorMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新增菜品
|
||||||
|
*
|
||||||
|
* @param dishDTO
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Transactional
|
||||||
|
public void saveWithFlavor(DishDTO dishDTO) {
|
||||||
|
//向菜品表插入数据(一条)
|
||||||
|
Dish dish = new Dish();
|
||||||
|
BeanUtils.copyProperties(dishDTO, dish);
|
||||||
|
dishMapper.insert(dish);
|
||||||
|
|
||||||
|
//获取Insert语句生成的主键值
|
||||||
|
Long id = dish.getId();
|
||||||
|
|
||||||
|
//向口味表插入N条数据
|
||||||
|
List<DishFlavor> flavors = dishDTO.getFlavors();
|
||||||
|
if (flavors != null && !flavors.isEmpty()) {
|
||||||
|
//遍历flavors,插入id
|
||||||
|
flavors.forEach(dishFlavor -> dishFlavor.setDishId(id));
|
||||||
|
//批量插入
|
||||||
|
dishFlavorMapper.insertBatch(flavors);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
13
sky-server/src/main/resources/mapper/DishFlavorMapper.xml
Normal file
13
sky-server/src/main/resources/mapper/DishFlavorMapper.xml
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper
|
||||||
|
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<mapper namespace="com.sky.mapper.DishFlavorMapper">
|
||||||
|
|
||||||
|
<insert id="insertBatch">
|
||||||
|
insert into dish_flavor (dish_id, name, value) VALUES
|
||||||
|
<foreach collection="flavors" item="df" separator=",">
|
||||||
|
(#{df.dishId},#{df.name},#{df.value})
|
||||||
|
</foreach>
|
||||||
|
</insert>
|
||||||
|
</mapper>
|
||||||
13
sky-server/src/main/resources/mapper/DishMapper.xml
Normal file
13
sky-server/src/main/resources/mapper/DishMapper.xml
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper
|
||||||
|
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<mapper namespace="com.sky.mapper.DishMapper">
|
||||||
|
|
||||||
|
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
|
||||||
|
insert into dish (name, category_id, price, image, description, status, create_time, update_time, create_user,
|
||||||
|
update_user)
|
||||||
|
values (#{name}, #{categoryId}, #{price}, #{image}, #{description}, #{status}, #{createTime}, #{updateTime},
|
||||||
|
#{createUser}, #{updateUser})
|
||||||
|
</insert>
|
||||||
|
</mapper>
|
||||||
Reference in New Issue
Block a user