Update code in endpoints/footballing.post.py
This commit is contained in:
parent
739e071090
commit
6201008c0e
@ -1,5 +1,5 @@
|
|||||||
```python
|
```python
|
||||||
from typing import Optional
|
from typing import List
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
@ -10,45 +10,58 @@ class GameCreate(GameBase):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
class GameUpdate(GameBase):
|
class GameUpdate(GameBase):
|
||||||
id: str = Field(default_factory=lambda: str(uuid4()))
|
pass
|
||||||
|
|
||||||
class GameInDB(GameBase):
|
class GameInDBBase(GameBase):
|
||||||
id: str = Field(default_factory=lambda: str(uuid4()))
|
id: str = Field(..., description="Unique identifier for the game")
|
||||||
created_at: datetime = Field(default_factory=datetime.utcnow)
|
created_at: datetime = Field(..., description="Timestamp of when the game was created")
|
||||||
updated_at: Optional[datetime] = None
|
updated_at: datetime = Field(..., description="Timestamp of when the game was last updated")
|
||||||
|
|
||||||
games = []
|
class Config:
|
||||||
|
orm_mode = True
|
||||||
|
|
||||||
from fastapi import APIRouter, HTTPException, Depends
|
class Game(GameInDBBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class GameInDB(GameInDBBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
games = [] # In-memory storage
|
||||||
|
|
||||||
|
from fastapi import APIRouter, HTTPException
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@router.post("/footballing", response_model=GameInDB)
|
@router.post("/footballing", status_code=201)
|
||||||
async def create_game(game: GameCreate):
|
async def create_game(game: GameCreate):
|
||||||
"""Create a new game"""
|
"""Create a new game"""
|
||||||
if request.method != "POST":
|
if request.method != "POST":
|
||||||
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
||||||
|
|
||||||
game_data = game.dict()
|
new_game = Game(
|
||||||
game_data["id"] = str(uuid4())
|
id=str(uuid4()),
|
||||||
game_data["created_at"] = datetime.utcnow()
|
name=game.name,
|
||||||
games.append(game_data)
|
description=game.description,
|
||||||
|
genre=game.genre,
|
||||||
|
release_date=game.release_date,
|
||||||
|
created_at=datetime.now(),
|
||||||
|
updated_at=datetime.now()
|
||||||
|
)
|
||||||
|
|
||||||
|
games.append(new_game)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"method": "POST",
|
"method": "POST",
|
||||||
"_verb": "post",
|
"_verb": "post",
|
||||||
**game_data
|
"message": "Game created successfully",
|
||||||
|
"game": new_game
|
||||||
}
|
}
|
||||||
|
|
||||||
@router.get("/footballing", response_model=list[GameInDB])
|
@router.get("/footballing", response_model=List[GameInDB])
|
||||||
async def get_games():
|
async def get_games():
|
||||||
"""Get all games"""
|
"""Get all games"""
|
||||||
if request.method != "GET":
|
if request.method != "GET":
|
||||||
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
||||||
|
|
||||||
return {
|
return games
|
||||||
"method": "GET",
|
|
||||||
"_verb": "get",
|
|
||||||
"games": games
|
|
||||||
}
|
|
||||||
```
|
```
|
Loading…
x
Reference in New Issue
Block a user