diff --git a/endpoints/basketba.post.py b/endpoints/basketba.post.py new file mode 100644 index 0000000..015811d --- /dev/null +++ b/endpoints/basketba.post.py @@ -0,0 +1,63 @@ +```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] +``` \ No newline at end of file