Skip to content

Commit

Permalink
feat: add qr scan schema, response class
Browse files Browse the repository at this point in the history
  • Loading branch information
ed-kyu committed Nov 21, 2024
1 parent 66455dc commit 7d0275f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
7 changes: 4 additions & 3 deletions app/api/bingo/bingo_boards/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
UpdateBingoCountResponse,
UserSelectedWordsResponse,
UpdateBingoStatusResponse,
UpdateBingoStatusResponseByQRScan,
GetUserBingoEventUser,
)
from .services import (
Expand Down Expand Up @@ -80,10 +81,10 @@ async def get_bingo_event_users(
return await bingo_boards.execute(bingo_count)


@bingo_boards_router.patch("/update/{user_id}/{booth_id}")
@bingo_boards_router.patch("/update/{user_id}/{booth_id}", response_model=UpdateBingoStatusResponseByQRScan)
async def update_bingo_status_booth(
user_id: str,
booth_id: str,
user_id: int = Path(..., title="요청 유저 ID", ge=0),
booth_id: int = Path(..., title="요청 부스 ID", ge=0),
bingo_boards: UpdateBingoStatusByQRScan = Depends(UpdateBingoStatusByQRScan),
):
return await bingo_boards.execute(user_id, booth_id)
8 changes: 7 additions & 1 deletion app/api/bingo/bingo_boards/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,18 @@ class UpdateBingoCountResponse(BaseSchema):
class UserSelectedWordsResponse(BaseSchema):
selected_words: Optional[list[str]] = Field(title="선택한 단어들", default=None)


class UpdateBingoStatusResponse(BaseSchema):
send_user_id: Optional[int] = Field(title="요청 유저 ID", default=None)
receive_user_id: Optional[int] = Field(title="대상 유저 ID", default=None)
updated_words: Optional[list[str]] = Field(title="업데이트된 단어들", default=None)
bingo_count: Optional[int] = Field(title="업데이트된 빙고 갯수", default=None)


class UpdateBingoStatusResponseByQRScan(UpdateBingoCountResponse):
booth_id: Optional[int] = Field(title="요청 부스 ID", default=None)
updated_words: Optional[list[str]] = Field(title="업데이트된 단어들", default=None)


class GetUserBingoEventUser(BaseSchema):
bingo_event_users: Optional[list[BingoEventUserInfo]] = Field(title="빙고 이벤트 당첨 유저 목록", default=None)

10 changes: 5 additions & 5 deletions app/api/bingo/bingo_boards/services.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from core.db import AsyncSessionDepends
from models.bingo import BingoBoards
from api.bingo.bingo_boards.schema import BingoBoardRequest, BingoBoardResponse, UpdateBingoCountResponse, UserSelectedWordsResponse, UpdateBingoStatusResponse, GetUserBingoEventUser
from api.bingo.bingo_boards.schema import BingoBoardRequest, BingoBoardResponse, UpdateBingoCountResponse, UserSelectedWordsResponse, UpdateBingoStatusResponse, GetUserBingoEventUser, UpdateBingoStatusResponseByQRScan


class BaseBingoBoard:
Expand Down Expand Up @@ -70,9 +70,9 @@ async def execute(self, bingo_count: int) -> list[str]:
return GetUserBingoEventUser(ok=False, message=str(e))

class UpdateBingoStatusByQRScan(BaseBingoBoard):
async def execute(self, iser_id: int, booth_id: int) -> BingoBoards:
async def execute(self, user_id: int, booth_id: int) -> BingoBoards:
try:
res = await BingoBoards.update_bingo_status_by_qr_scan(self.async_session, iser_id, booth_id)
return UpdateBingoStatusResponse(**res.__dict__, ok=True, message="빙고판 상태 업데이트에 성공하였습니다.")
res = await BingoBoards.update_bingo_status_by_qr_scan(self.async_session, user_id, booth_id)
return UpdateBingoStatusResponseByQRScan(**res.__dict__, ok=True, message="빙고판 상태 업데이트에 성공하였습니다.")
except ValueError as e:
return UpdateBingoStatusResponse(ok=False, message=str(e))
return UpdateBingoStatusResponseByQRScan(ok=False, message=str(e))
15 changes: 12 additions & 3 deletions app/models/bingo/bingo_boards.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from core.db import AsyncSession
from models.base import Base
from models.bingo.schema import BingoInteractionSchema, BingoEventUserInfo
from models.bingo.schema import BingoInteractionSchema, BingoEventUserInfo, BingoQRScanSchema
from models.user import BingoUser


Expand Down Expand Up @@ -144,9 +144,10 @@ async def update_bingo_status_by_qr_scan(cls, session: AsyncSession, user_id: in
# get board_data, check user_id is already have booth bingo
board = await cls.get_board_by_userid(session, user_id)
board_data = board.board_data
updated_booth_name = f'Booth {booth_id}'
for idx, bingo_dict in board_data.items():
value, status = bingo_dict["value"], bingo_dict["status"]
if value == f'Booth {booth_id}':
if value == updated_booth_name:
booth_exist = True
break
if status == 0:
Expand All @@ -156,6 +157,14 @@ async def update_bingo_status_by_qr_scan(cls, session: AsyncSession, user_id: in
if not booth_exist:
# update random board data
booth_idx = random.choice(not_selected_ids)
board_data[booth_idx]["value"] = f'Booth {booth_id}'
board_data[booth_idx]["value"] = updated_booth_name
board_data[booth_idx]["status"] = 1
await cls.update_board_by_userid(session, user_id, board_data)


return BingoQRScanSchema(
user_id=user_id,
booth_id=booth_id,
updated_words=[updated_booth_name],
bingo_count=board.bingo_count,
)
11 changes: 10 additions & 1 deletion app/models/bingo/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,22 @@ class BingoBoardSchema(BaseModel):

model_config = ConfigDict(from_attributes=True)


class BingoInteractionSchema(BaseModel):
send_user_id: int
receive_user_id: int
updated_words: list[str]
bingo_count: int


class BingoQRScanSchema(BaseModel):
user_id: int
booth_id: int
updated_words: list[str]
bingo_count: int


class BingoEventUserInfo(BaseModel):
rank: int
user_name: str
bingo_count: int
bingo_count: int

0 comments on commit 7d0275f

Please sign in to comment.