From e9c845dc0a4ab17fda18933044b1a67369130284 Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Tue, 25 Mar 2025 07:38:46 +0100 Subject: [PATCH] Update code in endpoints/basketb.post.py --- endpoints/basketb.post.py | 54 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 endpoints/basketb.post.py diff --git a/endpoints/basketb.post.py b/endpoints/basketb.post.py new file mode 100644 index 0000000..9448596 --- /dev/null +++ b/endpoints/basketb.post.py @@ -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. \ No newline at end of file