diff --git a/src/main/java/run/halo/app/controller/admin/api/PostController.java b/src/main/java/run/halo/app/controller/admin/api/PostController.java index d69f1ff25..2db8c2f4b 100644 --- a/src/main/java/run/halo/app/controller/admin/api/PostController.java +++ b/src/main/java/run/halo/app/controller/admin/api/PostController.java @@ -38,7 +38,7 @@ public class PostController { @GetMapping @ApiOperation("Lists posts") - public Page pageBy(@PageableDefault(sort = "updateTime", direction = DESC) Pageable pageable, + public Page pageBy(@PageableDefault(sort = "createTime", direction = DESC) Pageable pageable, PostQuery postQuery) { Page postPage = postService.pageBy(postQuery, pageable); return postService.convertToListVo(postPage); @@ -54,7 +54,7 @@ public class PostController { @ApiOperation("Gets a page of post by post status") public Page pageByStatus(@PathVariable(name = "status") PostStatus status, @RequestParam(value = "more", required = false, defaultValue = "false") Boolean more, - @PageableDefault(sort = "editTime", direction = DESC) Pageable pageable) { + @PageableDefault(sort = "createTime", direction = DESC) Pageable pageable) { Page posts = postService.pageBy(status, pageable); if (more) { diff --git a/src/main/java/run/halo/app/repository/CategoryRepository.java b/src/main/java/run/halo/app/repository/CategoryRepository.java index 078e87a35..f01e079ee 100644 --- a/src/main/java/run/halo/app/repository/CategoryRepository.java +++ b/src/main/java/run/halo/app/repository/CategoryRepository.java @@ -4,6 +4,7 @@ import org.springframework.lang.NonNull; import run.halo.app.model.entity.Category; import run.halo.app.repository.base.BaseRepository; +import java.util.List; import java.util.Optional; /** @@ -44,4 +45,11 @@ public interface CategoryRepository extends BaseRepository { * @return Optional of Category */ Optional getByName(@NonNull String name); + + /** + * List categories by parent id. + * @param id parent id. + * @return list of category + */ + List findByParentId(@NonNull Integer id); } diff --git a/src/main/java/run/halo/app/service/CategoryService.java b/src/main/java/run/halo/app/service/CategoryService.java index d0a76e021..fee7021cb 100755 --- a/src/main/java/run/halo/app/service/CategoryService.java +++ b/src/main/java/run/halo/app/service/CategoryService.java @@ -65,6 +65,12 @@ public interface CategoryService extends CrudService { @Transactional void removeCategoryAndPostCategoryBy(Integer categoryId); + /** + * List categories by parent id. + * @param id parent id. + * @return list of category. + */ + List listByParentId(@NonNull Integer id); /** * Converts to category dto. diff --git a/src/main/java/run/halo/app/service/impl/BasePostServiceImpl.java b/src/main/java/run/halo/app/service/impl/BasePostServiceImpl.java index ee36ee138..30f2f1a3b 100644 --- a/src/main/java/run/halo/app/service/impl/BasePostServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/BasePostServiceImpl.java @@ -137,7 +137,7 @@ public abstract class BasePostServiceImpl extends Abstrac public Page pageLatest(int top) { 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); } @@ -152,7 +152,7 @@ public abstract class BasePostServiceImpl extends Abstrac public List listLatest(int top) { 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(); } 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 e7da2c132..c88323f0c 100644 --- a/src/main/java/run/halo/app/service/impl/CategoryServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/CategoryServiceImpl.java @@ -165,12 +165,25 @@ public class CategoryServiceImpl extends AbstractCrudService @Override @Transactional public void removeCategoryAndPostCategoryBy(Integer categoryId) { + List categories = listByParentId(categoryId); + if (null != categories && categories.size() > 0) { + categories.forEach(category -> { + category.setParentId(0); + update(category); + }); + } // Remove category removeById(categoryId); // Remove post categories postCategoryService.removeByCategoryId(categoryId); } + @Override + public List listByParentId(Integer id) { + Assert.notNull(id, "Parent id must not be null"); + return categoryRepository.findByParentId(id); + } + @Override public CategoryDTO convertTo(Category category) { Assert.notNull(category, "Category must not be null");