From d961a5e8a5c0deb889da4a447542fb098bbad03f Mon Sep 17 00:00:00 2001 From: lan-yonghui <81747598+lan-yonghui@users.noreply.github.com> Date: Tue, 15 Feb 2022 17:50:37 +0800 Subject: [PATCH] feat: add priority field into category table (#1650) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 分类增加排序字段 * style: Optimize and change categories sorting codes * style: delete sql file * style: update categories priority default value 0 * style: Optimize categories priority default value --- .../admin/api/CategoryController.java | 5 ++-- .../freemarker/tag/CategoryTagDirective.java | 6 ++-- .../run/halo/app/model/dto/CategoryDTO.java | 2 ++ .../run/halo/app/model/entity/Category.java | 11 +++++++ .../halo/app/model/params/CategoryParam.java | 4 +++ .../app/service/impl/CategoryServiceTest.java | 30 ++++++++++++------- 6 files changed, 42 insertions(+), 16 deletions(-) diff --git a/src/main/java/run/halo/app/controller/admin/api/CategoryController.java b/src/main/java/run/halo/app/controller/admin/api/CategoryController.java index de41c6d9ac..523a9703e5 100644 --- a/src/main/java/run/halo/app/controller/admin/api/CategoryController.java +++ b/src/main/java/run/halo/app/controller/admin/api/CategoryController.java @@ -53,7 +53,7 @@ public CategoryDTO getBy(@PathVariable("categoryId") Integer categoryId) { @GetMapping @ApiOperation("Lists all categories") public List listAll( - @SortDefault(sort = "createTime", direction = DESC) Sort sort, + @SortDefault(sort = "priority", direction = ASC) Sort sort, @RequestParam(name = "more", required = false, defaultValue = "false") boolean more) { if (more) { return postCategoryService.listCategoryWithPostCountDto(sort, true); @@ -64,7 +64,8 @@ public List listAll( @GetMapping("tree_view") @ApiOperation("List all categories as tree") - public List listAsTree(@SortDefault(sort = "name", direction = ASC) Sort sort) { + public List listAsTree( + @SortDefault(sort = "priority", direction = ASC) Sort sort) { return categoryService.listAsTree(sort); } diff --git a/src/main/java/run/halo/app/core/freemarker/tag/CategoryTagDirective.java b/src/main/java/run/halo/app/core/freemarker/tag/CategoryTagDirective.java index 447a6e1c69..e3e69a008f 100644 --- a/src/main/java/run/halo/app/core/freemarker/tag/CategoryTagDirective.java +++ b/src/main/java/run/halo/app/core/freemarker/tag/CategoryTagDirective.java @@ -1,6 +1,6 @@ package run.halo.app.core.freemarker.tag; -import static org.springframework.data.domain.Sort.Direction.DESC; +import static org.springframework.data.domain.Sort.Direction.ASC; import freemarker.core.Environment; import freemarker.template.Configuration; @@ -51,11 +51,11 @@ public void execute(Environment env, Map params, TemplateModel[] loopVars, switch (method) { case "list": env.setVariable("categories", builder.build().wrap(postCategoryService - .listCategoryWithPostCountDto(Sort.by(DESC, "createTime"), false))); + .listCategoryWithPostCountDto(Sort.by(ASC, "priority"), false))); break; case "tree": env.setVariable("categories", builder.build() - .wrap(categoryService.listAsTree(Sort.by(DESC, "createTime")))); + .wrap(categoryService.listAsTree(Sort.by(ASC, "priority")))); break; case "listByPostId": Integer postId = Integer.parseInt(params.get("postId").toString()); diff --git a/src/main/java/run/halo/app/model/dto/CategoryDTO.java b/src/main/java/run/halo/app/model/dto/CategoryDTO.java index 01c02b986d..12d2fb9cde 100644 --- a/src/main/java/run/halo/app/model/dto/CategoryDTO.java +++ b/src/main/java/run/halo/app/model/dto/CategoryDTO.java @@ -36,4 +36,6 @@ public class CategoryDTO implements OutputConverter { private Date createTime; private String fullPath; + + private Integer priority; } diff --git a/src/main/java/run/halo/app/model/entity/Category.java b/src/main/java/run/halo/app/model/entity/Category.java index ec6fcc1ecd..51bc9522f1 100644 --- a/src/main/java/run/halo/app/model/entity/Category.java +++ b/src/main/java/run/halo/app/model/entity/Category.java @@ -73,6 +73,13 @@ public class Category extends BaseEntity { @ColumnDefault("0") private Integer parentId; + /** + * Priority category. + */ + @Column(name = "priority") + @ColumnDefault("0") + private Integer priority; + /** * Category password. */ @@ -90,6 +97,10 @@ public void prePersist() { if (parentId == null || parentId < 0) { parentId = 0; } + + if (priority == null) { + priority = 0; + } } } diff --git a/src/main/java/run/halo/app/model/params/CategoryParam.java b/src/main/java/run/halo/app/model/params/CategoryParam.java index c1b02ac74c..98bfb12b92 100644 --- a/src/main/java/run/halo/app/model/params/CategoryParam.java +++ b/src/main/java/run/halo/app/model/params/CategoryParam.java @@ -1,5 +1,6 @@ package run.halo.app.model.params; +import javax.validation.constraints.Min; import javax.validation.constraints.NotBlank; import javax.validation.constraints.Size; import lombok.Data; @@ -36,6 +37,9 @@ public class CategoryParam implements InputConverter { private Integer parentId = 0; + @Min(value = 0, message = "排序编号不能低于 {value}") + private Integer priority; + @Override public Category convertTo() { diff --git a/src/test/java/run/halo/app/service/impl/CategoryServiceTest.java b/src/test/java/run/halo/app/service/impl/CategoryServiceTest.java index c96c41ed4a..883dfe1cd7 100644 --- a/src/test/java/run/halo/app/service/impl/CategoryServiceTest.java +++ b/src/test/java/run/halo/app/service/impl/CategoryServiceTest.java @@ -32,22 +32,24 @@ void listToTree() throws JsonProcessingException { assertEquals( "[{\"id\":1,\"name\":\"分类-1\",\"slug\":\"category1\",\"description\":null," + "\"thumbnail\":null,\"parentId\":0,\"password\":null,\"createTime\":null," - + "\"fullPath\":\"http://127.0.0.1:8090/categories/category1\"," + + "\"fullPath\":\"http://127.0.0.1:8090/categories/category1\",\"priority\":0," + "\"children\":[]},{\"id\":2,\"name\":\"分类-2\",\"slug\":\"category2\"," + "\"description\":null,\"thumbnail\":null,\"parentId\":0,\"password\":null," + "\"createTime\":null,\"fullPath\":\"http://127.0.0.1:8090/categories/category2\"," - + "\"children\":[{\"id\":3,\"name\":\"分类-2-1\",\"slug\":\"category21\"," - + "\"description\":null,\"thumbnail\":null,\"parentId\":2,\"password\":null," - + "\"createTime\":null,\"fullPath\":\"http://127.0.0.1:8090/categories/category21\"," - + "\"children\":[{\"id\":5,\"name\":\"分类-2-1-1\",\"slug\":\"category211\"," - + "\"description\":null,\"thumbnail\":null,\"parentId\":3,\"password\":null," - + "\"createTime\":null,\"fullPath\":\"http://127.0.0.1:8090/categories/category211\"," + + "\"priority\":1,\"children\":[{\"id\":3,\"name\":\"分类-2-1\"," + + "\"slug\":\"category21\",\"description\":null,\"thumbnail\":null,\"parentId\":2," + + "\"password\":null,\"createTime\":null,\"fullPath\":\"http://127.0.0" + + ".1:8090/categories/category21\",\"priority\":2,\"children\":[{\"id\":5," + + "\"name\":\"分类-2-1-1\",\"slug\":\"category211\",\"description\":null," + + "\"thumbnail\":null,\"parentId\":3,\"password\":null,\"createTime\":null," + + "\"fullPath\":\"http://127.0.0.1:8090/categories/category211\",\"priority\":4," + "\"children\":[{\"id\":6,\"name\":\"分类-2-1-1-1\",\"slug\":\"category2111\"," + "\"description\":null,\"thumbnail\":null,\"parentId\":5,\"password\":null," - + "\"createTime\":null,\"fullPath\":\"http://127.0.0.1:8090/categories/category2111\"," - + "\"children\":[]}]}]},{\"id\":4,\"name\":\"分类-2-2\",\"slug\":\"category22\"," - + "\"description\":null,\"thumbnail\":null,\"parentId\":2,\"password\":null," - + "\"createTime\":null,\"fullPath\":\"http://127.0.0.1:8090/categories/category22\"," + + "\"createTime\":null,\"fullPath\":\"http://127.0.0" + + ".1:8090/categories/category2111\",\"priority\":5,\"children\":[]}]}]}," + + "{\"id\":4,\"name\":\"分类-2-2\",\"slug\":\"category22\",\"description\":null," + + "\"thumbnail\":null,\"parentId\":2,\"password\":null,\"createTime\":null," + + "\"fullPath\":\"http://127.0.0.1:8090/categories/category22\",\"priority\":3," + "\"children\":[]}]}]", JsonUtils.objectToJson(categoryVoList)); } @@ -58,36 +60,42 @@ private List mockCategories() { category1.setName("分类-1"); category1.setSlug("category1"); category1.setParentId(0); + category1.setPriority(0); Category category2 = new Category(); category2.setId(2); category2.setName("分类-2"); category2.setSlug("category2"); category2.setParentId(0); + category2.setPriority(1); Category category3 = new Category(); category3.setId(3); category3.setName("分类-2-1"); category3.setSlug("category21"); category3.setParentId(2); + category3.setPriority(2); Category category4 = new Category(); category4.setId(4); category4.setName("分类-2-2"); category4.setSlug("category22"); category4.setParentId(2); + category4.setPriority(3); Category category5 = new Category(); category5.setId(5); category5.setName("分类-2-1-1"); category5.setSlug("category211"); category5.setParentId(3); + category5.setPriority(4); Category category6 = new Category(); category6.setId(6); category6.setName("分类-2-1-1-1"); category6.setSlug("category2111"); category6.setParentId(5); + category6.setPriority(5); return List.of(category1, category2, category3, category4, category5, category6); } }