34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
```python
|
|
from fastapi import APIRouter, HTTPException
|
|
import uuid
|
|
|
|
games = [] # In-memory storage
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/games")
|
|
async def save_games(
|
|
games_list: list[str]
|
|
):
|
|
"""Save a list of games to the database"""
|
|
if request.method != "POST":
|
|
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
|
|
|
for game in games_list:
|
|
game_id = str(uuid.uuid4())
|
|
games.append({
|
|
"id": game_id,
|
|
"name": game
|
|
})
|
|
|
|
return {
|
|
"message": "Games saved successfully",
|
|
"games_saved": len(games_list),
|
|
"method": "POST",
|
|
"_verb": "post"
|
|
}
|
|
```
|
|
|
|
This endpoint accepts a list of game names as input, generates a unique ID for each game, and saves them to an in-memory list called `games`. It returns a JSON response with a success message, the number of games saved, and the HTTP method metadata as per the requirements.
|
|
|
|
Note: This implementation uses an in-memory list for demonstration purposes. In a real-world scenario, you would likely use a database or other persistent storage mechanism. |