完善了商品类别接口

This commit is contained in:
chen-xin-zhi 2024-10-31 22:10:44 +08:00
parent 45ff92ae85
commit 45abf0e74a
6 changed files with 45 additions and 25 deletions

View File

@ -63,7 +63,7 @@ public class AddressController {
*/ */
@PostMapping("/delete") @PostMapping("/delete")
public BaseResponse<Boolean> delAddress(@RequestBody CommonRequest deleteRequest) { public BaseResponse<Boolean> delAddress(@RequestBody CommonRequest deleteRequest) {
if (deleteRequest == null || deleteRequest.getId() == null) { if (deleteRequest == null || deleteRequest.getId() <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR); throw new BusinessException(ErrorCode.PARAMS_ERROR);
} }
Long id = deleteRequest.getId(); Long id = deleteRequest.getId();
@ -82,7 +82,7 @@ public class AddressController {
*/ */
@PostMapping("/update") @PostMapping("/update")
public BaseResponse<Boolean> updateAddress(@RequestBody AddressUpdateRequest addressUpdateRequest) { public BaseResponse<Boolean> updateAddress(@RequestBody AddressUpdateRequest addressUpdateRequest) {
if (addressUpdateRequest == null || addressUpdateRequest.getId() == null) { if (addressUpdateRequest == null || addressUpdateRequest.getId() <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR); throw new BusinessException(ErrorCode.PARAMS_ERROR);
} }
Address address = new Address(); Address address = new Address();
@ -103,7 +103,7 @@ public class AddressController {
*/ */
@PostMapping("/list") @PostMapping("/list")
public BaseResponse<List<Address>> listAddress(@RequestBody CommonRequest addressQueryRequest) { public BaseResponse<List<Address>> listAddress(@RequestBody CommonRequest addressQueryRequest) {
if (addressQueryRequest == null || addressQueryRequest.getId() == null) { if (addressQueryRequest == null || addressQueryRequest.getId() <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR); throw new BusinessException(ErrorCode.PARAMS_ERROR);
} }
Long id = addressQueryRequest.getId(); Long id = addressQueryRequest.getId();

View File

@ -1,6 +1,7 @@
package com.cultural.heritage.controller.good; 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.BaseResponse;
import com.cultural.heritage.common.ErrorCode; import com.cultural.heritage.common.ErrorCode;
import com.cultural.heritage.common.ResultUtils; import com.cultural.heritage.common.ResultUtils;
@ -54,17 +55,11 @@ public class CategoryController {
if (categoryAddRequest == null) { if (categoryAddRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR); throw new BusinessException(ErrorCode.PARAMS_ERROR);
} }
String typeName = categoryAddRequest.getTypeName();
List<String> typeNameList = categoryService.getTypeNameList();
if (typeNameList.contains(typeName)) {
throw new BusinessException(ErrorCode.OPERATION_ERROR, "已存在该类别");
}
Category category = new Category(); Category category = new Category();
BeanUtils.copyProperties(categoryAddRequest, category); BeanUtils.copyProperties(categoryAddRequest, category);
categoryService.validCategory(category, true);
boolean save = categoryService.save(category); boolean save = categoryService.save(category);
if (!save) { ThrowUtils.throwIf(!save, ErrorCode.OPERATION_ERROR);
throw new BusinessException(ErrorCode.SYSTEM_ERROR);
}
return ResultUtils.success(true, "类别插入成功"); return ResultUtils.success(true, "类别插入成功");
} }
@ -76,7 +71,7 @@ public class CategoryController {
*/ */
@PostMapping("/delete") @PostMapping("/delete")
public BaseResponse<Boolean> deleteCategory(@RequestBody CommonRequest deleteCategoryRequest) { public BaseResponse<Boolean> deleteCategory(@RequestBody CommonRequest deleteCategoryRequest) {
if (deleteCategoryRequest == null || deleteCategoryRequest.getId() == null) { if (deleteCategoryRequest == null || deleteCategoryRequest.getId() <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR); throw new BusinessException(ErrorCode.PARAMS_ERROR);
} }
Long id = deleteCategoryRequest.getId(); Long id = deleteCategoryRequest.getId();
@ -94,14 +89,10 @@ public class CategoryController {
*/ */
@PostMapping("/update") @PostMapping("/update")
public BaseResponse<Boolean> updateCategory(@RequestBody Category category) { public BaseResponse<Boolean> updateCategory(@RequestBody Category category) {
if (category == null || category.getId() == null) { if (category == null || category.getId() <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR); throw new BusinessException(ErrorCode.PARAMS_ERROR);
} }
String typeName = category.getTypeName(); categoryService.validCategory(category, false);
List<String> typeNameList = categoryService.getTypeNameList();
if (typeNameList.contains(typeName)) {
throw new BusinessException(ErrorCode.OPERATION_ERROR, "已存在该类别");
}
boolean result = categoryService.updateById(category); boolean result = categoryService.updateById(category);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
return ResultUtils.success(true, "类别更新成功"); return ResultUtils.success(true, "类别更新成功");
@ -115,9 +106,6 @@ public class CategoryController {
@PostMapping("/list") @PostMapping("/list")
public BaseResponse<List<Category>> listCategory() { public BaseResponse<List<Category>> listCategory() {
List<Category> list = categoryService.list(); List<Category> list = categoryService.list();
if (CollectionUtils.isEmpty(list)) {
list = new ArrayList<>();
}
return ResultUtils.success(list); return ResultUtils.success(list);
} }
@ -130,7 +118,7 @@ public class CategoryController {
*/ */
@PostMapping("/list/type") @PostMapping("/list/type")
public BaseResponse<Map<Category, List<Good>>> listGoodByCategory(@RequestBody CommonRequest categoryQueryRequest) { public BaseResponse<Map<Category, List<Good>>> listGoodByCategory(@RequestBody CommonRequest categoryQueryRequest) {
if (categoryQueryRequest == null || categoryQueryRequest.getId() == null) { if (categoryQueryRequest == null || categoryQueryRequest.getId() <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR); throw new BusinessException(ErrorCode.PARAMS_ERROR);
} }
Long id = categoryQueryRequest.getId(); Long id = categoryQueryRequest.getId();

View File

@ -71,7 +71,7 @@ public class GoodController {
@PostMapping("/delete") @PostMapping("/delete")
// @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) // @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
public BaseResponse<Boolean> deleteGood(@RequestBody CommonRequest deleteRequest) { public BaseResponse<Boolean> deleteGood(@RequestBody CommonRequest deleteRequest) {
if (deleteRequest == null || deleteRequest.getId() == null) { if (deleteRequest == null || deleteRequest.getId() <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR); throw new BusinessException(ErrorCode.PARAMS_ERROR);
} }
boolean result = goodService.removeById(deleteRequest.getId()); boolean result = goodService.removeById(deleteRequest.getId());
@ -88,7 +88,7 @@ public class GoodController {
@PostMapping("/update") @PostMapping("/update")
// @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) // @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
public BaseResponse<Boolean> updateGoods(@RequestBody GoodUpdateRequest goodUpdateRequest) { public BaseResponse<Boolean> updateGoods(@RequestBody GoodUpdateRequest goodUpdateRequest) {
if (goodUpdateRequest == null || goodUpdateRequest.getId() == null) { if (goodUpdateRequest == null || goodUpdateRequest.getId() <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR); throw new BusinessException(ErrorCode.PARAMS_ERROR);
} }
Good good = new Good(); Good good = new Good();

View File

@ -147,7 +147,7 @@ public class UserController {
@PostMapping("/update") @PostMapping("/update")
// @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) // @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateRequest userUpdateRequest) { public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateRequest userUpdateRequest) {
if (userUpdateRequest == null || userUpdateRequest.getId() == null) { if (userUpdateRequest == null || userUpdateRequest.getId() <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR); throw new BusinessException(ErrorCode.PARAMS_ERROR);
} }
User user = new User(); User user = new User();

View File

@ -19,4 +19,9 @@ public interface CategoryService extends IService<Category> {
*/ */
Category getCategoryById(Long id); Category getCategoryById(Long id);
/**
* 校验
*/
void validCategory(Category category, boolean add);
} }

View File

@ -4,9 +4,11 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cultural.heritage.common.ErrorCode; import com.cultural.heritage.common.ErrorCode;
import com.cultural.heritage.exception.BusinessException; import com.cultural.heritage.exception.BusinessException;
import com.cultural.heritage.exception.ThrowUtils;
import com.cultural.heritage.mapper.CategoryMapper; import com.cultural.heritage.mapper.CategoryMapper;
import com.cultural.heritage.model.entity.Category; import com.cultural.heritage.model.entity.Category;
import com.cultural.heritage.service.good.CategoryService; import com.cultural.heritage.service.good.CategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
@ -40,4 +42,29 @@ public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> i
} }
return category; return category;
} }
@Override
public void validCategory(Category category, boolean add) {
String typeName = category.getTypeName();
String typeUrl = category.getTypeUrl();
QueryWrapper<Category> 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, "请上传图片");
}
}
} }