From 45abf0e74a867579ba68b35ebefb14cf483f462f Mon Sep 17 00:00:00 2001 From: chen-xin-zhi <3588068430@qq.com> Date: Thu, 31 Oct 2024 22:10:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E4=BA=86=E5=95=86=E5=93=81?= =?UTF-8?q?=E7=B1=BB=E5=88=AB=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/address/AddressController.java | 6 ++--- .../controller/good/CategoryController.java | 26 +++++------------- .../controller/good/GoodController.java | 4 +-- .../controller/user/UserController.java | 2 +- .../service/good/CategoryService.java | 5 ++++ .../good/impl/CategoryServiceImpl.java | 27 +++++++++++++++++++ 6 files changed, 45 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/cultural/heritage/controller/address/AddressController.java b/src/main/java/com/cultural/heritage/controller/address/AddressController.java index a462f14..b66ca9b 100644 --- a/src/main/java/com/cultural/heritage/controller/address/AddressController.java +++ b/src/main/java/com/cultural/heritage/controller/address/AddressController.java @@ -63,7 +63,7 @@ public class AddressController { */ @PostMapping("/delete") public BaseResponse delAddress(@RequestBody CommonRequest deleteRequest) { - if (deleteRequest == null || deleteRequest.getId() == null) { + if (deleteRequest == null || deleteRequest.getId() <= 0) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } Long id = deleteRequest.getId(); @@ -82,7 +82,7 @@ public class AddressController { */ @PostMapping("/update") public BaseResponse updateAddress(@RequestBody AddressUpdateRequest addressUpdateRequest) { - if (addressUpdateRequest == null || addressUpdateRequest.getId() == null) { + if (addressUpdateRequest == null || addressUpdateRequest.getId() <= 0) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } Address address = new Address(); @@ -103,7 +103,7 @@ public class AddressController { */ @PostMapping("/list") public BaseResponse> listAddress(@RequestBody CommonRequest addressQueryRequest) { - if (addressQueryRequest == null || addressQueryRequest.getId() == null) { + if (addressQueryRequest == null || addressQueryRequest.getId() <= 0) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } Long id = addressQueryRequest.getId(); diff --git a/src/main/java/com/cultural/heritage/controller/good/CategoryController.java b/src/main/java/com/cultural/heritage/controller/good/CategoryController.java index 6256556..668a1ee 100644 --- a/src/main/java/com/cultural/heritage/controller/good/CategoryController.java +++ b/src/main/java/com/cultural/heritage/controller/good/CategoryController.java @@ -1,6 +1,7 @@ package com.cultural.heritage.controller.good; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.cultural.heritage.common.BaseResponse; import com.cultural.heritage.common.ErrorCode; import com.cultural.heritage.common.ResultUtils; @@ -54,17 +55,11 @@ public class CategoryController { if (categoryAddRequest == null) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } - String typeName = categoryAddRequest.getTypeName(); - List typeNameList = categoryService.getTypeNameList(); - if (typeNameList.contains(typeName)) { - throw new BusinessException(ErrorCode.OPERATION_ERROR, "已存在该类别"); - } Category category = new Category(); BeanUtils.copyProperties(categoryAddRequest, category); + categoryService.validCategory(category, true); boolean save = categoryService.save(category); - if (!save) { - throw new BusinessException(ErrorCode.SYSTEM_ERROR); - } + ThrowUtils.throwIf(!save, ErrorCode.OPERATION_ERROR); return ResultUtils.success(true, "类别插入成功"); } @@ -76,7 +71,7 @@ public class CategoryController { */ @PostMapping("/delete") public BaseResponse deleteCategory(@RequestBody CommonRequest deleteCategoryRequest) { - if (deleteCategoryRequest == null || deleteCategoryRequest.getId() == null) { + if (deleteCategoryRequest == null || deleteCategoryRequest.getId() <= 0) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } Long id = deleteCategoryRequest.getId(); @@ -94,14 +89,10 @@ public class CategoryController { */ @PostMapping("/update") public BaseResponse updateCategory(@RequestBody Category category) { - if (category == null || category.getId() == null) { + if (category == null || category.getId() <= 0) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } - String typeName = category.getTypeName(); - List typeNameList = categoryService.getTypeNameList(); - if (typeNameList.contains(typeName)) { - throw new BusinessException(ErrorCode.OPERATION_ERROR, "已存在该类别"); - } + categoryService.validCategory(category, false); boolean result = categoryService.updateById(category); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); return ResultUtils.success(true, "类别更新成功"); @@ -115,9 +106,6 @@ public class CategoryController { @PostMapping("/list") public BaseResponse> listCategory() { List list = categoryService.list(); - if (CollectionUtils.isEmpty(list)) { - list = new ArrayList<>(); - } return ResultUtils.success(list); } @@ -130,7 +118,7 @@ public class CategoryController { */ @PostMapping("/list/type") public BaseResponse>> listGoodByCategory(@RequestBody CommonRequest categoryQueryRequest) { - if (categoryQueryRequest == null || categoryQueryRequest.getId() == null) { + if (categoryQueryRequest == null || categoryQueryRequest.getId() <= 0) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } Long id = categoryQueryRequest.getId(); diff --git a/src/main/java/com/cultural/heritage/controller/good/GoodController.java b/src/main/java/com/cultural/heritage/controller/good/GoodController.java index c7503a7..e042dff 100644 --- a/src/main/java/com/cultural/heritage/controller/good/GoodController.java +++ b/src/main/java/com/cultural/heritage/controller/good/GoodController.java @@ -71,7 +71,7 @@ public class GoodController { @PostMapping("/delete") // @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) public BaseResponse deleteGood(@RequestBody CommonRequest deleteRequest) { - if (deleteRequest == null || deleteRequest.getId() == null) { + if (deleteRequest == null || deleteRequest.getId() <= 0) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } boolean result = goodService.removeById(deleteRequest.getId()); @@ -88,7 +88,7 @@ public class GoodController { @PostMapping("/update") // @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) public BaseResponse updateGoods(@RequestBody GoodUpdateRequest goodUpdateRequest) { - if (goodUpdateRequest == null || goodUpdateRequest.getId() == null) { + if (goodUpdateRequest == null || goodUpdateRequest.getId() <= 0) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } Good good = new Good(); diff --git a/src/main/java/com/cultural/heritage/controller/user/UserController.java b/src/main/java/com/cultural/heritage/controller/user/UserController.java index 8ade3f8..160f712 100644 --- a/src/main/java/com/cultural/heritage/controller/user/UserController.java +++ b/src/main/java/com/cultural/heritage/controller/user/UserController.java @@ -147,7 +147,7 @@ public class UserController { @PostMapping("/update") // @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) public BaseResponse updateUser(@RequestBody UserUpdateRequest userUpdateRequest) { - if (userUpdateRequest == null || userUpdateRequest.getId() == null) { + if (userUpdateRequest == null || userUpdateRequest.getId() <= 0) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } User user = new User(); diff --git a/src/main/java/com/cultural/heritage/service/good/CategoryService.java b/src/main/java/com/cultural/heritage/service/good/CategoryService.java index b0c61cb..858f793 100644 --- a/src/main/java/com/cultural/heritage/service/good/CategoryService.java +++ b/src/main/java/com/cultural/heritage/service/good/CategoryService.java @@ -19,4 +19,9 @@ public interface CategoryService extends IService { */ Category getCategoryById(Long id); + /** + * 校验 + */ + void validCategory(Category category, boolean add); + } diff --git a/src/main/java/com/cultural/heritage/service/good/impl/CategoryServiceImpl.java b/src/main/java/com/cultural/heritage/service/good/impl/CategoryServiceImpl.java index 898255f..2dc4cf9 100644 --- a/src/main/java/com/cultural/heritage/service/good/impl/CategoryServiceImpl.java +++ b/src/main/java/com/cultural/heritage/service/good/impl/CategoryServiceImpl.java @@ -4,9 +4,11 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.cultural.heritage.common.ErrorCode; import com.cultural.heritage.exception.BusinessException; +import com.cultural.heritage.exception.ThrowUtils; import com.cultural.heritage.mapper.CategoryMapper; import com.cultural.heritage.model.entity.Category; import com.cultural.heritage.service.good.CategoryService; +import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; @@ -40,4 +42,29 @@ public class CategoryServiceImpl extends ServiceImpl i } return category; } + + + + @Override + public void validCategory(Category category, boolean add) { + String typeName = category.getTypeName(); + String typeUrl = category.getTypeUrl(); + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("typeName", typeName); + // 创建时,参数不能为空 + if (add) { + ThrowUtils.throwIf(typeName == null, ErrorCode.PARAMS_ERROR); + Long count = this.baseMapper.selectCount(queryWrapper); + ThrowUtils.throwIf(count > 0, ErrorCode.PARAMS_ERROR, "类别名称不可重复"); + } else { + queryWrapper.ne("id", category.getId()); + Long count = this.baseMapper.selectCount(queryWrapper); + ThrowUtils.throwIf(count > 0, ErrorCode.PARAMS_ERROR, "类别名称已存在"); + } + // 有参数则校验 + if (StringUtils.isAnyBlank(typeUrl)) { + throw new BusinessException(ErrorCode.PARAMS_ERROR, "请上传图片"); + } + + } }