Update code in endpoints/footballing.post.py
This commit is contained in:
parent
e7c2228a6a
commit
739e071090
@ -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
|
||||
return {
|
||||
"method": "GET",
|
||||
"_verb": "get",
|
||||
"games": 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.
|
Loading…
x
Reference in New Issue
Block a user