Update code in endpoints/footballing.post.py

This commit is contained in:
Backend IM Bot 2025-03-25 08:46:50 +01:00
parent e7c2228a6a
commit 739e071090

View File

@ -1,54 +1,54 @@
```python ```python
from typing import List from typing import Optional
from pydantic import BaseModel, Field from pydantic import BaseModel, Field
from fastapi import APIRouter, HTTPException from datetime import datetime
from uuid import uuid4 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() router = APIRouter()
# Models @router.post("/footballing", response_model=GameInDB)
async def create_game(game: GameCreate):
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):
"""Create a new game""" """Create a new game"""
if request.method != "POST": if request.method != "POST":
raise HTTPException(status_code=405, detail="Method Not Allowed") raise HTTPException(status_code=405, detail="Method Not Allowed")
new_game = GameModel(**game.dict()) game_data = game.dict()
games.append(new_game) game_data["id"] = str(uuid4())
game_data["created_at"] = datetime.utcnow()
games.append(game_data)
return { return {
"method": "POST", "method": "POST",
"_verb": "post", "_verb": "post",
"message": "Game created successfully", **game_data
"game": new_game
} }
@router.get("/footballing", response_model=List[GameModel]) @router.get("/footballing", response_model=list[GameInDB])
async def get_games(): async def get_games():
"""Get a list of all games""" """Get all games"""
if request.method != "GET": if request.method != "GET":
raise HTTPException(status_code=405, detail="Method Not Allowed") raise HTTPException(status_code=405, detail="Method Not Allowed")
return games return {
``` "method": "GET",
"_verb": "get",
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. "games": games
}
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.