Skip to content

Commit

Permalink
Merge pull request #43 from wafflestudio/vote_early_end
Browse files Browse the repository at this point in the history
투표 조기 종료 코드 추가
  • Loading branch information
morecleverer authored Jan 26, 2025
2 parents d0533c8 + 8f13cba commit 05b709a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
4 changes: 4 additions & 0 deletions snuvote/app/vote/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ class InvalidFileExtensionError(HTTPException):
def __init__(self) -> None:
super().__init__(HTTP_400_BAD_REQUEST, "Invalid file extension")

class VoteNotYoursError(HTTPException):
def __init__(self) -> None:
super().__init__(HTTP_403_FORBIDDEN, "Vote not yours")


class InvalidVoteListCategoryError(HTTPException):
def __init__(self) -> None:
Expand Down
15 changes: 14 additions & 1 deletion snuvote/app/vote/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from fastapi import Depends, UploadFile
from snuvote.database.models import Vote, User, Choice, ChoiceParticipation, Comment
from snuvote.app.vote.store import VoteStore
from snuvote.app.vote.errors import ChoiceNotFoundError, InvalidFieldFormatError, MultipleChoicesError, ParticipationCodeError, ParticipationCodeNotProvidedError, WrongParticipationCodeError, EndedVoteError, CommentNotYoursError, CommentNotInThisVoteError, InvalidFileExtensionError
from snuvote.app.vote.errors import ChoiceNotFoundError, VoteNotYoursError, MultipleChoicesError, ParticipationCodeError, ParticipationCodeNotProvidedError, WrongParticipationCodeError, EndedVoteError, CommentNotYoursError, CommentNotInThisVoteError, InvalidFileExtensionError
from snuvote.app.vote.dto.requests import ParticipateVoteRequest, CommentRequest

from datetime import datetime, timedelta, timezone
Expand Down Expand Up @@ -138,6 +138,19 @@ def participate_vote(self, vote: Vote, user: User, participate_vote_request: Par

return self.vote_store.participate_vote(vote=vote, user_id=user_id, choice_id_list=choice_id_list)

#투표 조기 종료하기
def close_vote(self, vote:Vote, user: User)-> None:

#만약 투표 작성자가 아닐 경우
if vote.writer_id != user.id:
raise VoteNotYoursError()

if vote.end_datetime <= datetime.now(tz=timezone.utc).replace(tzinfo=None):
raise EndedVoteError()

self.vote_store.close_vote(vote_id=vote.id)


def create_comment(self, vote: Vote, user: User, comment_request: CommentRequest) -> None:
self.vote_store.create_comment(vote_id=vote.id, writed_id=user.id, content=comment_request.content)

Expand Down
6 changes: 6 additions & 0 deletions snuvote/app/vote/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,12 @@ def participate_vote(self, vote: Vote, user_id: int, choice_id_list: List[int])
self.session.commit()
return vote

#투표 조기 종료하기
def close_vote(self, vote_id: int) -> None:
vote = self.get_vote_by_vote_id(vote_id)
vote.end_datetime = datetime.now(timezone.utc)
self.session.commit()

def create_comment(self, vote_id: int, writed_id: int, content: str) -> Vote:
comment = Comment(vote_id=vote_id, writer_id=writed_id, content=content,
create_datetime=datetime.now(timezone.utc),
Expand Down
18 changes: 18 additions & 0 deletions snuvote/app/vote/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,24 @@ def paricipate_vote(

return get_vote(vote.id, user, vote_service)

#투표 조기 종료하기
@vote_router.patch("/{vote_id}/close", status_code=HTTP_200_OK)
def close_vote(
vote_id: int,
user: Annotated[User, Depends(login_with_access_token)],
vote_service: Annotated[VoteService, Depends()]
):
vote = vote_service.get_vote_by_vote_id(vote_id = vote_id)

# 해당 vote_id에 해당하는 투표글이 없을 경우 404 Not Found
if not vote:
raise VoteNotFoundError()

vote_service.close_vote(vote, user)

return get_vote(vote.id, user, vote_service)


# 댓글 추가하기
@vote_router.post("/{vote_id}/comment", status_code=HTTP_201_CREATED)
def create_comment(
Expand Down

0 comments on commit 05b709a

Please sign in to comment.