games-xxwif7/endpoints/basketba.post.py
2025-03-25 07:47:33 +01:00

63 lines
1.5 KiB
Python

```python
from typing import List
from pydantic import BaseModel, Field
from sqlalchemy import Column, Integer, String, Boolean
from .database import Base
class GameCreate(GameBase):
pass
class GameOut(GameBase):
id: int
id = Column(Integer, primary_key=True, index=True)
name = Column(String, nullable=False, unique=True)
description = Column(String, nullable=False)
category = Column(String, nullable=False)
is_active = Column(Boolean, default=True)
games = []
from fastapi import APIRouter, HTTPException
router = APIRouter()
@router.post("/basketba", status_code=201)
async def create_game(game: GameCreate):
"""Create a new game"""
if request.method != "POST":
raise HTTPException(status_code=405, detail="Method Not Allowed")
db_game = Game(
name=game.name,
description=game.description,
category=game.category,
is_active=game.is_active
)
db.add(db_game)
db.commit()
db.refresh(db_game)
return {
"method": "POST",
"_verb": "post",
**game.dict(),
"id": db_game.id
}
@router.get("/basketba", response_model=List[GameOut])
async def get_games():
"""Get all active games"""
games = db.query(Game).filter(Game.is_active == True).all()
return [{
"id": game.id,
"name": game.name,
"description": game.description,
"category": game.category,
"is_active": game.is_active
} for game in games]
```