From 0e208448e651c05ecea97edbf363bad5d3757136 Mon Sep 17 00:00:00 2001 From: johnniang Date: Thu, 11 Apr 2019 21:37:52 +0800 Subject: [PATCH] Refactor category remove service --- .../run/halo/app/service/CategoryService.java | 14 +++++++++++--- .../app/service/impl/CategoryServiceImpl.java | 19 ++++++++++++++----- .../app/service/impl/PostServiceImpl.java | 1 + .../run/halo/app/utils/ValidationUtils.java | 2 +- .../admin/api/CategoryController.java | 9 ++------- 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/main/java/run/halo/app/service/CategoryService.java b/src/main/java/run/halo/app/service/CategoryService.java index 3f1a586a4..78a2a43b7 100755 --- a/src/main/java/run/halo/app/service/CategoryService.java +++ b/src/main/java/run/halo/app/service/CategoryService.java @@ -1,10 +1,10 @@ package run.halo.app.service; -import run.halo.app.model.entity.Category; -import run.halo.app.model.vo.CategoryVO; -import run.halo.app.service.base.CrudService; import org.springframework.data.domain.Sort; import org.springframework.lang.NonNull; +import org.springframework.transaction.annotation.Transactional; +import run.halo.app.model.entity.Category; +import run.halo.app.model.vo.CategoryVO; import run.halo.app.service.base.CrudService; import java.util.List; @@ -40,4 +40,12 @@ public interface CategoryService extends CrudService { * @return Category */ Category getBySlugName(@NonNull String slugName); + + /** + * Removes category and post categories. + * + * @param categoryId category id must not be null + */ + @Transactional + void removeCategoryAndPostCategoryBy(Integer categoryId); } diff --git a/src/main/java/run/halo/app/service/impl/CategoryServiceImpl.java b/src/main/java/run/halo/app/service/impl/CategoryServiceImpl.java index 7877fdf79..3dfea1943 100644 --- a/src/main/java/run/halo/app/service/impl/CategoryServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/CategoryServiceImpl.java @@ -6,6 +6,7 @@ import run.halo.app.model.entity.Category; import run.halo.app.model.vo.CategoryVO; import run.halo.app.repository.CategoryRepository; import run.halo.app.service.CategoryService; +import run.halo.app.service.PostCategoryService; import run.halo.app.service.base.AbstractCrudService; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Sort; @@ -13,10 +14,6 @@ import org.springframework.lang.NonNull; import org.springframework.stereotype.Service; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; -import run.halo.app.exception.AlreadyExistsException; -import run.halo.app.exception.NotFoundException; -import run.halo.app.repository.CategoryRepository; -import run.halo.app.service.base.AbstractCrudService; import java.util.Collections; import java.util.LinkedList; @@ -34,9 +31,13 @@ public class CategoryServiceImpl extends AbstractCrudService private final CategoryRepository categoryRepository; - public CategoryServiceImpl(CategoryRepository categoryRepository) { + private final PostCategoryService postCategoryService; + + public CategoryServiceImpl(CategoryRepository categoryRepository, + PostCategoryService postCategoryService) { super(categoryRepository); this.categoryRepository = categoryRepository; + this.postCategoryService = postCategoryService; } /** @@ -162,4 +163,12 @@ public class CategoryServiceImpl extends AbstractCrudService public Category getBySlugName(String slugName) { return categoryRepository.getBySlugName(slugName).orElseThrow(() -> new NotFoundException("The Category does not exist").setErrorData(slugName)); } + + @Override + public void removeCategoryAndPostCategoryBy(Integer categoryId) { + // Remove category + removeById(categoryId); + // Remove post categories + postCategoryService.removeByCategoryId(categoryId); + } } diff --git a/src/main/java/run/halo/app/service/impl/PostServiceImpl.java b/src/main/java/run/halo/app/service/impl/PostServiceImpl.java index fbfb9a523..18eb1ca4f 100644 --- a/src/main/java/run/halo/app/service/impl/PostServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/PostServiceImpl.java @@ -405,6 +405,7 @@ public class PostServiceImpl extends AbstractCrudService implemen postListVO.setCategories(Optional.ofNullable(categoryListMap.get(post.getId())) .orElseGet(LinkedList::new) .stream() + .filter(Objects::nonNull) .map(category -> new CategoryOutputDTO().convertFrom(category)) .collect(Collectors.toList())); diff --git a/src/main/java/run/halo/app/utils/ValidationUtils.java b/src/main/java/run/halo/app/utils/ValidationUtils.java index 04863aa14..e28c20434 100644 --- a/src/main/java/run/halo/app/utils/ValidationUtils.java +++ b/src/main/java/run/halo/app/utils/ValidationUtils.java @@ -15,7 +15,7 @@ import java.util.*; * Object validation utilities. * * @author johnniang - * @data 03/29/19 + * @date 03/29/19 */ public class ValidationUtils { diff --git a/src/main/java/run/halo/app/web/controller/admin/api/CategoryController.java b/src/main/java/run/halo/app/web/controller/admin/api/CategoryController.java index 513e0a679..06a13504c 100644 --- a/src/main/java/run/halo/app/web/controller/admin/api/CategoryController.java +++ b/src/main/java/run/halo/app/web/controller/admin/api/CategoryController.java @@ -9,7 +9,6 @@ import run.halo.app.model.entity.Category; import run.halo.app.model.params.CategoryParam; import run.halo.app.model.vo.CategoryVO; import run.halo.app.service.CategoryService; -import run.halo.app.service.PostCategoryService; import javax.validation.Valid; import java.util.List; @@ -28,11 +27,8 @@ public class CategoryController { private final CategoryService categoryService; - private final PostCategoryService postCategoryService; - - public CategoryController(CategoryService categoryService, PostCategoryService postCategoryService) { + public CategoryController(CategoryService categoryService) { this.categoryService = categoryService; - this.postCategoryService = postCategoryService; } @GetMapping @@ -76,7 +72,6 @@ public class CategoryController { @DeleteMapping("{id:\\d+}") @ApiOperation("Delete category by id") public void deletePermanently(@PathVariable("id") Integer id) { - categoryService.removeById(id); - postCategoryService.removeByCategoryId(id); + categoryService.removeCategoryAndPostCategoryBy(id); } }