完成了基本的商品管理

This commit is contained in:
chen-xin-zhi 2024-10-29 22:45:28 +08:00
parent 9c250566cf
commit bec180a76e
4 changed files with 105 additions and 3 deletions

View File

@ -1,8 +1,21 @@
package com.cultural.heritage.controller.operategood;
import com.cultural.heritage.common.BaseResponse;
import com.cultural.heritage.common.ErrorCode;
import com.cultural.heritage.common.ResultUtils;
import com.cultural.heritage.exception.BusinessException;
import com.cultural.heritage.exception.ThrowUtils;
import com.cultural.heritage.model.dto.CommonRequest;
import com.cultural.heritage.model.dto.category.CategoryAddRequest;
import com.cultural.heritage.model.entity.Category;
import com.cultural.heritage.service.operategood.CategoryService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
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;
@ -12,5 +25,62 @@ import org.springframework.web.bind.annotation.RestController;
@Tag(name = "商品类别接口")
public class CategoryController {
@Resource
private CategoryService categoryService;
/**
* 添加商品类别
* @param categoryAddRequest 类别添加请求体
* @return 是否添加成功
*/
@PostMapping("/add")
public BaseResponse<Boolean> addCategory(@RequestBody CategoryAddRequest categoryAddRequest) {
if (categoryAddRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
Category category = new Category();
BeanUtils.copyProperties(categoryAddRequest, category);
boolean save = categoryService.save(category);
if (!save) {
throw new BusinessException(ErrorCode.SYSTEM_ERROR);
}
return ResultUtils.success(true, "类别插入成功");
}
/**
* 删除商品类别
* @param deleteCategoryRequest 类别删除请求体
* @return 是否删除成功
*/
@PostMapping("/delete")
public BaseResponse<Boolean> deleteCategory(@RequestBody CommonRequest deleteCategoryRequest) {
if (deleteCategoryRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
Long id = deleteCategoryRequest.getId();
boolean result = categoryService.removeById(id);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
return ResultUtils.success(true, "类别删除成功");
}
/**
* 更新商品类别
* @param category 类别更新请求体
* @return 是否更新成功
*/
@PostMapping("/update")
public BaseResponse<Boolean> updateCategory(@RequestBody Category category) {
if (category == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
boolean result = categoryService.updateById(category);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
return ResultUtils.success(true, "类别更新成功");
}
}

View File

@ -49,7 +49,7 @@ public class GoodController {
*/
@PostMapping("/add")
// @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
public BaseResponse<Good> addGood(@RequestBody GoodAddRequest goodAddRequest) {
public BaseResponse<Boolean> addGood(@RequestBody GoodAddRequest goodAddRequest) {
if (goodAddRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
@ -59,7 +59,7 @@ public class GoodController {
if (!save) {
throw new BusinessException(ErrorCode.SYSTEM_ERROR);
}
return ResultUtils.success(good, "商品插入成功");
return ResultUtils.success(true, "商品插入成功");
}

View File

@ -0,0 +1,29 @@
package com.cultural.heritage.model.dto.category;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
@Data
public class CategoryAddRequest implements Serializable {
/**
* 类别名称
*/
private String typeName;
/**
* 类别图片
*/
private String typeUrl;
/**
* 类别简介
*/
private String typeIntro;
@Serial
private static final long serialVersionUID = 1L;
}

View File

@ -1,6 +1,8 @@
package com.cultural.heritage.model.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import java.io.Serial;
@ -13,6 +15,7 @@ public class Category implements Serializable {
/**
* id
*/
@TableId(type = IdType.AUTO)
private Integer id;
@ -31,7 +34,7 @@ public class Category implements Serializable {
/**
* 类别介绍
*/
private String typeInfo;
private String typeIntro;
@Serial