Update code in endpoints/footballing.post.py
This commit is contained in:
parent
a0242c3008
commit
636e34dbf0
60
repos/kerza-ng8deg/endpoints/footballing.post.py
Normal file
60
repos/kerza-ng8deg/endpoints/footballing.post.py
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
```python
|
||||||
|
from typing import List
|
||||||
|
from pydantic import BaseModel, Field
|
||||||
|
from uuid import uuid4
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
# Database storage
|
||||||
|
games = []
|
||||||
|
|
||||||
|
# Pydantic models
|
||||||
|
|
||||||
|
|
||||||
|
class GameModel(GameBase):
|
||||||
|
id: str = Field(default_factory=lambda: str(uuid4()))
|
||||||
|
created_at: datetime = Field(default_factory=datetime.now)
|
||||||
|
updated_at: datetime = Field(default_factory=datetime.now)
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
||||||
|
|
||||||
|
class GameList(BaseModel):
|
||||||
|
games: List[GameModel]
|
||||||
|
|
||||||
|
# Routers
|
||||||
|
from fastapi import APIRouter, HTTPException
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.post("/footballing", status_code=201)
|
||||||
|
async def create_game(game: GameBase):
|
||||||
|
"""Save a new game to the database"""
|
||||||
|
if request.method != "POST":
|
||||||
|
raise HTTPException(status_code=405, detail={
|
||||||
|
"message": "Method Not Allowed",
|
||||||
|
"method": "POST",
|
||||||
|
"_verb": "post"
|
||||||
|
})
|
||||||
|
|
||||||
|
new_game = GameModel(**game.dict())
|
||||||
|
games.append(new_game)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"message": "Game created successfully",
|
||||||
|
"game": new_game,
|
||||||
|
"method": "POST",
|
||||||
|
"_verb": "post"
|
||||||
|
}
|
||||||
|
|
||||||
|
@router.get("/footballing", response_model=GameList)
|
||||||
|
async def get_games():
|
||||||
|
"""Fetch all games from the database"""
|
||||||
|
if request.method != "GET":
|
||||||
|
raise HTTPException(status_code=405, detail={
|
||||||
|
"message": "Method Not Allowed",
|
||||||
|
"method": "GET",
|
||||||
|
"_verb": "get"
|
||||||
|
})
|
||||||
|
|
||||||
|
return {"games": games}
|
||||||
|
```
|
Loading…
x
Reference in New Issue
Block a user