Add endpoints to list posts by tags and categories

This commit is contained in:
Tim
2025-07-02 14:39:54 +08:00
parent a6fd1b52ed
commit 8dbc940305
5 changed files with 83 additions and 0 deletions

View File

@@ -52,12 +52,23 @@ public class PostController {
@GetMapping
public List<PostDto> listPosts(@RequestParam(value = "categoryId", required = false) Long categoryId,
@RequestParam(value = "categoryIds", required = false) List<Long> categoryIds,
@RequestParam(value = "tagId", required = false) Long tagId,
@RequestParam(value = "tagIds", required = false) List<Long> tagIds,
@RequestParam(value = "page", required = false) Integer page,
@RequestParam(value = "pageSize", required = false) Integer pageSize) {
List<Long> ids = categoryIds;
if (categoryId != null) {
ids = java.util.List.of(categoryId);
}
if ((tagId != null || (tagIds != null && !tagIds.isEmpty()))) {
List<Long> tids = tagIds;
if (tagId != null) {
tids = java.util.List.of(tagId);
}
return postService.listPostsByTags(tids, page, pageSize)
.stream().map(this::toDto).collect(Collectors.toList());
}
return postService.listPostsByCategories(ids, page, pageSize)
.stream().map(this::toDto).collect(Collectors.toList());
}