Set default parent id when delete category.

pull/248/head
ruibaby 2019-07-14 10:52:12 +08:00
parent e5790b3231
commit b759380f75
5 changed files with 31 additions and 4 deletions

View File

@ -38,7 +38,7 @@ public class PostController {
@GetMapping @GetMapping
@ApiOperation("Lists posts") @ApiOperation("Lists posts")
public Page<PostListVO> pageBy(@PageableDefault(sort = "updateTime", direction = DESC) Pageable pageable, public Page<PostListVO> pageBy(@PageableDefault(sort = "createTime", direction = DESC) Pageable pageable,
PostQuery postQuery) { PostQuery postQuery) {
Page<Post> postPage = postService.pageBy(postQuery, pageable); Page<Post> postPage = postService.pageBy(postQuery, pageable);
return postService.convertToListVo(postPage); return postService.convertToListVo(postPage);
@ -54,7 +54,7 @@ public class PostController {
@ApiOperation("Gets a page of post by post status") @ApiOperation("Gets a page of post by post status")
public Page<? extends BasePostSimpleDTO> pageByStatus(@PathVariable(name = "status") PostStatus status, public Page<? extends BasePostSimpleDTO> pageByStatus(@PathVariable(name = "status") PostStatus status,
@RequestParam(value = "more", required = false, defaultValue = "false") Boolean more, @RequestParam(value = "more", required = false, defaultValue = "false") Boolean more,
@PageableDefault(sort = "editTime", direction = DESC) Pageable pageable) { @PageableDefault(sort = "createTime", direction = DESC) Pageable pageable) {
Page<Post> posts = postService.pageBy(status, pageable); Page<Post> posts = postService.pageBy(status, pageable);
if (more) { if (more) {

View File

@ -4,6 +4,7 @@ import org.springframework.lang.NonNull;
import run.halo.app.model.entity.Category; import run.halo.app.model.entity.Category;
import run.halo.app.repository.base.BaseRepository; import run.halo.app.repository.base.BaseRepository;
import java.util.List;
import java.util.Optional; import java.util.Optional;
/** /**
@ -44,4 +45,11 @@ public interface CategoryRepository extends BaseRepository<Category, Integer> {
* @return Optional of Category * @return Optional of Category
*/ */
Optional<Category> getByName(@NonNull String name); Optional<Category> getByName(@NonNull String name);
/**
* List categories by parent id.
* @param id parent id.
* @return list of category
*/
List<Category> findByParentId(@NonNull Integer id);
} }

View File

@ -65,6 +65,12 @@ public interface CategoryService extends CrudService<Category, Integer> {
@Transactional @Transactional
void removeCategoryAndPostCategoryBy(Integer categoryId); void removeCategoryAndPostCategoryBy(Integer categoryId);
/**
* List categories by parent id.
* @param id parent id.
* @return list of category.
*/
List<Category> listByParentId(@NonNull Integer id);
/** /**
* Converts to category dto. * Converts to category dto.

View File

@ -137,7 +137,7 @@ public abstract class BasePostServiceImpl<POST extends BasePost> extends Abstrac
public Page<POST> pageLatest(int top) { public Page<POST> pageLatest(int top) {
Assert.isTrue(top > 0, "Top number must not be less than 0"); Assert.isTrue(top > 0, "Top number must not be less than 0");
PageRequest latestPageable = PageRequest.of(0, top, Sort.by(DESC, "editTime")); PageRequest latestPageable = PageRequest.of(0, top, Sort.by(DESC, "createTime"));
return listAll(latestPageable); return listAll(latestPageable);
} }
@ -152,7 +152,7 @@ public abstract class BasePostServiceImpl<POST extends BasePost> extends Abstrac
public List<POST> listLatest(int top) { public List<POST> listLatest(int top) {
Assert.isTrue(top > 0, "Top number must not be less than 0"); Assert.isTrue(top > 0, "Top number must not be less than 0");
PageRequest latestPageable = PageRequest.of(0, top, Sort.by(DESC, "editTime")); PageRequest latestPageable = PageRequest.of(0, top, Sort.by(DESC, "createTime"));
return basePostRepository.findAllByStatus(PostStatus.PUBLISHED, latestPageable).getContent(); return basePostRepository.findAllByStatus(PostStatus.PUBLISHED, latestPageable).getContent();
} }

View File

@ -165,12 +165,25 @@ public class CategoryServiceImpl extends AbstractCrudService<Category, Integer>
@Override @Override
@Transactional @Transactional
public void removeCategoryAndPostCategoryBy(Integer categoryId) { public void removeCategoryAndPostCategoryBy(Integer categoryId) {
List<Category> categories = listByParentId(categoryId);
if (null != categories && categories.size() > 0) {
categories.forEach(category -> {
category.setParentId(0);
update(category);
});
}
// Remove category // Remove category
removeById(categoryId); removeById(categoryId);
// Remove post categories // Remove post categories
postCategoryService.removeByCategoryId(categoryId); postCategoryService.removeByCategoryId(categoryId);
} }
@Override
public List<Category> listByParentId(Integer id) {
Assert.notNull(id, "Parent id must not be null");
return categoryRepository.findByParentId(id);
}
@Override @Override
public CategoryDTO convertTo(Category category) { public CategoryDTO convertTo(Category category) {
Assert.notNull(category, "Category must not be null"); Assert.notNull(category, "Category must not be null");