diff --git a/open-isle-cli/src/components/CategorySelect.vue b/open-isle-cli/src/components/CategorySelect.vue index c81b2da4e..fd6ebfe4e 100644 --- a/open-isle-cli/src/components/CategorySelect.vue +++ b/open-isle-cli/src/components/CategorySelect.vue @@ -1,5 +1,17 @@ + + diff --git a/open-isle-cli/src/components/TagSelect.vue b/open-isle-cli/src/components/TagSelect.vue index 85cb36c50..8d8498712 100644 --- a/open-isle-cli/src/components/TagSelect.vue +++ b/open-isle-cli/src/components/TagSelect.vue @@ -5,7 +5,19 @@ multiple placeholder="选择标签" remote - /> + > + + + + diff --git a/src/main/java/com/openisle/controller/CategoryController.java b/src/main/java/com/openisle/controller/CategoryController.java index 7e1dac930..ab6efed0b 100644 --- a/src/main/java/com/openisle/controller/CategoryController.java +++ b/src/main/java/com/openisle/controller/CategoryController.java @@ -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 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 diff --git a/src/main/java/com/openisle/controller/TagController.java b/src/main/java/com/openisle/controller/TagController.java index 594fb6738..db275dfa8 100644 --- a/src/main/java/com/openisle/controller/TagController.java +++ b/src/main/java/com/openisle/controller/TagController.java @@ -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 list() { - return tagService.listTags().stream() - .map(this::toDto) + public List list(@RequestParam(value = "keyword", required = false) String keyword, + @RequestParam(value = "limit", required = false) Integer limit) { + List 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 diff --git a/src/main/java/com/openisle/repository/PostRepository.java b/src/main/java/com/openisle/repository/PostRepository.java index 22e541c44..ec2ead647 100644 --- a/src/main/java/com/openisle/repository/PostRepository.java +++ b/src/main/java/com/openisle/repository/PostRepository.java @@ -41,4 +41,8 @@ public interface PostRepository extends JpaRepository { @Query("SELECT SUM(p.views) FROM Post p WHERE p.author.username = :username AND p.status = com.openisle.model.PostStatus.PUBLISHED") Long sumViews(@Param("username") String username); + + long countByCategory_Id(Long categoryId); + + long countDistinctByTags_Id(Long tagId); } diff --git a/src/main/java/com/openisle/repository/TagRepository.java b/src/main/java/com/openisle/repository/TagRepository.java index 197d21a14..6d3722e2a 100644 --- a/src/main/java/com/openisle/repository/TagRepository.java +++ b/src/main/java/com/openisle/repository/TagRepository.java @@ -3,5 +3,8 @@ package com.openisle.repository; import com.openisle.model.Tag; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; + public interface TagRepository extends JpaRepository { + List findByNameContainingIgnoreCase(String keyword); } diff --git a/src/main/java/com/openisle/service/PostService.java b/src/main/java/com/openisle/service/PostService.java index 15d10db25..8408e2e83 100644 --- a/src/main/java/com/openisle/service/PostService.java +++ b/src/main/java/com/openisle/service/PostService.java @@ -274,4 +274,12 @@ public class PostService { public java.util.List getPostsByIds(java.util.List ids) { return postRepository.findAllById(ids); } + + public long countPostsByCategory(Long categoryId) { + return postRepository.countByCategory_Id(categoryId); + } + + public long countPostsByTag(Long tagId) { + return postRepository.countDistinctByTags_Id(tagId); + } } diff --git a/src/main/java/com/openisle/service/TagService.java b/src/main/java/com/openisle/service/TagService.java index 48c015e32..ad0601639 100644 --- a/src/main/java/com/openisle/service/TagService.java +++ b/src/main/java/com/openisle/service/TagService.java @@ -54,4 +54,11 @@ public class TagService { public List listTags() { return tagRepository.findAll(); } + + public List searchTags(String keyword) { + if (keyword == null || keyword.isBlank()) { + return tagRepository.findAll(); + } + return tagRepository.findByNameContainingIgnoreCase(keyword); + } }