Skip to content
Open
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
1 change: 1 addition & 0 deletions app/grad_2025/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def from_grad_2025(cls, grad_2025: Grad2025):

class Grad2025Cursor(APISchema):
id: int
seed: str | None = None

def encode(self) -> str:
payload = self.model_dump_json().encode()
Expand Down
10 changes: 7 additions & 3 deletions app/grad_2025/service.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import uuid

from app.category.enums import Category
from app.common.schemas import Page
from app.posts.repository import PostRepository
Expand Down Expand Up @@ -28,17 +30,19 @@ async def search_posts(
categories: list[Category] | None = None,
univ_majors: list[str] | None = None,
) -> Page[PostCompactRead]:
cursor_id = cursor.id if cursor else None
seed = cursor.seed if cursor and cursor.seed else str(uuid.uuid4())

posts = await self.post_repository.find_grad_2025_posts(
cursor=cursor_id,
seed=seed,
cursor=cursor,
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()
next_cursor = Grad2025Cursor(id=posts[-1].id, seed=seed).encode()
else:
next_cursor = None
compact_posts = [PostCompactRead.from_post(post) for post in posts]
Expand Down
21 changes: 12 additions & 9 deletions app/posts/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from app.common.schemas import FeedCursor
from app.database.deps import SessionDep
from app.grad_2025.models import Grad2025
from app.grad_2025.schemas import Grad2025Cursor
from app.users.models import User
from app.utils.dependency import dependency

Expand Down Expand Up @@ -296,12 +297,17 @@ async def find_univ_major_by_post_id(self, *, post_id: int) -> Grad2025 | None:
async def find_grad_2025_posts(
self,
*,
cursor: int | None,
seed: str,
cursor: Grad2025Cursor | None,
limit: int,
categories: list[Category] | None = None,
univ_majors: list[str] | None = None,
) -> list[Post]:
"""post_grad_2025_table에 존재하는 Post만 조회합니다."""
"""post_grad_2025_table에 존재하는 Post만 조회합니다. 시드 기반 랜덤 정렬."""
random_order = func.md5(func.concat(seed, Post.id.cast(String))).label(
"random_order"
)

stmt = (
select(Post)
.options(
Expand All @@ -310,19 +316,16 @@ async def find_grad_2025_posts(
)
.join(post_grad_2025_table, Post.id == post_grad_2025_table.c.post_id)
.where(Post.deleted_at.is_(None))
.order_by(Post.created_at.desc(), Post.id.desc())
.order_by(random_order, Post.id.desc())
.limit(limit)
)

if cursor is not None:
# 커서 id의 created_at을 가져와서 그 이전 것들만 조회
cursor_created_at = (
select(Post.created_at).where(Post.id == cursor).scalar_subquery()
)
current_random = func.md5(func.concat(seed, str(cursor.id)))
stmt = stmt.where(
or_(
Post.created_at < cursor_created_at,
and_(Post.created_at == cursor_created_at, Post.id < cursor),
random_order > current_random,
and_(random_order == current_random, Post.id < cursor.id),
)
)

Expand Down
Loading