mirror of
https://gitee.com/coder-xiaomo/flashsale
synced 2025-09-12 23:11:38 +08:00
商品模型-商品创建实现
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package com.cxyxiaomo.flashsale.controller;
|
||||
|
||||
import com.cxyxiaomo.flashsale.controller.viewobject.ItemVO;
|
||||
import com.cxyxiaomo.flashsale.error.BusinessException;
|
||||
import com.cxyxiaomo.flashsale.response.CommonReturnType;
|
||||
import com.cxyxiaomo.flashsale.service.ItemService;
|
||||
import com.cxyxiaomo.flashsale.service.model.ItemModel;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Controller("/item")
|
||||
@RequestMapping("/item")
|
||||
@CrossOrigin(allowedHeaders = "*", allowCredentials = "true")
|
||||
public class ItemController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private ItemService itemService;
|
||||
|
||||
// 创建商品的Controller
|
||||
@RequestMapping(value = "/create", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
|
||||
@ResponseBody
|
||||
public CommonReturnType createItem(@RequestParam(name = "title") String title,
|
||||
@RequestParam(name = "description") String description,
|
||||
@RequestParam(name = "price") BigDecimal price,
|
||||
@RequestParam(name = "stock") Integer stock,
|
||||
@RequestParam(name = "imgUrl") String imgUrl) throws BusinessException {
|
||||
// 封装Service请求用来创建商品
|
||||
ItemModel itemModel = new ItemModel();
|
||||
itemModel.setTitle(title);
|
||||
itemModel.setDescription(description);
|
||||
itemModel.setPrice(price);
|
||||
itemModel.setStock(stock);
|
||||
itemModel.setImgUrl(imgUrl);
|
||||
|
||||
ItemModel itemModelForReturn = itemService.createItem(itemModel);
|
||||
ItemVO itemVO = convertVOFromModel(itemModelForReturn);
|
||||
|
||||
return CommonReturnType.create(itemVO);
|
||||
}
|
||||
|
||||
private ItemVO convertVOFromModel(ItemModel itemModel) {
|
||||
if (itemModel == null) {
|
||||
return null;
|
||||
}
|
||||
ItemVO itemVO = new ItemVO();
|
||||
BeanUtils.copyProperties(itemModel, itemVO);
|
||||
return itemVO;
|
||||
}
|
||||
}
|
@@ -0,0 +1,82 @@
|
||||
package com.cxyxiaomo.flashsale.controller.viewobject;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class ItemVO {
|
||||
// 商品价格
|
||||
private Integer id;
|
||||
|
||||
// 商品名称
|
||||
private String title;
|
||||
|
||||
// 商品价格
|
||||
private BigDecimal price;
|
||||
|
||||
// 商品的库存
|
||||
private Integer stock;
|
||||
|
||||
// 商品的描述
|
||||
private String description;
|
||||
|
||||
// 商品的销量
|
||||
private Integer sales;
|
||||
|
||||
// 商品描述图片的URL
|
||||
private String imgUrl;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public BigDecimal getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(BigDecimal price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public Integer getStock() {
|
||||
return stock;
|
||||
}
|
||||
|
||||
public void setStock(Integer stock) {
|
||||
this.stock = stock;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Integer getSales() {
|
||||
return sales;
|
||||
}
|
||||
|
||||
public void setSales(Integer sales) {
|
||||
this.sales = sales;
|
||||
}
|
||||
|
||||
public String getImgUrl() {
|
||||
return imgUrl;
|
||||
}
|
||||
|
||||
public void setImgUrl(String imgUrl) {
|
||||
this.imgUrl = imgUrl;
|
||||
}
|
||||
}
|
@@ -35,6 +35,8 @@ public interface ItemStockDOMapper {
|
||||
*/
|
||||
ItemStockDO selectByPrimaryKey(Integer id);
|
||||
|
||||
ItemStockDO selectByItemId(Integer id);
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method corresponds to the database table item_stock
|
||||
|
@@ -0,0 +1,17 @@
|
||||
package com.cxyxiaomo.flashsale.service;
|
||||
|
||||
import com.cxyxiaomo.flashsale.error.BusinessException;
|
||||
import com.cxyxiaomo.flashsale.service.model.ItemModel;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ItemService {
|
||||
// 创建商品
|
||||
ItemModel createItem(ItemModel itemModel) throws BusinessException;
|
||||
|
||||
// 商品列表浏览
|
||||
List<ItemModel> listItem();
|
||||
|
||||
// 商品详情浏览
|
||||
ItemModel getItemById(Integer id);
|
||||
}
|
@@ -0,0 +1,105 @@
|
||||
package com.cxyxiaomo.flashsale.service.impl;
|
||||
|
||||
import com.cxyxiaomo.flashsale.dao.ItemDOMapper;
|
||||
import com.cxyxiaomo.flashsale.dao.ItemStockDOMapper;
|
||||
import com.cxyxiaomo.flashsale.dataobject.ItemDO;
|
||||
import com.cxyxiaomo.flashsale.dataobject.ItemStockDO;
|
||||
import com.cxyxiaomo.flashsale.error.BusinessException;
|
||||
import com.cxyxiaomo.flashsale.error.EmBusinessError;
|
||||
import com.cxyxiaomo.flashsale.service.ItemService;
|
||||
import com.cxyxiaomo.flashsale.service.model.ItemModel;
|
||||
import com.cxyxiaomo.flashsale.validator.ValidationResult;
|
||||
import com.cxyxiaomo.flashsale.validator.ValidatorImpl;
|
||||
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.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class ItemServiceImpl implements ItemService {
|
||||
|
||||
@Autowired
|
||||
private ValidatorImpl validator;
|
||||
|
||||
@Autowired
|
||||
private ItemDOMapper itemDOMapper;
|
||||
|
||||
@Autowired
|
||||
private ItemStockDOMapper itemStockDOMapper;
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public ItemModel createItem(ItemModel itemModel) throws BusinessException {
|
||||
// 校验入参
|
||||
ValidationResult result = validator.validate(itemModel);
|
||||
|
||||
if (result.isHasErrors()) {
|
||||
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, result.getErrMsg());
|
||||
}
|
||||
|
||||
// 转化 ItemModel -> Data Object
|
||||
ItemDO itemDO = this.convertItemDOFromItemModel(itemModel);
|
||||
|
||||
// 写入数据库
|
||||
itemDOMapper.insertSelective(itemDO);
|
||||
itemModel.setId(itemDO.getId());
|
||||
|
||||
ItemStockDO itemStockDO = this.convertItemStockDOFormItemStockModel(itemModel);
|
||||
|
||||
itemStockDOMapper.insertSelective(itemStockDO);
|
||||
|
||||
// 返回创建完成的对象
|
||||
return this.getItemById(itemModel.getId());
|
||||
}
|
||||
|
||||
private ItemDO convertItemDOFromItemModel(ItemModel itemModel) {
|
||||
if (itemModel == null) {
|
||||
return null;
|
||||
}
|
||||
ItemDO itemDO = new ItemDO();
|
||||
BeanUtils.copyProperties(itemModel, itemDO);
|
||||
|
||||
return itemDO;
|
||||
}
|
||||
|
||||
private ItemStockDO convertItemStockDOFormItemStockModel(ItemModel itemModel) {
|
||||
if (itemModel == null) {
|
||||
return null;
|
||||
}
|
||||
ItemStockDO itemStockDO = new ItemStockDO();
|
||||
itemStockDO.setItemId(itemModel.getId());
|
||||
itemStockDO.setStock(itemModel.getStock());
|
||||
|
||||
return itemStockDO;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ItemModel> listItem() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemModel getItemById(Integer id) {
|
||||
ItemDO itemDO = itemDOMapper.selectByPrimaryKey(id);
|
||||
if (itemDO == null) {
|
||||
return null;
|
||||
}
|
||||
// 操作获得库存数量
|
||||
ItemStockDO itemStockDO = itemStockDOMapper.selectByItemId(itemDO.getId());
|
||||
|
||||
// 将Data Object -> Model
|
||||
ItemModel itemModel = convertModelFromDataObject(itemDO, itemStockDO);
|
||||
return itemModel;
|
||||
}
|
||||
|
||||
private ItemModel convertModelFromDataObject(ItemDO itemDO, ItemStockDO itemStockDO) {
|
||||
ItemModel itemModel = new ItemModel();
|
||||
BeanUtils.copyProperties(itemDO, itemModel);
|
||||
itemModel.setStock(itemStockDO.getStock());
|
||||
|
||||
return itemModel;
|
||||
}
|
||||
}
|
@@ -1,5 +1,8 @@
|
||||
package com.cxyxiaomo.flashsale.service.model;
|
||||
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class ItemModel {
|
||||
@@ -7,21 +10,27 @@ public class ItemModel {
|
||||
private Integer id;
|
||||
|
||||
// 商品名称
|
||||
@NotBlank(message = "商品名称不能为空")
|
||||
private String title;
|
||||
|
||||
// 商品价格
|
||||
@NotNull(message = "商品价格不能为空")
|
||||
@Min(value = 0, message = "商品价格必须大于0")
|
||||
private BigDecimal price;
|
||||
|
||||
// 商品的库存
|
||||
@NotNull(message = "库存不能不填")
|
||||
private Integer stock;
|
||||
|
||||
// 商品的描述
|
||||
@NotBlank(message = "商品描述信息不能为空")
|
||||
private String description;
|
||||
|
||||
// 商品的销量
|
||||
private Integer sales;
|
||||
|
||||
// 商品描述图片的URL
|
||||
@NotBlank(message = "商品描述图片不能为空")
|
||||
private String imgUrl;
|
||||
|
||||
public Integer getId() {
|
||||
|
@@ -108,4 +108,10 @@
|
||||
item_id = #{itemId,jdbcType=INTEGER}
|
||||
where id = #{id,jdbcType=INTEGER}
|
||||
</update>
|
||||
<select id="selectByItemId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
|
||||
select
|
||||
<include refid="Base_Column_List" />
|
||||
from item_stock
|
||||
where item_id = #{itemId,jdbcType=INTEGER}
|
||||
</select>
|
||||
</mapper>
|
Reference in New Issue
Block a user