Add count and description to category and tag dropdown

This commit is contained in:
Tim
2025-07-10 11:51:04 +08:00
parent bdbc7e72b7
commit a9cffc6e42
8 changed files with 139 additions and 38 deletions

View File

@@ -20,13 +20,15 @@ public class CategoryController {
@PostMapping
public CategoryDto create(@RequestBody CategoryRequest req) {
Category c = categoryService.createCategory(req.getName(), req.getDescription(), req.getIcon(), req.getSmallIcon());
return toDto(c);
long count = postService.countPostsByCategory(c.getId());
return toDto(c, count);
}
@PutMapping("/{id}")
public CategoryDto update(@PathVariable Long id, @RequestBody CategoryRequest req) {
Category c = categoryService.updateCategory(id, req.getName(), req.getDescription(), req.getIcon(), req.getSmallIcon());
return toDto(c);
long count = postService.countPostsByCategory(c.getId());
return toDto(c, count);
}
@DeleteMapping("/{id}")
@@ -37,13 +39,16 @@ public class CategoryController {
@GetMapping
public List<CategoryDto> list() {
return categoryService.listCategories().stream()
.map(this::toDto)
.map(c -> toDto(c, postService.countPostsByCategory(c.getId())))
.sorted((a, b) -> Long.compare(b.getCount(), a.getCount()))
.collect(Collectors.toList());
}
@GetMapping("/{id}")
public CategoryDto get(@PathVariable Long id) {
return toDto(categoryService.getCategory(id));
Category c = categoryService.getCategory(id);
long count = postService.countPostsByCategory(c.getId());
return toDto(c, count);
}
@GetMapping("/{id}/posts")
@@ -61,13 +66,14 @@ public class CategoryController {
.collect(Collectors.toList());
}
private CategoryDto toDto(Category c) {
private CategoryDto toDto(Category c, long count) {
CategoryDto dto = new CategoryDto();
dto.setId(c.getId());
dto.setName(c.getName());
dto.setIcon(c.getIcon());
dto.setSmallIcon(c.getSmallIcon());
dto.setDescription(c.getDescription());
dto.setCount(count);
return dto;
}
@@ -86,6 +92,7 @@ public class CategoryController {
private String description;
private String icon;
private String smallIcon;
private Long count;
}
@Data

View File

@@ -20,13 +20,15 @@ public class TagController {
@PostMapping
public TagDto create(@RequestBody TagRequest req) {
Tag tag = tagService.createTag(req.getName(), req.getDescription(), req.getIcon(), req.getSmallIcon());
return toDto(tag);
long count = postService.countPostsByTag(tag.getId());
return toDto(tag, count);
}
@PutMapping("/{id}")
public TagDto update(@PathVariable Long id, @RequestBody TagRequest req) {
Tag tag = tagService.updateTag(id, req.getName(), req.getDescription(), req.getIcon(), req.getSmallIcon());
return toDto(tag);
long count = postService.countPostsByTag(tag.getId());
return toDto(tag, count);
}
@DeleteMapping("/{id}")
@@ -35,15 +37,23 @@ public class TagController {
}
@GetMapping
public List<TagDto> list() {
return tagService.listTags().stream()
.map(this::toDto)
public List<TagDto> list(@RequestParam(value = "keyword", required = false) String keyword,
@RequestParam(value = "limit", required = false) Integer limit) {
List<TagDto> dtos = tagService.searchTags(keyword).stream()
.map(t -> toDto(t, postService.countPostsByTag(t.getId())))
.sorted((a, b) -> Long.compare(b.getCount(), a.getCount()))
.collect(Collectors.toList());
if (limit != null && limit > 0 && dtos.size() > limit) {
return dtos.subList(0, limit);
}
return dtos;
}
@GetMapping("/{id}")
public TagDto get(@PathVariable Long id) {
return toDto(tagService.getTag(id));
Tag tag = tagService.getTag(id);
long count = postService.countPostsByTag(tag.getId());
return toDto(tag, count);
}
@GetMapping("/{id}/posts")
@@ -61,13 +71,14 @@ public class TagController {
.collect(Collectors.toList());
}
private TagDto toDto(Tag tag) {
private TagDto toDto(Tag tag, long count) {
TagDto dto = new TagDto();
dto.setId(tag.getId());
dto.setName(tag.getName());
dto.setIcon(tag.getIcon());
dto.setSmallIcon(tag.getSmallIcon());
dto.setDescription(tag.getDescription());
dto.setCount(count);
return dto;
}
@@ -86,6 +97,7 @@ public class TagController {
private String description;
private String icon;
private String smallIcon;
private Long count;
}
@Data