Skip to content

Commit

Permalink
Merge pull request #34 from wafflestudio/get_list_ended
Browse files Browse the repository at this point in the history
get_ended_list 기능 추가
  • Loading branch information
morecleverer authored Jan 19, 2025
2 parents ddbdf11 + 170bba0 commit f9c1f97
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
7 changes: 6 additions & 1 deletion snuvote/app/vote/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ def __init__(self) -> None:

class InvalidFileExtensionError(HTTPException):
def __init__(self) -> None:
super().__init__(HTTP_400_BAD_REQUEST, "Invalid file extension")
super().__init__(HTTP_400_BAD_REQUEST, "Invalid file extension")


class InvalidVoteListCategoryError(HTTPException):
def __init__(self) -> None:
super().__init__(HTTP_400_BAD_REQUEST, "Invalid vote list category")
4 changes: 4 additions & 0 deletions snuvote/app/vote/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ def add_vote(self,
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_ended_votes_list(self, start_cursor: datetime|None) -> tuple[List[Vote], bool, datetime|None]:
return self.vote_store.get_ended_votes_list(start_cursor)

# 투표글 상세 내용 조회
def get_vote_by_vote_id(self, vote_id: int) -> Vote:
return self.vote_store.get_vote_by_vote_id(vote_id=vote_id)
Expand Down
45 changes: 38 additions & 7 deletions snuvote/app/vote/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
class VoteStore:
def __init__(self, session: Annotated[Session, Depends(get_db_session)]) -> None:
self.session = session
self.pagination_size = 10


#투표 추가하기
Expand Down Expand Up @@ -66,29 +67,59 @@ def add_vote_image(self, vote_id: int, image_order: int, image_src: str):
# 진행 중인 투표 리스트 조회
def get_ongoing_list(self, start_cursor: datetime|None) -> tuple[List[Vote], bool, datetime|None]:

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

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

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

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

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

return results, has_next, next_cursor

# 완료된 투표글 리스트 조회
def get_ended_votes_list(self, start_cursor: datetime|None) -> tuple[List[Vote], bool, datetime|None]:

# 커서가 none이면 가장 최근에 끝난 투표부터 최근에 끝난 순으로 self.pagination_size개
if start_cursor is None:
query = (
select(Vote)
.where(Vote.end_datetime <= datetime.now(timezone.utc))
.order_by(Vote.end_datetime.desc())
.limit(self.pagination_size)
)
else: # 커서가 None이 아니면 커서보다 과거에 끝난 투표부터 최근에 끝난 순으로 self.pagination_size개
query = (
select(Vote)
.where(Vote.end_datetime <= datetime.now(timezone.utc))
.where(Vote.end_datetime < start_cursor)
.order_by(Vote.end_datetime.desc())
.limit(self.pagination_size)
)

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

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

# 다음 커서는 self.pagination_size개 중 가장 과거에 완료된 것
next_cursor = results[-1].end_datetime if has_next else None

return results, has_next, next_cursor

# 투표글 상세 내용 조회
def get_vote_by_vote_id(self, vote_id: int) -> Vote:
return self.session.scalar(select(Vote).where(Vote.id == vote_id))
Expand Down Expand Up @@ -138,4 +169,4 @@ def delete_comment_by_comment_id(self, comment_id: int) -> None:
# is_deleted = True로 바꾸고, deleted_datetime을 기록
comment.is_deleted = True
comment.deleted_datetime = datetime.now(timezone.utc)
self.session.commit()
self.session.commit()
20 changes: 18 additions & 2 deletions snuvote/app/vote/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,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, ChoiceNotFoundError, CommentNotFoundError
from snuvote.app.vote.errors import VoteNotFoundError, ChoiceNotFoundError, CommentNotFoundError, InvalidVoteListCategoryError
from datetime import datetime, timedelta, timezone

from snuvote.database.models import User
Expand Down Expand Up @@ -57,12 +57,28 @@ def get_ongoing_list(
):
votes, has_next, next_cursor = vote_service.get_ongoing_list(start_cursor)
return OnGoingVotesListResponse(
votes_list = list([ VotesListInfoResponse.from_vote_user(vote, user) for vote in votes]),
votes_list = [ VotesListInfoResponse.from_vote_user(vote, user) for vote in votes ],
has_next = has_next,
next_cursor = next_cursor
)

# 완료된 투표글 조회
@vote_router.get("/list", status_code=HTTP_200_OK)
def get_votes_list(
user: Annotated[User, Depends(login_with_access_token)],
vote_service: Annotated[VoteService, Depends()],
category: str,
start_cursor: datetime|None = None
):
if category == "ended":
votes, has_next, next_cursor = vote_service.get_ended_votes_list(start_cursor)
else: raise InvalidVoteListCategoryError()

return OnGoingVotesListResponse(
votes_list = [ VotesListInfoResponse.from_vote_user(vote, user) for vote in votes ],
has_next = has_next,
next_cursor = next_cursor
)

# 특정 투표글 정보 조회
@vote_router.get("/{vote_id}", status_code=HTTP_200_OK)
Expand Down

0 comments on commit f9c1f97

Please sign in to comment.