mirror of https://github.com/halo-dev/halo
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 valuepull/1657/head
parent
2ff45600c4
commit
d961a5e8a5
|
@ -53,7 +53,7 @@ public class CategoryController {
|
||||||
@GetMapping
|
@GetMapping
|
||||||
@ApiOperation("Lists all categories")
|
@ApiOperation("Lists all categories")
|
||||||
public List<? extends CategoryDTO> listAll(
|
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) {
|
@RequestParam(name = "more", required = false, defaultValue = "false") boolean more) {
|
||||||
if (more) {
|
if (more) {
|
||||||
return postCategoryService.listCategoryWithPostCountDto(sort, true);
|
return postCategoryService.listCategoryWithPostCountDto(sort, true);
|
||||||
|
@ -64,7 +64,8 @@ public class CategoryController {
|
||||||
|
|
||||||
@GetMapping("tree_view")
|
@GetMapping("tree_view")
|
||||||
@ApiOperation("List all categories as tree")
|
@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);
|
return categoryService.listAsTree(sort);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package run.halo.app.core.freemarker.tag;
|
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.core.Environment;
|
||||||
import freemarker.template.Configuration;
|
import freemarker.template.Configuration;
|
||||||
|
@ -51,11 +51,11 @@ public class CategoryTagDirective implements TemplateDirectiveModel {
|
||||||
switch (method) {
|
switch (method) {
|
||||||
case "list":
|
case "list":
|
||||||
env.setVariable("categories", builder.build().wrap(postCategoryService
|
env.setVariable("categories", builder.build().wrap(postCategoryService
|
||||||
.listCategoryWithPostCountDto(Sort.by(DESC, "createTime"), false)));
|
.listCategoryWithPostCountDto(Sort.by(ASC, "priority"), false)));
|
||||||
break;
|
break;
|
||||||
case "tree":
|
case "tree":
|
||||||
env.setVariable("categories", builder.build()
|
env.setVariable("categories", builder.build()
|
||||||
.wrap(categoryService.listAsTree(Sort.by(DESC, "createTime"))));
|
.wrap(categoryService.listAsTree(Sort.by(ASC, "priority"))));
|
||||||
break;
|
break;
|
||||||
case "listByPostId":
|
case "listByPostId":
|
||||||
Integer postId = Integer.parseInt(params.get("postId").toString());
|
Integer postId = Integer.parseInt(params.get("postId").toString());
|
||||||
|
|
|
@ -36,4 +36,6 @@ public class CategoryDTO implements OutputConverter<CategoryDTO, Category> {
|
||||||
private Date createTime;
|
private Date createTime;
|
||||||
|
|
||||||
private String fullPath;
|
private String fullPath;
|
||||||
|
|
||||||
|
private Integer priority;
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,6 +73,13 @@ public class Category extends BaseEntity {
|
||||||
@ColumnDefault("0")
|
@ColumnDefault("0")
|
||||||
private Integer parentId;
|
private Integer parentId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Priority category.
|
||||||
|
*/
|
||||||
|
@Column(name = "priority")
|
||||||
|
@ColumnDefault("0")
|
||||||
|
private Integer priority;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Category password.
|
* Category password.
|
||||||
*/
|
*/
|
||||||
|
@ -90,6 +97,10 @@ public class Category extends BaseEntity {
|
||||||
if (parentId == null || parentId < 0) {
|
if (parentId == null || parentId < 0) {
|
||||||
parentId = 0;
|
parentId = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (priority == null) {
|
||||||
|
priority = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package run.halo.app.model.params;
|
package run.halo.app.model.params;
|
||||||
|
|
||||||
|
import javax.validation.constraints.Min;
|
||||||
import javax.validation.constraints.NotBlank;
|
import javax.validation.constraints.NotBlank;
|
||||||
import javax.validation.constraints.Size;
|
import javax.validation.constraints.Size;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
@ -36,6 +37,9 @@ public class CategoryParam implements InputConverter<Category> {
|
||||||
|
|
||||||
private Integer parentId = 0;
|
private Integer parentId = 0;
|
||||||
|
|
||||||
|
@Min(value = 0, message = "排序编号不能低于 {value}")
|
||||||
|
private Integer priority;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Category convertTo() {
|
public Category convertTo() {
|
||||||
|
|
||||||
|
|
|
@ -32,22 +32,24 @@ public class CategoryServiceTest {
|
||||||
assertEquals(
|
assertEquals(
|
||||||
"[{\"id\":1,\"name\":\"分类-1\",\"slug\":\"category1\",\"description\":null,"
|
"[{\"id\":1,\"name\":\"分类-1\",\"slug\":\"category1\",\"description\":null,"
|
||||||
+ "\"thumbnail\":null,\"parentId\":0,\"password\":null,\"createTime\":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\","
|
+ "\"children\":[]},{\"id\":2,\"name\":\"分类-2\",\"slug\":\"category2\","
|
||||||
+ "\"description\":null,\"thumbnail\":null,\"parentId\":0,\"password\":null,"
|
+ "\"description\":null,\"thumbnail\":null,\"parentId\":0,\"password\":null,"
|
||||||
+ "\"createTime\":null,\"fullPath\":\"http://127.0.0.1:8090/categories/category2\","
|
+ "\"createTime\":null,\"fullPath\":\"http://127.0.0.1:8090/categories/category2\","
|
||||||
+ "\"children\":[{\"id\":3,\"name\":\"分类-2-1\",\"slug\":\"category21\","
|
+ "\"priority\":1,\"children\":[{\"id\":3,\"name\":\"分类-2-1\","
|
||||||
+ "\"description\":null,\"thumbnail\":null,\"parentId\":2,\"password\":null,"
|
+ "\"slug\":\"category21\",\"description\":null,\"thumbnail\":null,\"parentId\":2,"
|
||||||
+ "\"createTime\":null,\"fullPath\":\"http://127.0.0.1:8090/categories/category21\","
|
+ "\"password\":null,\"createTime\":null,\"fullPath\":\"http://127.0.0"
|
||||||
+ "\"children\":[{\"id\":5,\"name\":\"分类-2-1-1\",\"slug\":\"category211\","
|
+ ".1:8090/categories/category21\",\"priority\":2,\"children\":[{\"id\":5,"
|
||||||
+ "\"description\":null,\"thumbnail\":null,\"parentId\":3,\"password\":null,"
|
+ "\"name\":\"分类-2-1-1\",\"slug\":\"category211\",\"description\":null,"
|
||||||
+ "\"createTime\":null,\"fullPath\":\"http://127.0.0.1:8090/categories/category211\","
|
+ "\"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\","
|
+ "\"children\":[{\"id\":6,\"name\":\"分类-2-1-1-1\",\"slug\":\"category2111\","
|
||||||
+ "\"description\":null,\"thumbnail\":null,\"parentId\":5,\"password\":null,"
|
+ "\"description\":null,\"thumbnail\":null,\"parentId\":5,\"password\":null,"
|
||||||
+ "\"createTime\":null,\"fullPath\":\"http://127.0.0.1:8090/categories/category2111\","
|
+ "\"createTime\":null,\"fullPath\":\"http://127.0.0"
|
||||||
+ "\"children\":[]}]}]},{\"id\":4,\"name\":\"分类-2-2\",\"slug\":\"category22\","
|
+ ".1:8090/categories/category2111\",\"priority\":5,\"children\":[]}]}]},"
|
||||||
+ "\"description\":null,\"thumbnail\":null,\"parentId\":2,\"password\":null,"
|
+ "{\"id\":4,\"name\":\"分类-2-2\",\"slug\":\"category22\",\"description\":null,"
|
||||||
+ "\"createTime\":null,\"fullPath\":\"http://127.0.0.1:8090/categories/category22\","
|
+ "\"thumbnail\":null,\"parentId\":2,\"password\":null,\"createTime\":null,"
|
||||||
|
+ "\"fullPath\":\"http://127.0.0.1:8090/categories/category22\",\"priority\":3,"
|
||||||
+ "\"children\":[]}]}]",
|
+ "\"children\":[]}]}]",
|
||||||
JsonUtils.objectToJson(categoryVoList));
|
JsonUtils.objectToJson(categoryVoList));
|
||||||
}
|
}
|
||||||
|
@ -58,36 +60,42 @@ public class CategoryServiceTest {
|
||||||
category1.setName("分类-1");
|
category1.setName("分类-1");
|
||||||
category1.setSlug("category1");
|
category1.setSlug("category1");
|
||||||
category1.setParentId(0);
|
category1.setParentId(0);
|
||||||
|
category1.setPriority(0);
|
||||||
|
|
||||||
Category category2 = new Category();
|
Category category2 = new Category();
|
||||||
category2.setId(2);
|
category2.setId(2);
|
||||||
category2.setName("分类-2");
|
category2.setName("分类-2");
|
||||||
category2.setSlug("category2");
|
category2.setSlug("category2");
|
||||||
category2.setParentId(0);
|
category2.setParentId(0);
|
||||||
|
category2.setPriority(1);
|
||||||
|
|
||||||
Category category3 = new Category();
|
Category category3 = new Category();
|
||||||
category3.setId(3);
|
category3.setId(3);
|
||||||
category3.setName("分类-2-1");
|
category3.setName("分类-2-1");
|
||||||
category3.setSlug("category21");
|
category3.setSlug("category21");
|
||||||
category3.setParentId(2);
|
category3.setParentId(2);
|
||||||
|
category3.setPriority(2);
|
||||||
|
|
||||||
Category category4 = new Category();
|
Category category4 = new Category();
|
||||||
category4.setId(4);
|
category4.setId(4);
|
||||||
category4.setName("分类-2-2");
|
category4.setName("分类-2-2");
|
||||||
category4.setSlug("category22");
|
category4.setSlug("category22");
|
||||||
category4.setParentId(2);
|
category4.setParentId(2);
|
||||||
|
category4.setPriority(3);
|
||||||
|
|
||||||
Category category5 = new Category();
|
Category category5 = new Category();
|
||||||
category5.setId(5);
|
category5.setId(5);
|
||||||
category5.setName("分类-2-1-1");
|
category5.setName("分类-2-1-1");
|
||||||
category5.setSlug("category211");
|
category5.setSlug("category211");
|
||||||
category5.setParentId(3);
|
category5.setParentId(3);
|
||||||
|
category5.setPriority(4);
|
||||||
|
|
||||||
Category category6 = new Category();
|
Category category6 = new Category();
|
||||||
category6.setId(6);
|
category6.setId(6);
|
||||||
category6.setName("分类-2-1-1-1");
|
category6.setName("分类-2-1-1-1");
|
||||||
category6.setSlug("category2111");
|
category6.setSlug("category2111");
|
||||||
category6.setParentId(5);
|
category6.setParentId(5);
|
||||||
|
category6.setPriority(5);
|
||||||
return List.of(category1, category2, category3, category4, category5, category6);
|
return List.of(category1, category2, category3, category4, category5, category6);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue