Update code in endpoints/footi.post.py
This commit is contained in:
parent
00157a4736
commit
3dac795d8f
59
endpoints/footi.post.py
Normal file
59
endpoints/footi.post.py
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
from fastapi import APIRouter, HTTPException
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
games = [] # In-memory storage
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.post("/footi")
|
||||||
|
async def save_game(
|
||||||
|
home_team: str,
|
||||||
|
away_team: str,
|
||||||
|
home_score: int,
|
||||||
|
away_score: int
|
||||||
|
):
|
||||||
|
"""Save a new game to the database"""
|
||||||
|
if request.method != "POST":
|
||||||
|
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
||||||
|
|
||||||
|
game_id = str(uuid.uuid4())
|
||||||
|
games.append({
|
||||||
|
"id": game_id,
|
||||||
|
"home_team": home_team,
|
||||||
|
"away_team": away_team,
|
||||||
|
"home_score": home_score,
|
||||||
|
"away_score": away_score
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
"message": "Game saved successfully",
|
||||||
|
"game_id": game_id,
|
||||||
|
"method": "POST",
|
||||||
|
"_verb": "post"
|
||||||
|
}
|
||||||
|
|
||||||
|
@router.get("/footi")
|
||||||
|
async def get_games():
|
||||||
|
"""Fetch all saved games from the database"""
|
||||||
|
if request.method != "GET":
|
||||||
|
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
||||||
|
|
||||||
|
return {
|
||||||
|
"games": games,
|
||||||
|
"method": "GET",
|
||||||
|
"_verb": "get"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
This code provides two endpoints:
|
||||||
|
|
||||||
|
1. `@router.post("/footi")` to save a new game to the in-memory `games` list.
|
||||||
|
2. `@router.get("/footi")` to fetch all saved games from the `games` list.
|
||||||
|
|
||||||
|
The `save_game` function takes the home team, away team, home score, and away score as parameters, generates a unique `game_id`, and appends a dictionary representing the game to the `games` list.
|
||||||
|
|
||||||
|
The `get_games` function simply returns the `games` list.
|
||||||
|
|
||||||
|
Both endpoints include method validation and return the expected response format with the method and `_verb` metadata.
|
||||||
|
|
||||||
|
Note: This is an example using an in-memory list for data storage. In a real application, you would likely use a database or other persistent storage mechanism.
|
Loading…
x
Reference in New Issue
Block a user