Skip to content

Commit

Permalink
Merge pull request #21 from wafflestudio/create_vote-Datetime-형식-수정
Browse files Browse the repository at this point in the history
Create vote datetime 형식 수정
  • Loading branch information
morecleverer authored Jan 7, 2025
2 parents eb513ed + e8c13e4 commit ace3619
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
5 changes: 4 additions & 1 deletion snuvote/app/vote/dto/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Annotated, Callable, TypeVar, List
from pydantic import BaseModel, EmailStr, Field
from pydantic.functional_validators import AfterValidator
from datetime import datetime

from snuvote.app.vote.errors import InvalidFieldFormatError, ChoicesNotProvidedError, ChoiceInvalidFormatError

Expand All @@ -23,10 +24,12 @@ def validate_participation_code(value: str) -> str:
raise InvalidFieldFormatError()
return value

"""
def validate_vote_period(value: int) -> int:
if value < 1 or value > 14:
raise InvalidFieldFormatError()
return value
"""

def validate_choices(value: List[str]) -> List[str]:
if len(value) < 1:
Expand Down Expand Up @@ -62,5 +65,5 @@ class CreateVoteRequest(BaseModel):
realtime_result: bool
multiple_choice: bool
annonymous_choice: bool
vote_period: Annotated[int, AfterValidator(validate_vote_period)]
end_datetime: datetime
choices: Annotated[List[str], AfterValidator(validate_choices)]
17 changes: 14 additions & 3 deletions snuvote/app/vote/dto/responses.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime
from typing import List
from snuvote.database.models import Vote, Choice, ChoiceParticipation
from snuvote.database.models import Vote, User, Choice, ChoiceParticipation
from pydantic import BaseModel


Expand All @@ -11,15 +11,25 @@ class VotesListInfoResponse(BaseModel):
content: str
create_datetime: datetime
end_datetime: datetime
participated: bool

@staticmethod
def from_vote(vote: Vote) -> "VotesListInfoResponse":
def from_vote_user(vote: Vote, user: User) -> "VotesListInfoResponse":

# 해당 유저의 참여 여부도 포함시켜야 함
participated = False
for choice in vote.choices:
for choice_participation in choice.choice_participations:
if choice_participation.user_id == user.id:
participated = True

return VotesListInfoResponse(
id=vote.id,
title=vote.title,
content=vote.content,
create_datetime=vote.create_datetime,
end_datetime=vote.end_datetime
end_datetime=vote.end_datetime,
participated = participated
)

class OnGoingVotesListResponse(BaseModel):
Expand Down Expand Up @@ -49,6 +59,7 @@ def from_choice(choice: Choice, annonymous_choice, realtime_result) -> "ChoiceDe
if not realtime_result:
num_participants = None
participants_name = None


return ChoiceDetailResponse(
choice_id=id,
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 @@ -23,7 +23,7 @@ def add_vote(self,
realtime_result:bool,
multiple_choice:bool,
annonymous_choice:bool,
vote_period:int,
end_datetime:datetime,
choices: List[str]) -> Vote:

#참여코드가 필요한데 참여코드가 없을 경우 400 에러
Expand All @@ -38,7 +38,7 @@ def add_vote(self,
realtime_result=realtime_result,
multiple_choice=multiple_choice,
annonymous_choice=annonymous_choice,
vote_period=vote_period,
end_datetime=end_datetime,
choices=choices)

# 진행 중인 투표 리스트 조회
Expand Down
3 changes: 1 addition & 2 deletions snuvote/app/vote/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ def add_vote(self,
realtime_result:bool,
multiple_choice:bool,
annonymous_choice:bool,
vote_period:int,
end_datetime:datetime,
choices: List[str]) -> Vote:

create_datetime = datetime.now()
end_datetime = create_datetime + timedelta(days=vote_period)


vote = Vote(writer_id=writer_id,
Expand Down
5 changes: 3 additions & 2 deletions snuvote/app/vote/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def create_vote(
realtime_result=create_vote_request.realtime_result,
multiple_choice=create_vote_request.multiple_choice,
annonymous_choice=create_vote_request.annonymous_choice,
vote_period=create_vote_request.vote_period,
end_datetime=create_vote_request.end_datetime,
choices=create_vote_request.choices
)

Expand All @@ -54,11 +54,12 @@ 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()]
):
votes = vote_service.get_ongoing_list()
return OnGoingVotesListResponse(
votes_list = [ VotesListInfoResponse.from_vote(vote) for vote in votes],
votes_list = [ VotesListInfoResponse.from_vote_user(vote, user) for vote in votes],
has_next = True,
next_cursor = 'next_cursor'
)
Expand Down

0 comments on commit ace3619

Please sign in to comment.