Skip to content

Commit

Permalink
QR scan 관련 api (#30)
Browse files Browse the repository at this point in the history
* feat: qr function init

* feat: refactoring

* feat: add qr scan schema, response class

* chore: delete unused thing
  • Loading branch information
ed-kyu authored Nov 22, 2024
1 parent 5c5a1af commit 3f0099b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 5 deletions.
11 changes: 11 additions & 0 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 All @@ -16,6 +17,7 @@
GetUserSelectedWords,
UpdateBingoStatusBySelectedUser,
GetBingoEventUser,
UpdateBingoStatusByQRScan,
)


Expand Down Expand Up @@ -77,3 +79,12 @@ async def get_bingo_event_users(
bingo_boards: GetBingoEventUser = Depends(GetBingoEventUser),
):
return await bingo_boards.execute(bingo_count)


@bingo_boards_router.patch("/update/{user_id}/{booth_id}", response_model=UpdateBingoStatusResponseByQRScan)
async def update_bingo_status_booth(
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)

13 changes: 11 additions & 2 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 BingoBoardResponse, UpdateBingoCountResponse, UserSelectedWordsResponse, UpdateBingoStatusResponse, GetUserBingoEventUser
from api.bingo.bingo_boards.schema import BingoBoardRequest, BingoBoardResponse, UpdateBingoCountResponse, UserSelectedWordsResponse, UpdateBingoStatusResponse, GetUserBingoEventUser, UpdateBingoStatusResponseByQRScan


class BaseBingoBoard:
Expand All @@ -11,6 +11,7 @@ def __init__(self, session: AsyncSessionDepends):
class CreateBingoBoard(BaseBingoBoard):
async def execute(self, user_id: int, board_data: dict) -> BingoBoards:
try:
board_data = BingoBoardRequest.Config.json_schema_extra["example"]["board_data"]
res = await BingoBoards.create(self.async_session, user_id, board_data)
return BingoBoardResponse(**res.__dict__, ok=True, message="빙고판 생성에 성공하였습니다.")
except ValueError as e:
Expand Down Expand Up @@ -66,4 +67,12 @@ async def execute(self, bingo_count: int) -> list[str]:
res = await BingoBoards.get_bingo_event_users(self.async_session, bingo_count)
return GetUserBingoEventUser(bingo_event_users=res, ok=True, message="빙고 이벤트 당첨 유저 목록 생성에 성공하였습니다.")
except ValueError as e:
return GetUserBingoEventUser(ok=False, message=str(e))
return GetUserBingoEventUser(ok=False, message=str(e))

class UpdateBingoStatusByQRScan(BaseBingoBoard):
async def execute(self, user_id: int, booth_id: int) -> BingoBoards:
try:
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 UpdateBingoStatusResponseByQRScan(ok=False, message=str(e))
35 changes: 34 additions & 1 deletion app/models/bingo/bingo_boards.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from datetime import datetime
from zoneinfo import ZoneInfo
import asyncio
import random

from sqlalchemy.orm import mapped_column
from sqlalchemy import Integer, DateTime, JSON, select, desc
from sqlalchemy.ext.mutable import MutableDict

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 @@ -135,3 +136,35 @@ async def get_bingo_event_users(cls, session: AsyncSession, bingo_count: int) ->
]

return bingo_event_users_info

@classmethod
async def update_bingo_status_by_qr_scan(cls, session: AsyncSession, user_id: int, booth_id: int):
booth_exist = False
not_selected_ids = []
# 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 == updated_booth_name:
booth_exist = True
break
if status == 0:
# get not selected list
not_selected_ids.append(idx)

if not booth_exist:
# update random board data
booth_idx = random.choice(not_selected_ids)
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 3f0099b

Please sign in to comment.