From 7935740b4ac3d19b8a887ee226c6a1d006b6d006 Mon Sep 17 00:00:00 2001 From: sehooh5 Date: Sat, 31 Jan 2026 12:49:46 +0900 Subject: [PATCH] cursor update limit+1 --- app/comments/service.py | 11 ++++++++--- app/grad_2025/service.py | 11 +++++++---- app/posts/service.py | 42 ++++++++++++++++++++++++++-------------- app/users/service.py | 30 ++++++++++++++-------------- 4 files changed, 59 insertions(+), 35 deletions(-) diff --git a/app/comments/service.py b/app/comments/service.py index a1e33260..fb3bb765 100644 --- a/app/comments/service.py +++ b/app/comments/service.py @@ -37,9 +37,16 @@ async def get_paginated_comments( raise HTTPException(status_code=400, detail="부모 댓글을 찾을 수 없습니다.") comments = await self.comment_repository.find_all( - post_id=post_id, parent_id=parent_id, cursor=cursor, limit=limit + post_id=post_id, parent_id=parent_id, cursor=cursor, limit=limit + 1 ) + has_next = len(comments) > limit + if has_next: + comments = comments[:-1] + next_cursor = str(comments[-1].id) + else: + next_cursor = None + comment_read_list = [ CommentRead.from_comment( comment, @@ -56,8 +63,6 @@ async def get_paginated_comments( for comment in comments ] - next_cursor = str(comments[-1].id) if len(comments) == limit else None - return Page[CommentRead]( list=comment_read_list, next_cursor=next_cursor, diff --git a/app/grad_2025/service.py b/app/grad_2025/service.py index 6ec98cd1..9de1169a 100644 --- a/app/grad_2025/service.py +++ b/app/grad_2025/service.py @@ -30,14 +30,17 @@ async def search_posts( cursor_id = cursor.id if cursor else None posts = await self.post_repository.find_grad_2025_posts( cursor=cursor_id, - limit=limit, + limit=limit + 1, categories=categories, univ_majors=univ_majors, ) + has_next = len(posts) > limit + if has_next: + posts = posts[:-1] + next_cursor = Grad2025Cursor(id=posts[-1].id).encode() + else: + next_cursor = None compact_posts = [PostCompactRead.from_post(post) for post in posts] - next_cursor = ( - Grad2025Cursor(id=posts[-1].id).encode() if len(posts) == limit else None - ) return Page[PostCompactRead]( list=compact_posts, next_cursor=next_cursor, diff --git a/app/posts/service.py b/app/posts/service.py index 25426758..a426a9a0 100644 --- a/app/posts/service.py +++ b/app/posts/service.py @@ -66,12 +66,17 @@ async def get_posts( ) -> Page[PostCompactRead]: posts = await self.post_repository.find_all( cursor=cursor, - limit=limit, + limit=limit + 1, search=search, category=category, ) + has_next = len(posts) > limit + if has_next: + posts = posts[:-1] + next_cursor = str(posts[-1].id) + else: + next_cursor = None compact_posts = [PostCompactRead.from_post(post) for post in posts] - next_cursor = str(posts[-1].id) if len(posts) == limit else None return Page[PostCompactRead]( list=compact_posts, next_cursor=next_cursor, @@ -87,14 +92,19 @@ async def get_posts_by_user( ) -> PageWithCount[PostCompactRead]: posts = await self.post_repository.find_all( cursor=cursor, - limit=limit, + limit=limit + 1, user_handle=user_handle, ) total_count = await self.post_repository.count_by_user_handle( user_handle=user_handle ) + has_next = len(posts) > limit + if has_next: + posts = posts[:-1] + next_cursor = str(posts[-1].id) + else: + next_cursor = None compact_posts = [PostCompactRead.from_post(post) for post in posts] - next_cursor = str(posts[-1].id) if len(posts) == limit else None return PageWithCount[PostCompactRead]( list=compact_posts, next_cursor=next_cursor, @@ -116,15 +126,18 @@ async def get_feed( posts = await self.post_repository.feed( preferred_categories=preferred_categories, cursor=cursor, - limit=limit, + limit=limit + 1, seed=seed, ) - next_cursor = None - if len(posts) == limit: + has_next = len(posts) > limit + if has_next: + posts = posts[:-1] last = posts[-1] flag = 0 if last.category in current_user.preferred_categories else 1 next_cursor = FeedCursor(flag=flag, id=last.id, seed=seed).encode() + else: + next_cursor = None compact_posts = [PostCompactRead.from_post(post) for post in posts] return Page[PostCompactRead]( @@ -296,17 +309,18 @@ async def get_post_liked_users( cursor_id = cursor.user_id if cursor else None users = await self.post_like_repository.find_users_by_post_id( - post_id=post_id, cursor=cursor_id, limit=limit + post_id=post_id, cursor=cursor_id, limit=limit + 1 ) + has_next = len(users) > limit + if has_next: + users = users[:-1] + next_cursor = UserListCursor(user_id=users[-1].id).encode() + else: + next_cursor = None + user_reads = [ UserRead.from_user(user, current_user_id=current_user.id) for user in users ] - next_cursor = ( - UserListCursor(user_id=users[-1].id).encode() - if len(users) == limit - else None - ) - return Page[UserRead](list=user_reads, next_cursor=next_cursor) diff --git a/app/users/service.py b/app/users/service.py index aff54571..eff7edc5 100644 --- a/app/users/service.py +++ b/app/users/service.py @@ -241,20 +241,21 @@ async def get_user_followers( cursor_id = cursor.user_id if cursor else None followers = await self.follow_repository.find_followers_by_user_id( - user_id=user_id, cursor=cursor_id, limit=limit + user_id=user_id, cursor=cursor_id, limit=limit + 1 ) + has_next = len(followers) > limit + if has_next: + followers = followers[:-1] + next_cursor = UserListCursor(user_id=followers[-1].id).encode() + else: + next_cursor = None + user_reads = [ UserRead.from_user(user=follower, current_user_id=current_user.id) for follower in followers ] - next_cursor = ( - UserListCursor(user_id=followers[-1].id).encode() - if len(followers) == limit - else None - ) - return Page[UserRead](list=user_reads, next_cursor=next_cursor) async def get_user_followings( @@ -271,20 +272,21 @@ async def get_user_followings( cursor_id = cursor.user_id if cursor else None followings = await self.follow_repository.find_followings_by_user_id( - user_id=user_id, cursor=cursor_id, limit=limit + user_id=user_id, cursor=cursor_id, limit=limit + 1 ) + has_next = len(followings) > limit + if has_next: + followings = followings[:-1] + next_cursor = UserListCursor(user_id=followings[-1].id).encode() + else: + next_cursor = None + user_reads = [ UserRead.from_user(user=following, current_user_id=current_user.id) for following in followings ] - next_cursor = ( - UserListCursor(user_id=followings[-1].id).encode() - if len(followings) == limit - else None - ) - return Page[UserRead](list=user_reads, next_cursor=next_cursor) async def _validate_target_user(