59 lines
860 B
Python
59 lines
860 B
Python
from fastapi import APIRouter
|
|
from fastapi import Depends
|
|
from fastapi import HTTPException
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
import schemas
|
|
from services import crud
|
|
|
|
from app.db.session import SessionLocal
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
def get_db():
|
|
|
|
db = SessionLocal()
|
|
|
|
try:
|
|
yield db
|
|
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
# GET ITEMS
|
|
@router.get(
|
|
"/items",
|
|
response_model=list[schemas.ItemResponse]
|
|
)
|
|
def get_items(
|
|
db: Session = Depends(get_db)
|
|
):
|
|
|
|
return crud.get_items(db)
|
|
|
|
|
|
# SPIN
|
|
@router.post("/spin")
|
|
def spin(
|
|
db: Session = Depends(get_db)
|
|
):
|
|
|
|
winner = crud.spin_wheel(db)
|
|
|
|
if not winner:
|
|
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="No items available"
|
|
)
|
|
|
|
return {
|
|
"winner": {
|
|
"id": winner.id,
|
|
"title": winner.title
|
|
}
|
|
} |