Update code in endpoints/footballing.post.py

This commit is contained in:
Backend IM Bot 2025-03-25 08:48:51 +01:00
parent 739e071090
commit 6201008c0e

View File

@ -1,5 +1,5 @@
```python
from typing import Optional
from typing import List
from pydantic import BaseModel, Field
from datetime import datetime
from uuid import uuid4
@ -10,45 +10,58 @@ class GameCreate(GameBase):
pass
class GameUpdate(GameBase):
id: str = Field(default_factory=lambda: str(uuid4()))
pass
class GameInDB(GameBase):
id: str = Field(default_factory=lambda: str(uuid4()))
created_at: datetime = Field(default_factory=datetime.utcnow)
updated_at: Optional[datetime] = None
class GameInDBBase(GameBase):
id: str = Field(..., description="Unique identifier for the game")
created_at: datetime = Field(..., description="Timestamp of when the game was created")
updated_at: datetime = Field(..., description="Timestamp of when the game was last updated")
games = []
class Config:
orm_mode = True
from fastapi import APIRouter, HTTPException, Depends
class Game(GameInDBBase):
pass
class GameInDB(GameInDBBase):
pass
games = [] # In-memory storage
from fastapi import APIRouter, HTTPException
router = APIRouter()
@router.post("/footballing", response_model=GameInDB)
@router.post("/footballing", 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")
game_data = game.dict()
game_data["id"] = str(uuid4())
game_data["created_at"] = datetime.utcnow()
games.append(game_data)
new_game = Game(
id=str(uuid4()),
name=game.name,
description=game.description,
genre=game.genre,
release_date=game.release_date,
created_at=datetime.now(),
updated_at=datetime.now()
)
games.append(new_game)
return {
"method": "POST",
"_verb": "post",
**game_data
"message": "Game created successfully",
"game": new_game
}
@router.get("/footballing", response_model=list[GameInDB])
@router.get("/footballing", response_model=List[GameInDB])
async def get_games():
"""Get all games"""
if request.method != "GET":
raise HTTPException(status_code=405, detail="Method Not Allowed")
return {
"method": "GET",
"_verb": "get",
"games": games
}
return games
```