Update code in endpoints/football.post.py
This commit is contained in:
parent
9edb17433d
commit
7c259b0417
69
endpoints/football.post.py
Normal file
69
endpoints/football.post.py
Normal file
@ -0,0 +1,69 @@
|
||||
```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
|
||||
```
|
Loading…
x
Reference in New Issue
Block a user