Update code in endpoints/sportres.post.py
This commit is contained in:
parent
bf2dd4ec75
commit
734d431380
41
endpoints/sportres.post.py
Normal file
41
endpoints/sportres.post.py
Normal file
@ -0,0 +1,41 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
import uuid
|
||||
|
||||
games = [] # In-memory storage
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
# Game model
|
||||
|
||||
@router.post("/sportres")
|
||||
async def save_game(game: GameModel):
|
||||
"""Save a new game to the database"""
|
||||
if request.method != "POST":
|
||||
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
||||
|
||||
game_dict = game.dict()
|
||||
game_dict["id"] = str(uuid.uuid4())
|
||||
games.append(game_dict)
|
||||
|
||||
return {
|
||||
"message": "Game saved successfully",
|
||||
"game": game_dict,
|
||||
"method": "POST",
|
||||
"_verb": "post"
|
||||
}
|
||||
|
||||
@router.get("/games")
|
||||
async def get_games():
|
||||
"""Fetch all games from the database"""
|
||||
return {
|
||||
"games": games,
|
||||
"method": "GET",
|
||||
"_verb": "get"
|
||||
}
|
||||
```
|
||||
|
||||
This code provides an endpoint `/sportres` that accepts POST requests to save a new game to an in-memory list called `games`. The `GameModel` class defines the schema for the game data. The `save_game` function validates that the request method is POST, creates a new UUID for the game, converts the `GameModel` instance to a dictionary, and appends it to the `games` list.
|
||||
|
||||
The `/games` endpoint handles GET requests and returns the list of all games stored in the `games` list.
|
||||
|
||||
Note that this is a basic example using an in-memory list for data storage. In a real-world application, you would typically use a database or other persistent storage mechanism.
|
Loading…
x
Reference in New Issue
Block a user