feat: add priority field into category table (#1650)

* 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
pull/1657/head
lan-yonghui 2022-02-15 17:50:37 +08:00 committed by GitHub
parent 2ff45600c4
commit d961a5e8a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 42 additions and 16 deletions

View File

@ -53,7 +53,7 @@ public class CategoryController {
@GetMapping
@ApiOperation("Lists all categories")
public List<? extends CategoryDTO> 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 class CategoryController {
@GetMapping("tree_view")
@ApiOperation("List all categories as tree")
public List<CategoryVO> listAsTree(@SortDefault(sort = "name", direction = ASC) Sort sort) {
public List<CategoryVO> listAsTree(
@SortDefault(sort = "priority", direction = ASC) Sort sort) {
return categoryService.listAsTree(sort);
}

View File

@ -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 class CategoryTagDirective implements TemplateDirectiveModel {
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());

View File

@ -36,4 +36,6 @@ public class CategoryDTO implements OutputConverter<CategoryDTO, Category> {
private Date createTime;
private String fullPath;
private Integer priority;
}

View File

@ -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 class Category extends BaseEntity {
if (parentId == null || parentId < 0) {
parentId = 0;
}
if (priority == null) {
priority = 0;
}
}
}

View File

@ -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<Category> {
private Integer parentId = 0;
@Min(value = 0, message = "排序编号不能低于 {value}")
private Integer priority;
@Override
public Category convertTo() {

View File

@ -32,22 +32,24 @@ public class CategoryServiceTest {
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 @@ public class CategoryServiceTest {
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);
}
}