69 lines
1.4 KiB
Python
69 lines
1.4 KiB
Python
```python
|
|
from typing import List, Optional
|
|
from pydantic import BaseModel, Field
|
|
from datetime import datetime
|
|
|
|
# Models
|
|
|
|
|
|
class GameCreate(GameBase):
|
|
pass
|
|
|
|
class GameUpdate(GameBase):
|
|
pass
|
|
|
|
class GameInDBBase(GameBase):
|
|
id: int
|
|
created_at: datetime
|
|
updated_at: datetime
|
|
|
|
class Config:
|
|
orm_mode = True
|
|
|
|
class Game(GameInDBBase):
|
|
pass
|
|
|
|
class GameInDB(GameInDBBase):
|
|
pass
|
|
|
|
# In-memory storage
|
|
games = []
|
|
|
|
from fastapi import APIRouter, HTTPException
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/football", response_model=Game)
|
|
async def create_game(game: GameCreate):
|
|
"""Save a new game to the database"""
|
|
if request.method != "POST":
|
|
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
|
|
|
new_game = Game(
|
|
name=game.name,
|
|
description=game.description,
|
|
release_date=game.release_date,
|
|
platform=game.platform,
|
|
created_at=datetime.now(),
|
|
updated_at=datetime.now()
|
|
)
|
|
games.append(new_game)
|
|
|
|
response = {
|
|
"message": "Game created successfully",
|
|
"game": new_game,
|
|
"method": "POST",
|
|
"_verb": "post"
|
|
}
|
|
return response
|
|
|
|
@router.get("/football", response_model=List[Game])
|
|
async def get_games():
|
|
"""Fetch all games from the database"""
|
|
response = {
|
|
"games": games,
|
|
"method": "GET",
|
|
"_verb": "get"
|
|
}
|
|
return response
|
|
``` |