苍穹外卖--新增菜品-功能实现
This commit is contained in:
@@ -0,0 +1,33 @@
|
|||||||
|
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 io.swagger.annotations.ApiOperation;
|
||||||
|
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
|
||||||
|
@Slf4j
|
||||||
|
@Api(tags = "菜品相关接口")
|
||||||
|
@RequestMapping("/admin/dish")
|
||||||
|
public class DishController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DishService dishService;
|
||||||
|
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
@ApiOperation("新增菜品")
|
||||||
|
public Result add(@RequestBody DishDTO dto){
|
||||||
|
log.info("新增菜品:{}",dto);
|
||||||
|
dishService.saveDishWitchFlavor(dto);
|
||||||
|
|
||||||
|
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 insertBath(List<DishFlavor> flavors);
|
||||||
|
}
|
||||||
@@ -1,6 +1,10 @@
|
|||||||
package com.sky.mapper;
|
package com.sky.mapper;
|
||||||
|
|
||||||
|
import com.sky.annotation.AutoFill;
|
||||||
|
import com.sky.entity.Dish;
|
||||||
|
import com.sky.enumeration.OperationType;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Options;
|
||||||
import org.apache.ibatis.annotations.Select;
|
import org.apache.ibatis.annotations.Select;
|
||||||
|
|
||||||
@Mapper
|
@Mapper
|
||||||
@@ -14,4 +18,10 @@ public interface DishMapper {
|
|||||||
@Select("select count(id) from dish where category_id = #{categoryId}")
|
@Select("select count(id) from dish where category_id = #{categoryId}")
|
||||||
Integer countByCategoryId(Long categoryId);
|
Integer countByCategoryId(Long categoryId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新增菜品
|
||||||
|
* @param dish
|
||||||
|
*/
|
||||||
|
@AutoFill(OperationType.INSERT)
|
||||||
|
void insert(Dish dish);
|
||||||
}
|
}
|
||||||
|
|||||||
10
sky-server/src/main/java/com/sky/service/DishService.java
Normal file
10
sky-server/src/main/java/com/sky/service/DishService.java
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package com.sky.service;
|
||||||
|
|
||||||
|
import com.sky.dto.DishDTO;
|
||||||
|
|
||||||
|
public interface DishService {
|
||||||
|
/*
|
||||||
|
新增菜品和它的口味
|
||||||
|
*/
|
||||||
|
void saveDishWitchFlavor(DishDTO dto);
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
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 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;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class DishServiceImpl implements DishService {
|
||||||
|
@Autowired
|
||||||
|
private DishMapper dishMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DishFlavorMapper dishFlavorMapper;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
新增菜品和它的口味
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Transactional //启用spring的事务操作,让菜品的添加和口味的添加,同时成功或失败
|
||||||
|
public void saveDishWitchFlavor(DishDTO dto) {
|
||||||
|
//添加一条Dish数据
|
||||||
|
Dish dish = new Dish();
|
||||||
|
BeanUtils.copyProperties(dto,dish);
|
||||||
|
//注意:新增菜品时,要通过主键回显,获取数据库生成的id
|
||||||
|
dishMapper.insert(dish);
|
||||||
|
//如果有口味数据,添加DishFlavor数据
|
||||||
|
List<DishFlavor> flavors = dto.getFlavors();
|
||||||
|
if (flavors != null && flavors.size() > 0) {
|
||||||
|
//插入菜品口味数据之前,先将菜品的id补充完整
|
||||||
|
// flavors.forEach(flaver->flaver.setDishId(dish.getId()));
|
||||||
|
for (DishFlavor flavor : flavors) {
|
||||||
|
flavor.setDishId(dish.getId());
|
||||||
|
}
|
||||||
|
//菜品口味的插入(可以遍历逐个insert,也可以批量插入)
|
||||||
|
dishFlavorMapper.insertBath(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="insertBath">
|
||||||
|
insert into dish_flavor(dish_id, name, value)
|
||||||
|
values
|
||||||
|
<foreach collection="flavors" item="flavor" separator=",">
|
||||||
|
(#{flavor.dishId},#{flavor.name},#{flavor.value})
|
||||||
|
</foreach>
|
||||||
|
</insert>
|
||||||
|
</mapper>
|
||||||
10
sky-server/src/main/resources/mapper/DishMapper.xml
Normal file
10
sky-server/src/main/resources/mapper/DishMapper.xml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<?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, create_time, update_time, create_user, update_user,status)
|
||||||
|
values (#{name},#{categoryId},#{price},#{image},#{description},#{createTime},#{updateTime},#{createUser},#{updateUser},#{status})
|
||||||
|
</insert>
|
||||||
|
</mapper>
|
||||||
Reference in New Issue
Block a user