Update code in endpoints/keeper.post.py
This commit is contained in:
parent
bb555d51d1
commit
55727b0160
97
endpoints/keeper.post.py
Normal file
97
endpoints/keeper.post.py
Normal file
@ -0,0 +1,97 @@
|
||||
```python
|
||||
from typing import List, Optional
|
||||
from pydantic import BaseModel, Field
|
||||
from datetime import datetime
|
||||
|
||||
# models/game.py
|
||||
|
||||
|
||||
class GameCreate(GameBase):
|
||||
pass
|
||||
|
||||
class GameUpdate(GameBase):
|
||||
pass
|
||||
|
||||
class GameInDBBase(GameBase):
|
||||
id: int
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
class Game(GameInDBBase):
|
||||
pass
|
||||
|
||||
class GameInDB(GameInDBBase):
|
||||
pass
|
||||
|
||||
# schemas/game.py
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional
|
||||
|
||||
class GameBase(BaseModel):
|
||||
name: str
|
||||
description: Optional[str] = None
|
||||
genre: str
|
||||
price: float
|
||||
|
||||
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
|
||||
|
||||
# endpoints/games.post.py
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List
|
||||
|
||||
from models.game import GameCreate, GameInDB
|
||||
from schemas.game import Game
|
||||
import crud
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.post("/games", response_model=Game)
|
||||
async def create_game(game: GameCreate, db: Session = Depends(get_db)):
|
||||
"""Create a new game"""
|
||||
if request.method != "POST":
|
||||
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
||||
|
||||
db_game = crud.game.create(db, game=game)
|
||||
return {
|
||||
"method": "POST",
|
||||
"_verb": "post",
|
||||
"data": db_game
|
||||
}
|
||||
|
||||
@router.get("/games", response_model=List[Game])
|
||||
async def get_games(db: Session = Depends(get_db)):
|
||||
"""Get all games"""
|
||||
games = crud.game.get_multi(db)
|
||||
return {
|
||||
"method": "GET",
|
||||
"_verb": "get",
|
||||
"data": games
|
||||
}
|
||||
```
|
||||
|
||||
```
|
||||
# requirements.txt
|
||||
fastapi
|
||||
uvicorn
|
||||
pydantic
|
||||
sqlalchemy
|
||||
```
|
Loading…
x
Reference in New Issue
Block a user