49 lines
1.5 KiB
Python
49 lines
1.5 KiB
Python
from fastapi import APIRouter, HTTPException
|
|
import uuid
|
|
|
|
games = [] # In-memory storage
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/footing")
|
|
async def save_game(
|
|
name: str,
|
|
description: str,
|
|
genre: str,
|
|
platform: str
|
|
):
|
|
"""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())
|
|
game = {
|
|
"id": game_id,
|
|
"name": name,
|
|
"description": description,
|
|
"genre": genre,
|
|
"platform": platform
|
|
}
|
|
games.append(game)
|
|
|
|
return {
|
|
"method": "POST",
|
|
"_verb": "post",
|
|
"message": "Game saved successfully",
|
|
"game_id": game_id
|
|
}
|
|
|
|
@router.get("/games")
|
|
async def get_games():
|
|
"""Fetch all saved games"""
|
|
if request.method != "GET":
|
|
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
|
|
|
return {
|
|
"method": "GET",
|
|
"_verb": "get",
|
|
"games": games
|
|
}
|
|
```
|
|
|
|
This code provides an endpoint `/footing` to save a new game to an in-memory list called `games`. It also includes a `GET` endpoint `/games` to fetch all saved games. The `save_game` function checks if the request method is `POST` and raises a `405 Method Not Allowed` error otherwise. It creates a new `game` dictionary with the provided parameters and a unique `game_id`, appends it to the `games` list, and returns a success message with the `game_id`. The `get_games` function checks if the request method is `GET` and returns the list of `games`. |