Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions app/comments/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
11 changes: 7 additions & 4 deletions app/grad_2025/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
42 changes: 28 additions & 14 deletions app/posts/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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](
Expand Down Expand Up @@ -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)
30 changes: 16 additions & 14 deletions app/users/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down
Loading