Skip to content

Commit

Permalink
feat: add priority field into category table (#1650)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
lan-yonghui authored Feb 15, 2022
1 parent 2ff4560 commit d961a5e
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public CategoryDTO getBy(@PathVariable("categoryId") Integer categoryId) {
@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);
Expand All @@ -64,7 +64,8 @@ public List<? extends CategoryDTO> listAll(

@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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/run/halo/app/model/dto/CategoryDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ public class CategoryDTO implements OutputConverter<CategoryDTO, Category> {
private Date createTime;

private String fullPath;

private Integer priority;
}
11 changes: 11 additions & 0 deletions src/main/java/run/halo/app/model/entity/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -90,6 +97,10 @@ public void prePersist() {
if (parentId == null || parentId < 0) {
parentId = 0;
}

if (priority == null) {
priority = 0;
}
}

}
4 changes: 4 additions & 0 deletions src/main/java/run/halo/app/model/params/CategoryParam.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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() {

Expand Down
30 changes: 19 additions & 11 deletions src/test/java/run/halo/app/service/impl/CategoryServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -58,36 +60,42 @@ private List<Category> 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);
}
}

0 comments on commit d961a5e

Please sign in to comment.