Feat: add status and fix response_model

This commit is contained in:
Bertrand Benjamin 2022-12-21 10:39:51 +01:00
parent 8911f8ddeb
commit 21821c275e
2 changed files with 9 additions and 9 deletions

View File

@ -4,7 +4,7 @@ from sqlalchemy.orm import clear_mappers, sessionmaker
import backend.adapters.orm as orm import backend.adapters.orm as orm
from backend.api.model import TribeModel from backend.api.model import TribeModel
from backend.model.tribe import Tribe from backend.model.tribe import Tribe
from fastapi import FastAPI from fastapi import FastAPI, status
orm.start_mappers() orm.start_mappers()
engine = create_engine("sqlite:///:memory:") engine = create_engine("sqlite:///:memory:")
@ -14,22 +14,22 @@ get_session = sessionmaker(bind=engine)
app = FastAPI() app = FastAPI()
@app.put("/tribes") @app.put("/tribes", status_code=status.HTTP_201_CREATED, response_model=TribeModel)
async def put_tribe(item: TribeModel, status_code=201): async def put_tribe(item: TribeModel):
tribe = Tribe(**item.dict()) tribe = Tribe(**item.dict())
session = get_session() session = get_session()
session.add(tribe) session.add(tribe)
session.commit() session.commit()
tribes = session.query(Tribe).all()
return {} return tribe.to_dict()
@app.get("/tribes") @app.get("/tribes", response_model=list[TribeModel])
async def get_tribes(response_model=list[TribeModel]): async def get_tribes():
session = get_session() session = get_session()
tribes = session.query(Tribe).all() tribes = session.query(Tribe).all()
print(tribes[0].__dict__)
return [t.to_dict() for t in tribes] return [t.to_dict() for t in tribes]

View File

@ -4,5 +4,5 @@ from pydantic import BaseModel
class TribeModel(BaseModel): class TribeModel(BaseModel):
name: str name: str
level: str level: str
students: list = [] students: list | None = []
assessments: list = [] assessments: list | None = []