Update code in endpoints/basketba.post.py
This commit is contained in:
parent
ad8cfc349d
commit
ce046e7fad
63
endpoints/basketba.post.py
Normal file
63
endpoints/basketba.post.py
Normal file
@ -0,0 +1,63 @@
|
||||
```python
|
||||
from typing import List
|
||||
from pydantic import BaseModel, Field
|
||||
from sqlalchemy import Column, Integer, String, Boolean
|
||||
from .database import Base
|
||||
|
||||
|
||||
|
||||
class GameCreate(GameBase):
|
||||
pass
|
||||
|
||||
class GameOut(GameBase):
|
||||
id: int
|
||||
|
||||
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
name = Column(String, nullable=False, unique=True)
|
||||
description = Column(String, nullable=False)
|
||||
category = Column(String, nullable=False)
|
||||
is_active = Column(Boolean, default=True)
|
||||
|
||||
games = []
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.post("/basketba", status_code=201)
|
||||
async def create_game(game: GameCreate):
|
||||
"""Create a new game"""
|
||||
if request.method != "POST":
|
||||
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
||||
|
||||
db_game = Game(
|
||||
name=game.name,
|
||||
description=game.description,
|
||||
category=game.category,
|
||||
is_active=game.is_active
|
||||
)
|
||||
db.add(db_game)
|
||||
db.commit()
|
||||
db.refresh(db_game)
|
||||
|
||||
return {
|
||||
"method": "POST",
|
||||
"_verb": "post",
|
||||
**game.dict(),
|
||||
"id": db_game.id
|
||||
}
|
||||
|
||||
@router.get("/basketba", response_model=List[GameOut])
|
||||
async def get_games():
|
||||
"""Get all active games"""
|
||||
games = db.query(Game).filter(Game.is_active == True).all()
|
||||
return [{
|
||||
"id": game.id,
|
||||
"name": game.name,
|
||||
"description": game.description,
|
||||
"category": game.category,
|
||||
"is_active": game.is_active
|
||||
} for game in games]
|
||||
```
|
Loading…
x
Reference in New Issue
Block a user