recopytex/backend/api/main.py

36 lines
877 B
Python

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
from fastapi import FastAPI, status
orm.start_mappers()
engine = create_engine("sqlite:///:memory:")
orm.metadata.create_all(engine)
get_session = sessionmaker(bind=engine)
app = FastAPI()
@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()
return tribe.to_dict()
@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]