From bec180a76e5d48e6a56b13b671dcc553fdb6e511 Mon Sep 17 00:00:00 2001 From: chen-xin-zhi <3588068430@qq.com> Date: Tue, 29 Oct 2024 22:45:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=86=E5=9F=BA=E6=9C=AC?= =?UTF-8?q?=E7=9A=84=E5=95=86=E5=93=81=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../operategood/CategoryController.java | 70 +++++++++++++++++++ .../operategood/GoodController.java | 4 +- .../dto/category/CategoryAddRequest.java | 29 ++++++++ .../heritage/model/entity/Category.java | 5 +- 4 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/cultural/heritage/model/dto/category/CategoryAddRequest.java diff --git a/src/main/java/com/cultural/heritage/controller/operategood/CategoryController.java b/src/main/java/com/cultural/heritage/controller/operategood/CategoryController.java index 9999486..1bf6904 100644 --- a/src/main/java/com/cultural/heritage/controller/operategood/CategoryController.java +++ b/src/main/java/com/cultural/heritage/controller/operategood/CategoryController.java @@ -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 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 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 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, "类别更新成功"); + } + } diff --git a/src/main/java/com/cultural/heritage/controller/operategood/GoodController.java b/src/main/java/com/cultural/heritage/controller/operategood/GoodController.java index ddc15d9..c391d14 100644 --- a/src/main/java/com/cultural/heritage/controller/operategood/GoodController.java +++ b/src/main/java/com/cultural/heritage/controller/operategood/GoodController.java @@ -49,7 +49,7 @@ public class GoodController { */ @PostMapping("/add") // @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) - public BaseResponse addGood(@RequestBody GoodAddRequest goodAddRequest) { + public BaseResponse 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, "商品插入成功"); } diff --git a/src/main/java/com/cultural/heritage/model/dto/category/CategoryAddRequest.java b/src/main/java/com/cultural/heritage/model/dto/category/CategoryAddRequest.java new file mode 100644 index 0000000..c7255e1 --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/dto/category/CategoryAddRequest.java @@ -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; +} diff --git a/src/main/java/com/cultural/heritage/model/entity/Category.java b/src/main/java/com/cultural/heritage/model/entity/Category.java index c3867bb..df6f6e9 100644 --- a/src/main/java/com/cultural/heritage/model/entity/Category.java +++ b/src/main/java/com/cultural/heritage/model/entity/Category.java @@ -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