From 739e0710907caa25295a20ab53b6bee64a550917 Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Tue, 25 Mar 2025 08:46:50 +0100 Subject: [PATCH] Update code in endpoints/footballing.post.py --- .../endpoints/footballing.post.py | 68 +++++++++---------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/repos/kerzan-pyt4gj/endpoints/footballing.post.py b/repos/kerzan-pyt4gj/endpoints/footballing.post.py index b1b7f57..cbc4de5 100644 --- a/repos/kerzan-pyt4gj/endpoints/footballing.post.py +++ b/repos/kerzan-pyt4gj/endpoints/footballing.post.py @@ -1,54 +1,54 @@ ```python -from typing import List +from typing import Optional from pydantic import BaseModel, Field -from fastapi import APIRouter, HTTPException +from datetime import datetime from uuid import uuid4 + + +class GameCreate(GameBase): + pass + +class GameUpdate(GameBase): + id: str = Field(default_factory=lambda: str(uuid4())) + +class GameInDB(GameBase): + id: str = Field(default_factory=lambda: str(uuid4())) + created_at: datetime = Field(default_factory=datetime.utcnow) + updated_at: Optional[datetime] = None + +games = [] + +from fastapi import APIRouter, HTTPException, Depends + router = APIRouter() -# Models - - -class GameBase(BaseModel): - title: str - description: str | None = None - rating: float | None = None - -class GameModel(GameBase): - id: str = Field(default_factory=lambda: str(uuid4())) - -# In-memory storage -games: List[GameModel] = [] - -@router.post("/footballing", status_code=201) -async def create_game(game: GameBase): +@router.post("/footballing", response_model=GameInDB) +async def create_game(game: GameCreate): """Create a new game""" if request.method != "POST": raise HTTPException(status_code=405, detail="Method Not Allowed") - new_game = GameModel(**game.dict()) - games.append(new_game) + game_data = game.dict() + game_data["id"] = str(uuid4()) + game_data["created_at"] = datetime.utcnow() + games.append(game_data) return { "method": "POST", "_verb": "post", - "message": "Game created successfully", - "game": new_game + **game_data } -@router.get("/footballing", response_model=List[GameModel]) +@router.get("/footballing", response_model=list[GameInDB]) async def get_games(): - """Get a list of all games""" + """Get all games""" if request.method != "GET": raise HTTPException(status_code=405, detail="Method Not Allowed") - return games -``` - -This code defines a `GameBase` Pydantic model and a `GameModel` model that inherits from `GameBase` and adds an `id` field. The `games` list stores all the game instances. - -The `create_game` function handles POST requests to the `/footballing` endpoint. It creates a new `GameModel` instance from the request body, appends it to the `games` list, and returns a success response. - -The `get_games` function handles GET requests to the `/footballing` endpoint and returns the list of all games. - -Note that the `get_games` function is a GET endpoint, which violates the strict method adherence rule you provided. If you want to follow that rule, you should remove the `get_games` function. \ No newline at end of file + return { + "method": "GET", + "_verb": "get", + "games": games + } +``` \ No newline at end of file