From f1c260290fc6d4acfbd4ead70c78f057d1326b4a Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Tue, 25 Mar 2025 08:20:11 +0100 Subject: [PATCH] Update code in endpoints/footbal.post.py --- repos/games-xxwif7/endpoints/footbal.post.py | 57 ++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 repos/games-xxwif7/endpoints/footbal.post.py diff --git a/repos/games-xxwif7/endpoints/footbal.post.py b/repos/games-xxwif7/endpoints/footbal.post.py new file mode 100644 index 0000000..bb69b9d --- /dev/null +++ b/repos/games-xxwif7/endpoints/footbal.post.py @@ -0,0 +1,57 @@ +```python +from typing import List +from pydantic import BaseModel, Field +from sqlalchemy import Column, Integer, String, Boolean +from sqlalchemy.ext.declarative import declarative_base + +Base = declarative_base() + + + + class Config: + orm_mode = True + + + + id = Column(Integer, primary_key=True, index=True) + name = Column(String, index=True) + description = Column(String) + is_multiplayer = Column(Boolean, default=False) + +games = [] # In-memory storage + +from fastapi import APIRouter, HTTPException + +router = APIRouter() + +@router.post("/football", status_code=201) +async def create_game(game: GameSchema): + """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, is_multiplayer=game.is_multiplayer) + db.add(db_game) + db.commit() + db.refresh(db_game) + + return { + "method": "POST", + "_verb": "post", + "id": db_game.id, + **game.dict() + } + +@router.get("/football", response_model=List[GameSchema]) +async def get_games(): + """Get all games""" + if request.method != "GET": + raise HTTPException(status_code=405, detail="Method Not Allowed") + + games = db.query(Game).all() + return games +``` + +This code provides a `GameSchema` Pydantic model and a `Game` SQLAlchemy model for storing game data. The `create_game` endpoint allows creating a new game by sending a POST request to `/football` with the game details in the request body. The `get_games` endpoint retrieves all games from the database by sending a GET request to `/football`. + +Note that this code assumes the existence of a SQLAlchemy database session `db`. You'll need to set up the database connection and session before using these endpoints. \ No newline at end of file