From 819e1e02887149f980da951008654ea3791841fd Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Tue, 25 Mar 2025 08:37:55 +0100 Subject: [PATCH] Update code in endpoints/footballing.post.py --- .../endpoints/footballing.post.py | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 repos/beans-aqdspd/endpoints/footballing.post.py diff --git a/repos/beans-aqdspd/endpoints/footballing.post.py b/repos/beans-aqdspd/endpoints/footballing.post.py new file mode 100644 index 0000000..4c734af --- /dev/null +++ b/repos/beans-aqdspd/endpoints/footballing.post.py @@ -0,0 +1,65 @@ +```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, nullable=False) + description = Column(String, nullable=False) + is_multiplayer = Column(Boolean, nullable=False) + +from fastapi import APIRouter, HTTPException +from sqlalchemy.orm import Session + +router = APIRouter() + +@router.post("/footballing", response_model=GameSchema) +async def save_game(game: GameSchema, db: Session): + """Save a new game to the database""" + if request.method != "POST": + raise HTTPException(status_code=405, detail="Method Not Allowed") + + db_game = GameModel( + 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", + **db_game.__dict__ + } + +@router.get("/footballing", response_model=List[GameSchema]) +async def get_games(db: Session): + """Fetch all games from the database""" + games = db.query(GameModel).all() + return [{ + "method": "GET", + "_verb": "get", + **game.__dict__ + } for game in games] +``` + +This code provides: + +1. A `GameSchema` Pydantic model for data validation and response formatting. +2. A `GameModel` SQLAlchemy model for database table definition. +3. A `save_game` endpoint that accepts a `GameSchema` instance, creates a `GameModel` instance, saves it to the database, and returns the saved game data. +4. A `get_games` endpoint that fetches all games from the database and returns them as a list of `GameSchema` instances. + +Note that this code assumes the existence of a SQLAlchemy database session (`db`) and handles method validation and error responses as per the provided requirements. \ No newline at end of file