recopytex/backend/api/main.py

36 lines
817 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
orm.start_mappers()
engine = create_engine("sqlite:///:memory:")
orm.metadata.create_all(engine)
get_session = sessionmaker(bind=engine)
app = FastAPI()
@app.put("/tribes")
async def put_tribe(item: TribeModel, status_code=201):
tribe = Tribe(**item.dict())
session = get_session()
session.add(tribe)
session.commit()
tribes = session.query(Tribe).all()
return {}
@app.get("/tribes")
async def get_tribes(response_model=list[TribeModel]):
session = get_session()
tribes = session.query(Tribe).all()
return [t.to_dict() for t in tribes]