Update code in endpoints/basketb.post.py
This commit is contained in:
parent
5dea9c0ab6
commit
e9c845dc0a
54
endpoints/basketb.post.py
Normal file
54
endpoints/basketb.post.py
Normal file
@ -0,0 +1,54 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
import uuid
|
||||
|
||||
games = [] # In-memory storage
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.post("/basketb")
|
||||
async def save_game(
|
||||
home_team: str,
|
||||
away_team: str,
|
||||
home_score: int,
|
||||
away_score: int
|
||||
):
|
||||
"""Save a basketball 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("/basketb")
|
||||
async def get_games():
|
||||
"""Fetch all basketball games from the database"""
|
||||
return {
|
||||
"games": games,
|
||||
"method": "GET",
|
||||
"_verb": "get"
|
||||
}
|
||||
```
|
||||
|
||||
This code defines two routes:
|
||||
|
||||
1. `@router.post("/basketb")` to save a new basketball game to the in-memory `games` list.
|
||||
2. `@router.get("/basketb")` to retrieve all saved basketball games.
|
||||
|
||||
The `save_game` function expects the home team, away team, home score, and away score as parameters. It generates a unique `game_id` using `uuid.uuid4()`, creates a dictionary representing the game, and appends it to the `games` list. The response includes a success message and the `game_id`.
|
||||
|
||||
The `get_games` function simply returns the `games` list in the response.
|
||||
|
||||
Note that this implementation uses an in-memory list for storing games, which is not suitable for production use. In a real-world scenario, you would need to integrate with a database like PostgreSQL, MySQL, or MongoDB.
|
Loading…
x
Reference in New Issue
Block a user