Skip to content

Commit

Permalink
Merge pull request #29 from wafflestudio/pagnation
Browse files Browse the repository at this point in the history
페이지네이션 구현
  • Loading branch information
morecleverer authored Jan 17, 2025
2 parents bea07ca + 6c6d6c8 commit 9eb7c0b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
2 changes: 1 addition & 1 deletion snuvote/app/vote/dto/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def from_vote_user(vote: Vote, user: User) -> "VotesListInfoResponse":
class OnGoingVotesListResponse(BaseModel):
votes_list: List[VotesListInfoResponse]
has_next: bool
next_cursor: str|None = None
next_cursor: datetime|None = None

class ChoiceDetailResponse(BaseModel):
choice_id: int
Expand Down
4 changes: 2 additions & 2 deletions snuvote/app/vote/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def add_vote(self,
choices=choices)

# 진행 중인 투표 리스트 조회
def get_ongoing_list(self) -> List[Vote]:
return self.vote_store.get_ongoing_list()
def get_ongoing_list(self, start_cursor: datetime|None) -> tuple[List[Vote], bool, datetime|None]:
return self.vote_store.get_ongoing_list(start_cursor)

# 투표글 상세 내용 조회
def get_vote_by_vote_id(self, vote_id: int) -> Vote:
Expand Down
26 changes: 24 additions & 2 deletions snuvote/app/vote/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,30 @@ def add_vote(self,
return vote

# 진행 중인 투표 리스트 조회
def get_ongoing_list(self) -> List[Vote]:
return self.session.execute(select(Vote).where(Vote.end_datetime > datetime.now(timezone.utc))).scalars().all()
def get_ongoing_list(self, start_cursor: datetime|None) -> tuple[List[Vote], bool, datetime|None]:

#커서가 none이면 가장 최신 것부터 40개
if start_cursor is None:
start_cursor = datetime.now(timezone.utc)

#생성 시간이 커서보다 최신인 것부터 오름차순(최신순)으로 40개 리턴
query = (
select(Vote)
.where(Vote.create_datetime < start_cursor)
.where(Vote.end_datetime > datetime.now(timezone.utc))
.order_by(Vote.create_datetime.desc())
.limit(40)
)

results = self.session.execute(query).scalars().all()

#만약 40개를 꽉 채웠다면 추가 내용이 있을 가능성 있음
has_next = len(results) == 40

#다음 커서는 40개 중 가장 과거에 생성된 것
next_cursor = results[-1].create_datetime if has_next else None

return results, has_next, next_cursor

# 투표글 상세 내용 조회
def get_vote_by_vote_id(self, vote_id: int) -> Vote:
Expand Down
12 changes: 7 additions & 5 deletions snuvote/app/vote/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from snuvote.app.vote.dto.requests import CreateVoteRequest, ParticipateVoteRequest, CommentRequest
from snuvote.app.vote.dto.responses import OnGoingVotesListResponse, VotesListInfoResponse, VoteDetailResponse, ChoiceDetailResponse, CommentDetailResponse
from snuvote.app.vote.errors import VoteNotFoundError, MultipleChoicesError, ChoiceNotFoundError, CommentNotFoundError
from datetime import datetime, timedelta, timezone

from snuvote.database.models import User
from snuvote.app.vote.service import VoteService
Expand Down Expand Up @@ -45,13 +46,14 @@ def create_vote(
@vote_router.get("/ongoing_list", status_code=HTTP_200_OK)
def get_ongoing_list(
user: Annotated[User, Depends(login_with_access_token)],
vote_service: Annotated[VoteService, Depends()]
vote_service: Annotated[VoteService, Depends()],
start_cursor: datetime|None = None
):
votes = vote_service.get_ongoing_list()
votes, has_next, next_cursor = vote_service.get_ongoing_list(start_cursor)
return OnGoingVotesListResponse(
votes_list = list(reversed([ VotesListInfoResponse.from_vote_user(vote, user) for vote in votes])),
has_next = True,
next_cursor = 'next_cursor'
votes_list = list([ VotesListInfoResponse.from_vote_user(vote, user) for vote in votes]),
has_next = has_next,
next_cursor = next_cursor
)


Expand Down

0 comments on commit 9eb7c0b

Please sign in to comment.