diff --git a/endpoints/basketb.post.py b/endpoints/basketb.post.py index 9448596..a983358 100644 --- a/endpoints/basketb.post.py +++ b/endpoints/basketb.post.py @@ -1,54 +1,50 @@ +from typing import Optional +from pydantic import BaseModel, Field from fastapi import APIRouter, HTTPException -import uuid - -games = [] # In-memory storage +from uuid import uuid4 router = APIRouter() +# In-memory storage +games = [] + +class GameModel(GameBase): + id: str = Field(default_factory=lambda: str(uuid4())) + + class Config: + orm_mode = True + @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""" +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_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 - }) + raise HTTPException(status_code=405, detail={"message": "Method Not Allowed", "method": "POST", "_verb": "post"}) + games.append(game.dict()) return { "message": "Game saved successfully", - "game_id": game_id, + "game": game, "method": "POST", "_verb": "post" } @router.get("/basketb") async def get_games(): - """Fetch all basketball games from the database""" + """Fetch all games from the database""" return { - "games": games, + "games": [GameModel(**game) for game in games], "method": "GET", "_verb": "get" } ``` -This code defines two routes: +This code provides: -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. +1. A `GameBase` Pydantic model to define the base schema for games. +2. A `GameModel` Pydantic model that inherits from `GameBase` and adds an `id` field. +3. A `@router.post("/basketb")` endpoint to save a new game to the in-memory `games` list. +4. A `@router.get("/basketb")` endpoint to fetch all games from the in-memory `games` list. +5. Proper method validation and error handling for the POST endpoint. +6. The required response format with `method` and `_verb` fields. -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 +Note: This implementation uses an in-memory list for data storage. In a real application, you would likely use a database or other persistent storage solution. \ No newline at end of file