86 lines
3.2 KiB
Django/Jinja
86 lines
3.2 KiB
Django/Jinja
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlmodel import SQLModel, Session
|
|
|
|
from learn_sql_model.api.websocket_connection_manager import manager
|
|
from learn_sql_model.config import get_config, get_session
|
|
from learn_sql_model.models.{{modelname.lower()}} import {{modelname}}, {{modelname}}Create, {{modelname}}Read, {{modelname}}Update
|
|
|
|
{{modelname.lower()}}_router = APIRouter()
|
|
|
|
|
|
@{{modelname.lower()}}_router.on_event("startup")
|
|
def on_startup() -> None:
|
|
SQLModel.metadata.create_all(get_config().database.engine)
|
|
|
|
|
|
@{{modelname.lower()}}_router.get("/{{modelname.lower()}}/{{{modelname.lower()}}_id}")
|
|
async def get_{{modelname.lower()}}(
|
|
*,
|
|
session: Session = Depends(get_session),
|
|
{{modelname.lower()}}_id: int,
|
|
) -> {{modelname}}Read:
|
|
"get one {{modelname.lower()}}"
|
|
{{modelname.lower()}} = session.get({{modelname}}, {{modelname.lower()}}_id)
|
|
if not {{modelname.lower()}}:
|
|
raise HTTPException(status_code=404, detail="{{modelname}} not found")
|
|
return {{modelname.lower()}}
|
|
|
|
|
|
@{{modelname.lower()}}_router.post("/{{modelname.lower()}}/")
|
|
async def post_{{modelname.lower()}}(
|
|
*,
|
|
session: Session = Depends(get_session),
|
|
{{modelname.lower()}}: {{modelname}}Create,
|
|
) -> {{modelname}}Read:
|
|
"read all the {{modelname.lower()}}s"
|
|
db_{{modelname.lower()}} = {{modelname}}.from_orm({{modelname.lower()}})
|
|
session.add(db_{{modelname.lower()}})
|
|
session.commit()
|
|
session.refresh(db_{{modelname.lower()}})
|
|
await manager.broadcast({{{modelname.lower()}}.json()}, id=1)
|
|
return db_{{modelname.lower()}}
|
|
|
|
|
|
@{{modelname.lower()}}_router.patch("/{{modelname.lower()}}/")
|
|
async def patch_{{modelname.lower()}}(
|
|
*,
|
|
session: Session = Depends(get_session),
|
|
{{modelname.lower()}}: {{modelname}}Update,
|
|
) -> {{modelname}}Read:
|
|
"read all the {{modelname.lower()}}s"
|
|
db_{{modelname.lower()}} = session.get({{modelname}}, {{modelname.lower()}}.id)
|
|
if not db_{{modelname.lower()}}:
|
|
raise HTTPException(status_code=404, detail="{{modelname}} not found")
|
|
for key, value in {{modelname.lower()}}.dict(exclude_unset=True).items():
|
|
setattr(db_{{modelname.lower()}}, key, value)
|
|
session.add(db_{{modelname.lower()}})
|
|
session.commit()
|
|
session.refresh(db_{{modelname.lower()}})
|
|
await manager.broadcast({{{modelname.lower()}}.json()}, id=1)
|
|
return db_{{modelname.lower()}}
|
|
|
|
|
|
@{{modelname.lower()}}_router.delete("/{{modelname.lower()}}/{{{modelname.lower()}}_id}")
|
|
async def delete_{{modelname.lower()}}(
|
|
*,
|
|
session: Session = Depends(get_session),
|
|
{{modelname.lower()}}_id: int,
|
|
):
|
|
"read all the {{modelname.lower()}}s"
|
|
{{modelname.lower()}} = session.get({{modelname}}, {{modelname.lower()}}_id)
|
|
if not {{modelname.lower()}}:
|
|
raise HTTPException(status_code=404, detail="{{modelname}} not found")
|
|
session.delete({{modelname.lower()}})
|
|
session.commit()
|
|
await manager.broadcast(f"deleted {{modelname.lower()}} {{{modelname.lower()}}_id}", id=1)
|
|
return {"ok": True}
|
|
|
|
|
|
@{{modelname.lower()}}_router.get("/{{modelname.lower()}}s/")
|
|
async def get_{{modelname.lower()}}s(
|
|
*,
|
|
session: Session = Depends(get_session),
|
|
) -> list[{{modelname}}]:
|
|
"get all {{modelname.lower()}}s"
|
|
return {{modelname}}Read.list(session=session)
|
|
|