forked from Q-1515/reggie_parent
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
116 additions
and
2 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
reggie_server/src/main/java/com/reggie/controller/admin/DishController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.reggie.controller.admin; | ||
|
||
import com.reggie.dto.DishDTO; | ||
import com.reggie.result.R; | ||
import com.reggie.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 | ||
@RequestMapping("/admin/dish") | ||
@Slf4j | ||
@Api(tags = "菜品相关接口") | ||
public class DishController { | ||
|
||
@Autowired | ||
private DishService dishService; | ||
|
||
|
||
/** | ||
* 添加菜品 | ||
* @param dishDTO 分类id,菜品描述,口味,菜品id,菜品图片路径,菜品名称,菜品价格,菜品状态 | ||
* @return success | ||
*/ | ||
@PostMapping | ||
@ApiOperation("新增菜品") | ||
public R<String> save(@RequestBody DishDTO dishDTO) { | ||
log.info("添加菜品:{}",dishDTO); | ||
dishService.save(dishDTO); | ||
return R.success("菜品添加成功"); | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
reggie_server/src/main/java/com/reggie/mapper/DishFlavorMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
package com.reggie.mapper; | ||
|
||
import com.reggie.entity.DishFlavor; | ||
import org.apache.ibatis.annotations.Mapper; | ||
import org.apache.ibatis.annotations.Param; | ||
|
||
import java.util.List; | ||
|
||
@Mapper | ||
public interface DishFlavorMapper { | ||
|
||
//插入菜品口味 | ||
void insert(@Param("flavors") List<DishFlavor> flavors); | ||
} |
8 changes: 8 additions & 0 deletions
8
reggie_server/src/main/java/com/reggie/mapper/DishMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,18 @@ | ||
package com.reggie.mapper; | ||
|
||
import com.reggie.annotation.AutoFill; | ||
import com.reggie.constant.AutoFillConstant; | ||
import com.reggie.entity.Dish; | ||
import org.apache.ibatis.annotations.Mapper; | ||
|
||
@Mapper | ||
public interface DishMapper { | ||
|
||
//根据分类id查询对应的菜品数量 | ||
Long cuntByCategoryId(Long id); | ||
|
||
|
||
@AutoFill(type = AutoFillConstant.INSERT) | ||
//插入数据到菜品表 | ||
void insert(Dish dish); | ||
} |
4 changes: 4 additions & 0 deletions
4
reggie_server/src/main/java/com/reggie/service/DishService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
package com.reggie.service; | ||
|
||
import com.reggie.dto.DishDTO; | ||
|
||
public interface DishService { | ||
|
||
//添加菜品 | ||
void save(DishDTO dishDTO); | ||
} |
42 changes: 40 additions & 2 deletions
42
reggie_server/src/main/java/com/reggie/service/impl/DishServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,59 @@ | ||
package com.reggie.service.impl; | ||
|
||
import com.reggie.dto.DishDTO; | ||
import com.reggie.entity.Dish; | ||
import com.reggie.entity.DishFlavor; | ||
import com.reggie.mapper.DishFlavorMapper; | ||
import com.reggie.mapper.DishMapper; | ||
import com.reggie.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 java.util.List; | ||
|
||
/** | ||
* 菜品业务实现 | ||
*/ | ||
@Service | ||
@Slf4j | ||
public class DishServiceImpl implements DishService { | ||
@Autowired | ||
private DishMapper dishMapper; | ||
private DishMapper dishMapper; //菜品Dao | ||
@Autowired | ||
private DishFlavorMapper dishFlavorMapper; | ||
private DishFlavorMapper dishFlavorMapper; //菜品味道Dao | ||
|
||
/** | ||
* 添加菜品 | ||
* | ||
* @param dishDTO 分类id,菜品描述,口味,菜品id,菜品图片路径,菜品名称,菜品价格,菜品状态 | ||
*/ | ||
public void save(DishDTO dishDTO) { | ||
//菜品实体类 | ||
Dish dish = new Dish(); | ||
BeanUtils.copyProperties(dishDTO, dish); | ||
//插入数据到菜品表 | ||
dishMapper.insert(dish); | ||
|
||
//获取菜品主键值,需要保存到口味表中 | ||
Long dishId = dish.getId(); | ||
|
||
//菜品口味 | ||
List<DishFlavor> flavors = dishDTO.getFlavors(); | ||
if (flavors == null || flavors.size() == 0) { | ||
return; | ||
} | ||
|
||
//循环添加id | ||
flavors.forEach( item -> { | ||
//设置口味关联的菜品id | ||
item.setDishId(dishId); | ||
}); | ||
|
||
//插入菜品口味 | ||
dishFlavorMapper.insert(flavors); | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,12 @@ | ||
<?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.reggie.mapper.DishFlavorMapper"> | ||
|
||
<!--插入菜品口味--> | ||
<insert id="insert"> | ||
insert into dish_flavor(dish_id, name, value) values | ||
<foreach collection="flavors" item="flavor" separator=","> | ||
(#{flavor.dishId},#{flavor.name},#{flavor.value}) | ||
</foreach> | ||
</insert> | ||
</mapper> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters