Feat: add status and fix response_model
This commit is contained in:
parent
8911f8ddeb
commit
21821c275e
@ -4,7 +4,7 @@ from sqlalchemy.orm import clear_mappers, sessionmaker
|
||||
import backend.adapters.orm as orm
|
||||
from backend.api.model import TribeModel
|
||||
from backend.model.tribe import Tribe
|
||||
from fastapi import FastAPI
|
||||
from fastapi import FastAPI, status
|
||||
|
||||
orm.start_mappers()
|
||||
engine = create_engine("sqlite:///:memory:")
|
||||
@ -14,22 +14,22 @@ get_session = sessionmaker(bind=engine)
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.put("/tribes")
|
||||
async def put_tribe(item: TribeModel, status_code=201):
|
||||
@app.put("/tribes", status_code=status.HTTP_201_CREATED, response_model=TribeModel)
|
||||
async def put_tribe(item: TribeModel):
|
||||
tribe = Tribe(**item.dict())
|
||||
|
||||
session = get_session()
|
||||
session.add(tribe)
|
||||
session.commit()
|
||||
tribes = session.query(Tribe).all()
|
||||
|
||||
return {}
|
||||
return tribe.to_dict()
|
||||
|
||||
|
||||
@app.get("/tribes")
|
||||
async def get_tribes(response_model=list[TribeModel]):
|
||||
@app.get("/tribes", response_model=list[TribeModel])
|
||||
async def get_tribes():
|
||||
|
||||
session = get_session()
|
||||
tribes = session.query(Tribe).all()
|
||||
print(tribes[0].__dict__)
|
||||
|
||||
return [t.to_dict() for t in tribes]
|
||||
|
@ -4,5 +4,5 @@ from pydantic import BaseModel
|
||||
class TribeModel(BaseModel):
|
||||
name: str
|
||||
level: str
|
||||
students: list = []
|
||||
assessments: list = []
|
||||
students: list | None = []
|
||||
assessments: list | None = []
|
||||
|
Loading…
Reference in New Issue
Block a user