2022-12-21 05:53:59 +00:00
|
|
|
from sqlalchemy import create_engine
|
|
|
|
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
|
2022-12-21 09:39:51 +00:00
|
|
|
from fastapi import FastAPI, status
|
2022-12-21 05:53:59 +00:00
|
|
|
|
|
|
|
orm.start_mappers()
|
|
|
|
engine = create_engine("sqlite:///:memory:")
|
|
|
|
orm.metadata.create_all(engine)
|
|
|
|
get_session = sessionmaker(bind=engine)
|
|
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
|
|
|
2022-12-21 09:39:51 +00:00
|
|
|
@app.put("/tribes", status_code=status.HTTP_201_CREATED, response_model=TribeModel)
|
|
|
|
async def put_tribe(item: TribeModel):
|
2022-12-21 05:53:59 +00:00
|
|
|
tribe = Tribe(**item.dict())
|
|
|
|
|
|
|
|
session = get_session()
|
|
|
|
session.add(tribe)
|
|
|
|
session.commit()
|
|
|
|
|
2022-12-21 09:39:51 +00:00
|
|
|
return tribe.to_dict()
|
2022-12-21 05:53:59 +00:00
|
|
|
|
|
|
|
|
2022-12-21 09:39:51 +00:00
|
|
|
@app.get("/tribes", response_model=list[TribeModel])
|
|
|
|
async def get_tribes():
|
2022-12-21 05:53:59 +00:00
|
|
|
|
|
|
|
session = get_session()
|
|
|
|
tribes = session.query(Tribe).all()
|
2022-12-21 09:39:51 +00:00
|
|
|
print(tribes[0].__dict__)
|
2022-12-21 05:53:59 +00:00
|
|
|
|
|
|
|
return [t.to_dict() for t in tribes]
|