完成新增菜品功能
This commit is contained in:
parent
ee83d06372
commit
fe98f53a71
@ -0,0 +1,28 @@
|
||||
package com.sky.config;
|
||||
|
||||
import com.sky.properties.AliOssProperties;
|
||||
import com.sky.utils.AliOssUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 配置类,用于创建AliOssUtil对象
|
||||
*/
|
||||
@Configuration
|
||||
@Slf4j
|
||||
public class OssConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public AliOssUtil aliOssUtil(AliOssProperties aliOssProperties) {
|
||||
log.info("开始创建阿里云文件上传工具类对象:{}", aliOssProperties);
|
||||
return new AliOssUtil(
|
||||
aliOssProperties.getEndpoint(),
|
||||
aliOssProperties.getAccessKeyId(),
|
||||
aliOssProperties.getAccessKeySecret(),
|
||||
aliOssProperties.getBucketName()
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package com.sky.controller.admin;
|
||||
|
||||
import com.sky.constant.MessageConstant;
|
||||
import com.sky.result.Result;
|
||||
import com.sky.utils.AliOssUtil;
|
||||
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.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 通用接口
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/admin/common")
|
||||
@Api(tags = "通用接口")
|
||||
@Slf4j
|
||||
public class CommonController {
|
||||
|
||||
@Autowired
|
||||
private AliOssUtil aliOssUtil;
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
* @param file
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/upload")
|
||||
@ApiOperation("文件上传")
|
||||
public Result<String> upload(MultipartFile file) {
|
||||
log.info("文件上传:{}", file);
|
||||
|
||||
try {
|
||||
//原始文件名
|
||||
String originalFilename = file.getOriginalFilename();
|
||||
//截取原始文件的扩展名
|
||||
String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
|
||||
//构造新的文件名
|
||||
String objectName = UUID.randomUUID().toString() + extension;
|
||||
//上传文件到OSS
|
||||
String filePath = aliOssUtil.upload(file.getBytes(), objectName);
|
||||
//返回文件路径
|
||||
return Result.success(filePath);
|
||||
} catch (IOException e) {
|
||||
log.error("文件上传失败:{}", e.getMessage());
|
||||
}
|
||||
|
||||
return Result.error(MessageConstant.UPLOAD_FAILED);
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
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
|
||||
@RequestMapping("/admin/dish")
|
||||
@Api(tags = "菜品管理相关接口")
|
||||
@Slf4j
|
||||
public class DishController {
|
||||
|
||||
@Autowired
|
||||
private DishService dishService;
|
||||
|
||||
/**
|
||||
* 新增菜品
|
||||
* @param dishDTO
|
||||
* @return
|
||||
*/
|
||||
@PostMapping
|
||||
@ApiOperation("新增菜品")
|
||||
public Result save(@RequestBody DishDTO dishDTO) {
|
||||
log.info("新增菜品: {}", dishDTO);
|
||||
dishService.saveWithFlavor(dishDTO);
|
||||
|
||||
return Result.success();
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
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);
|
||||
}
|
@ -1,5 +1,8 @@
|
||||
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.Select;
|
||||
|
||||
@ -14,4 +17,10 @@ public interface DishMapper {
|
||||
@Select("select count(id) from dish where category_id = #{categoryId}")
|
||||
Integer countByCategoryId(Long categoryId);
|
||||
|
||||
/**
|
||||
* 插入菜品
|
||||
* @param dish
|
||||
*/
|
||||
@AutoFill(value = OperationType.INSERT)
|
||||
void insert(Dish dish);
|
||||
}
|
||||
|
13
sky-server/src/main/java/com/sky/service/DishService.java
Normal file
13
sky-server/src/main/java/com/sky/service/DishService.java
Normal file
@ -0,0 +1,13 @@
|
||||
package com.sky.service;
|
||||
|
||||
import com.sky.dto.DishDTO;
|
||||
|
||||
public interface DishService {
|
||||
|
||||
/**
|
||||
* 新增菜品和对应的口味
|
||||
* @param dishDTO
|
||||
*/
|
||||
void saveWithFlavor(DishDTO dishDTO);
|
||||
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
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;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class DishServiceImpl implements DishService {
|
||||
|
||||
@Autowired
|
||||
private DishMapper dishMapper;
|
||||
|
||||
@Autowired
|
||||
private DishFlavorMapper dishFlavorMapper;
|
||||
|
||||
/**
|
||||
* 新增菜品和对应的口味
|
||||
* @param dishDTO
|
||||
*/
|
||||
@Transactional
|
||||
public void saveWithFlavor(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) {
|
||||
//遍历口味列表,设置对应的菜品id
|
||||
flavors.forEach(flavor -> {
|
||||
flavor.setDishId(dishId);
|
||||
});
|
||||
//向口味表查入n条数据
|
||||
dishFlavorMapper.insertBatch(flavors);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -6,3 +6,8 @@ sky:
|
||||
database: sky_take_out
|
||||
username: root
|
||||
password: 123456
|
||||
alioss:
|
||||
access-key-id: LTAI5tAggPBTvvugwGhD3onk
|
||||
access-key-secret: YyUv8YJZjHStekVVoh8ilpCevL2NY4
|
||||
endpoint: oss-cn-guangzhou.aliyuncs.com
|
||||
bucket-name: sky-take-out-1919810
|
||||
|
@ -38,3 +38,8 @@ sky:
|
||||
admin-ttl: 720000000
|
||||
# 设置前端传递过来的令牌名称
|
||||
admin-token-name: token
|
||||
alioss:
|
||||
access-key-id: ${sky.alioss.access-key-id}
|
||||
access-key-secret: ${sky.alioss.access-key-secret}
|
||||
endpoint: ${sky.alioss.endpoint}
|
||||
bucket-name: ${sky.alioss.bucket-name}
|
12
sky-server/src/main/resources/mapper/DishFlavorMapper.xml
Normal file
12
sky-server/src/main/resources/mapper/DishFlavorMapper.xml
Normal file
@ -0,0 +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.sky.mapper.DishFlavorMapper">
|
||||
|
||||
<insert id="insertBatch">
|
||||
insert into dish_flavor (dish_id, name, value) values
|
||||
<foreach collection="flavors" item="flavor" separator="," >
|
||||
(#{flavor.dishId}, #{flavor.name}, #{flavor.value})
|
||||
</foreach>
|
||||
</insert>
|
||||
</mapper>
|
11
sky-server/src/main/resources/mapper/DishMapper.xml
Normal file
11
sky-server/src/main/resources/mapper/DishMapper.xml
Normal file
@ -0,0 +1,11 @@
|
||||
<?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>
|
Loading…
Reference in New Issue
Block a user