-
Notifications
You must be signed in to change notification settings - Fork 0
[feat] 최근 게시물 조회 API 추가 #101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,8 @@ | |
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import java.time.Instant; | ||
| import java.time.temporal.ChronoUnit; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
@@ -73,7 +75,21 @@ public List<CurationPost> getAllCurationPostsWithPaging(PageRequestDto pageReque | |
|
|
||
| @Override | ||
| public List<StoryPost> getAllStoryPostsWithPaging(PageRequestDto pageRequest) { | ||
| return getAllPostsWithPaging(pageRequest, storyPost, storyPost._super); | ||
| JPAQuery<StoryPost> query = queryFactory | ||
| .selectFrom(storyPost) | ||
| .leftJoin(storyPost._super.user, user).fetchJoin() | ||
| .leftJoin(storyPost.primaryComposer).fetchJoin() | ||
| .where( | ||
| storyPost._super.isBlocked.isFalse() | ||
| .and(storyPost._super.postStatus.eq(PostStatus.PUBLISHED)) | ||
| ); | ||
|
|
||
| return pagingUtils.applyCursorPagination( | ||
| query, | ||
| pageRequest, | ||
| storyPost._super.createdAt, | ||
| storyPost._super.id | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -212,6 +228,64 @@ public Map<Long, StoryPostStatsDto> findStoryPostStatsByAllComposers() { | |
| )); | ||
| } | ||
|
|
||
| @Override | ||
| public List<Post> getRecentPostsWithPaging(PageRequestDto pageRequest) { | ||
| Instant sevenDaysAgo = Instant.now().minus(7, ChronoUnit.DAYS); | ||
|
|
||
| List<FreePost> freePosts = queryFactory | ||
| .selectFrom(freePost) | ||
| .leftJoin(freePost._super.user, user).fetchJoin() | ||
| .where( | ||
| freePost._super.isBlocked.isFalse() | ||
| .and(freePost._super.postStatus.eq(PostStatus.PUBLISHED)) | ||
| .and(freePost._super.createdAt.goe(sevenDaysAgo)) | ||
| ) | ||
| .orderBy(freePost._super.createdAt.desc(), freePost._super.id.desc()) | ||
| .fetch(); | ||
|
|
||
| List<StoryPost> storyPosts = queryFactory | ||
| .selectFrom(storyPost) | ||
| .leftJoin(storyPost._super.user, user).fetchJoin() | ||
| .leftJoin(storyPost.primaryComposer).fetchJoin() | ||
| .where( | ||
| storyPost._super.isBlocked.isFalse() | ||
| .and(storyPost._super.postStatus.eq(PostStatus.PUBLISHED)) | ||
| .and(storyPost._super.createdAt.goe(sevenDaysAgo)) | ||
| ) | ||
| .orderBy(storyPost._super.createdAt.desc(), storyPost._super.id.desc()) | ||
| .fetch(); | ||
|
|
||
| List<CurationPost> curationPosts = queryFactory | ||
| .selectFrom(curationPost) | ||
| .leftJoin(curationPost._super.user, user).fetchJoin() | ||
| .leftJoin(curationPost.primaryComposer).fetchJoin() | ||
| .where( | ||
| curationPost._super.isBlocked.isFalse() | ||
| .and(curationPost._super.postStatus.eq(PostStatus.PUBLISHED)) | ||
| .and(curationPost._super.createdAt.goe(sevenDaysAgo)) | ||
| ) | ||
| .orderBy(curationPost._super.createdAt.desc(), curationPost._super.id.desc()) | ||
| .fetch(); | ||
|
|
||
| List<Post> allPosts = new ArrayList<>(); | ||
| allPosts.addAll(freePosts); | ||
| allPosts.addAll(storyPosts); | ||
| allPosts.addAll(curationPosts); | ||
|
|
||
| allPosts.sort((p1, p2) -> { | ||
| int dateCompare = p2.getCreatedAt().compareTo(p1.getCreatedAt()); | ||
| if (dateCompare != 0) return dateCompare; | ||
| return Long.compare(p2.getId(), p1.getId()); | ||
| }); | ||
|
|
||
| return pagingUtils.applyCursorPaginationToList( | ||
| allPosts, | ||
| pageRequest, | ||
| Post::getCreatedAt, | ||
| Post::getId | ||
| ); | ||
| } | ||
|
Comment on lines
+232
to
+287
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The To remediate this, it is strongly recommended to perform pagination at the database level. A more scalable approach involves:
Additionally, the |
||
|
|
||
| private <T extends Post> List<T> getAllPostsWithPaging( | ||
| PageRequestDto pageRequest, | ||
| EntityPathBase<T> qEntity, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 정렬 로직은
getPostsByComposerIdWithPaging메서드(189-195행)에서도 동일하게 사용되고 있습니다. 코드 중복을 피하고 가독성을 높이기 위해Comparator를 사용하는 것이 좋습니다.Comparator.comparing()과thenComparing()을 연결하여 사용하면 더 선언적이고 읽기 좋은 코드를 작성할 수 있습니다.