Refactor category remove service

pull/146/head
johnniang 2019-04-11 21:37:52 +08:00
parent de7b44cbbc
commit 0e208448e6
5 changed files with 29 additions and 16 deletions

View File

@ -1,10 +1,10 @@
package run.halo.app.service; 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.data.domain.Sort;
import org.springframework.lang.NonNull; 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 run.halo.app.service.base.CrudService;
import java.util.List; import java.util.List;
@ -40,4 +40,12 @@ public interface CategoryService extends CrudService<Category, Integer> {
* @return Category * @return Category
*/ */
Category getBySlugName(@NonNull String slugName); Category getBySlugName(@NonNull String slugName);
/**
* Removes category and post categories.
*
* @param categoryId category id must not be null
*/
@Transactional
void removeCategoryAndPostCategoryBy(Integer categoryId);
} }

View File

@ -6,6 +6,7 @@ import run.halo.app.model.entity.Category;
import run.halo.app.model.vo.CategoryVO; import run.halo.app.model.vo.CategoryVO;
import run.halo.app.repository.CategoryRepository; import run.halo.app.repository.CategoryRepository;
import run.halo.app.service.CategoryService; import run.halo.app.service.CategoryService;
import run.halo.app.service.PostCategoryService;
import run.halo.app.service.base.AbstractCrudService; import run.halo.app.service.base.AbstractCrudService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
@ -13,10 +14,6 @@ import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils; 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.Collections;
import java.util.LinkedList; import java.util.LinkedList;
@ -34,9 +31,13 @@ public class CategoryServiceImpl extends AbstractCrudService<Category, Integer>
private final CategoryRepository categoryRepository; private final CategoryRepository categoryRepository;
public CategoryServiceImpl(CategoryRepository categoryRepository) { private final PostCategoryService postCategoryService;
public CategoryServiceImpl(CategoryRepository categoryRepository,
PostCategoryService postCategoryService) {
super(categoryRepository); super(categoryRepository);
this.categoryRepository = categoryRepository; this.categoryRepository = categoryRepository;
this.postCategoryService = postCategoryService;
} }
/** /**
@ -162,4 +163,12 @@ public class CategoryServiceImpl extends AbstractCrudService<Category, Integer>
public Category getBySlugName(String slugName) { public Category getBySlugName(String slugName) {
return categoryRepository.getBySlugName(slugName).orElseThrow(() -> new NotFoundException("The Category does not exist").setErrorData(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);
}
} }

View File

@ -405,6 +405,7 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
postListVO.setCategories(Optional.ofNullable(categoryListMap.get(post.getId())) postListVO.setCategories(Optional.ofNullable(categoryListMap.get(post.getId()))
.orElseGet(LinkedList::new) .orElseGet(LinkedList::new)
.stream() .stream()
.filter(Objects::nonNull)
.map(category -> new CategoryOutputDTO().<CategoryOutputDTO>convertFrom(category)) .map(category -> new CategoryOutputDTO().<CategoryOutputDTO>convertFrom(category))
.collect(Collectors.toList())); .collect(Collectors.toList()));

View File

@ -15,7 +15,7 @@ import java.util.*;
* Object validation utilities. * Object validation utilities.
* *
* @author johnniang * @author johnniang
* @data 03/29/19 * @date 03/29/19
*/ */
public class ValidationUtils { public class ValidationUtils {

View File

@ -9,7 +9,6 @@ import run.halo.app.model.entity.Category;
import run.halo.app.model.params.CategoryParam; import run.halo.app.model.params.CategoryParam;
import run.halo.app.model.vo.CategoryVO; import run.halo.app.model.vo.CategoryVO;
import run.halo.app.service.CategoryService; import run.halo.app.service.CategoryService;
import run.halo.app.service.PostCategoryService;
import javax.validation.Valid; import javax.validation.Valid;
import java.util.List; import java.util.List;
@ -28,11 +27,8 @@ public class CategoryController {
private final CategoryService categoryService; private final CategoryService categoryService;
private final PostCategoryService postCategoryService; public CategoryController(CategoryService categoryService) {
public CategoryController(CategoryService categoryService, PostCategoryService postCategoryService) {
this.categoryService = categoryService; this.categoryService = categoryService;
this.postCategoryService = postCategoryService;
} }
@GetMapping @GetMapping
@ -76,7 +72,6 @@ public class CategoryController {
@DeleteMapping("{id:\\d+}") @DeleteMapping("{id:\\d+}")
@ApiOperation("Delete category by id") @ApiOperation("Delete category by id")
public void deletePermanently(@PathVariable("id") Integer id) { public void deletePermanently(@PathVariable("id") Integer id) {
categoryService.removeById(id); categoryService.removeCategoryAndPostCategoryBy(id);
postCategoryService.removeByCategoryId(id);
} }
} }