refactor: restructure project into layered architecture
This commit is contained in:
59
backend/api/user.py
Normal file
59
backend/api/user.py
Normal file
@@ -0,0 +1,59 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user