Update code in endpoints/foot.post.py
This commit is contained in:
parent
1d958e8b2a
commit
00157a4736
51
endpoints/foot.post.py
Normal file
51
endpoints/foot.post.py
Normal file
@ -0,0 +1,51 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
import uuid
|
||||
|
||||
games = [] # In-memory storage
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.post("/games")
|
||||
async def create_game(game: Game):
|
||||
"""Save a new game to the database"""
|
||||
if request.method != "POST":
|
||||
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
||||
|
||||
game_model = game.dict()
|
||||
game_model["id"] = str(uuid.uuid4())
|
||||
games.append(game_model)
|
||||
|
||||
return {
|
||||
"method": "POST",
|
||||
"_verb": "post",
|
||||
"message": "Game created successfully",
|
||||
"game": game_model
|
||||
}
|
||||
|
||||
@router.get("/games")
|
||||
async def get_games():
|
||||
"""Fetch all games from the database"""
|
||||
return {
|
||||
"method": "GET",
|
||||
"_verb": "get",
|
||||
"games": games
|
||||
}
|
||||
```
|
||||
|
||||
This code provides the following:
|
||||
|
||||
1. A `Game` Pydantic model to define the schema for game data.
|
||||
2. A `create_game` endpoint to save a new game to the in-memory `games` list.
|
||||
3. A `get_games` endpoint to retrieve all games from the in-memory `games` list.
|
||||
|
||||
The `create_game` endpoint:
|
||||
- Validates that the request method is POST, raising a 405 error otherwise.
|
||||
- Converts the `game` Pydantic model to a dictionary.
|
||||
- Generates a new UUID for the `id` field.
|
||||
- Appends the game dictionary to the `games` list.
|
||||
- Returns a success response with the created game data.
|
||||
|
||||
The `get_games` endpoint:
|
||||
- Returns all games in the `games` list.
|
||||
|
||||
Note that this is an in-memory implementation using a Python list. In a real application, you would likely use a database to store and retrieve game data.
|
Loading…
x
Reference in New Issue
Block a user