Merge pull request #36 from nagisa77/codex/update-post-endpoint-for-theme-filtering-and-pagination

Add post listing filters and pagination
This commit is contained in:
Tim
2025-07-01 16:03:20 +08:00
committed by GitHub
4 changed files with 39 additions and 4 deletions

View File

@@ -49,8 +49,16 @@ public class PostController {
} }
@GetMapping @GetMapping
public List<PostDto> listPosts() { public List<PostDto> listPosts(@RequestParam(value = "categoryId", required = false) Long categoryId,
return postService.listPosts().stream().map(this::toDto).collect(Collectors.toList()); @RequestParam(value = "categoryIds", required = false) List<Long> categoryIds,
@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);
}
return postService.listPostsByCategories(ids, page, pageSize)
.stream().map(this::toDto).collect(Collectors.toList());
} }
private PostDto toDto(Post post) { private PostDto toDto(Post post) {

View File

@@ -3,6 +3,7 @@ package com.openisle.repository;
import com.openisle.model.Post; import com.openisle.model.Post;
import com.openisle.model.PostStatus; import com.openisle.model.PostStatus;
import com.openisle.model.User; import com.openisle.model.User;
import com.openisle.model.Category;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
@@ -10,5 +11,8 @@ import java.util.List;
public interface PostRepository extends JpaRepository<Post, Long> { public interface PostRepository extends JpaRepository<Post, Long> {
List<Post> findByStatus(PostStatus status); List<Post> findByStatus(PostStatus status);
List<Post> findByStatus(PostStatus status, Pageable pageable);
List<Post> findByAuthorAndStatusOrderByCreatedAtDesc(User author, PostStatus status, Pageable pageable); List<Post> findByAuthorAndStatusOrderByCreatedAtDesc(User author, PostStatus status, Pageable pageable);
List<Post> findByCategoryInAndStatus(List<Category> categories, PostStatus status);
List<Post> findByCategoryInAndStatus(List<Category> categories, PostStatus status, Pageable pageable);
} }

View File

@@ -58,7 +58,29 @@ public class PostService {
} }
public List<Post> listPosts() { public List<Post> listPosts() {
return postRepository.findByStatus(PostStatus.PUBLISHED); return listPostsByCategories(null, null, null);
}
public List<Post> listPostsByCategories(java.util.List<Long> categoryIds,
Integer page,
Integer pageSize) {
Pageable pageable = null;
if (page != null && pageSize != null) {
pageable = PageRequest.of(page, pageSize);
}
if (categoryIds == null || categoryIds.isEmpty()) {
if (pageable != null) {
return postRepository.findByStatus(PostStatus.PUBLISHED, pageable);
}
return postRepository.findByStatus(PostStatus.PUBLISHED);
}
java.util.List<Category> categories = categoryRepository.findAllById(categoryIds);
if (pageable != null) {
return postRepository.findByCategoryInAndStatus(categories, PostStatus.PUBLISHED, pageable);
}
return postRepository.findByCategoryInAndStatus(categories, PostStatus.PUBLISHED);
} }
public List<Post> getRecentPostsByUser(String username, int limit) { public List<Post> getRecentPostsByUser(String username, int limit) {

View File

@@ -81,7 +81,8 @@ class PostControllerTest {
post.setCreatedAt(LocalDateTime.now()); post.setCreatedAt(LocalDateTime.now());
post.setAuthor(user); post.setAuthor(user);
post.setCategory(cat); post.setCategory(cat);
Mockito.when(postService.listPosts()).thenReturn(List.of(post)); Mockito.when(postService.listPostsByCategories(Mockito.isNull(), Mockito.isNull(), Mockito.isNull()))
.thenReturn(List.of(post));
mockMvc.perform(get("/api/posts")) mockMvc.perform(get("/api/posts"))
.andExpect(status().isOk()) .andExpect(status().isOk())