wip
This commit is contained in:
parent
e86e432102
commit
28eda9e899
5 changed files with 121 additions and 184 deletions
|
|
@ -1,86 +1,89 @@
|
|||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlmodel import SQLModel, Session
|
||||
from sqlmodel import Session, select
|
||||
|
||||
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
|
||||
from learn_sql_model.config import get_session
|
||||
from learn_sql_model.models.{{ modelname }} import {{ modelname }}, {{ modelname }}Create, {{ modelname }}Read, {{ modelname }}Update, {{ modelname }}s
|
||||
|
||||
{{modelname.lower()}}_router = APIRouter()
|
||||
{{ modelname }}_router = APIRouter()
|
||||
|
||||
|
||||
@{{modelname.lower()}}_router.on_event("startup")
|
||||
@{{ modelname }}_router.on_event("startup")
|
||||
def on_startup() -> None:
|
||||
SQLModel.metadata.create_all(get_config().database.engine)
|
||||
# SQLModel.metadata.create_all(get_config().database.engine)
|
||||
...
|
||||
|
||||
|
||||
@{{modelname.lower()}}_router.get("/{{modelname.lower()}}/{{{modelname.lower()}}_id}")
|
||||
async def get_{{modelname.lower()}}(
|
||||
@{{ modelname }}_router.get("/{{ modelname }}/{{{ modelname }}_id}")
|
||||
def get_{{ modelname }}(
|
||||
*,
|
||||
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 }}_id: int,
|
||||
) -> {{ modelname }}Read:
|
||||
"get one {{ modelname }}"
|
||||
{{ modelname }} = session.get({{ modelname }}, {{ modelname }}_id)
|
||||
if not {{ modelname }}:
|
||||
raise HTTPException(status_code=404, detail="{{ modelname }} not found")
|
||||
return {{ modelname }}
|
||||
|
||||
|
||||
@{{modelname.lower()}}_router.post("/{{modelname.lower()}}/")
|
||||
async def post_{{modelname.lower()}}(
|
||||
@{{ modelname }}_router.post("/{{ modelname }}/")
|
||||
def post_{{ modelname }}(
|
||||
*,
|
||||
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()}})
|
||||
{{ modelname }}: {{ modelname }}Create,
|
||||
) -> {{ modelname }}Read:
|
||||
"create a {{ modelname }}"
|
||||
db_{{ modelname }} = {{ modelname }}.from_orm({{ modelname }})
|
||||
session.add(db_{{ modelname }})
|
||||
session.commit()
|
||||
session.refresh(db_{{modelname.lower()}})
|
||||
await manager.broadcast({{{modelname.lower()}}.json()}, id=1)
|
||||
return db_{{modelname.lower()}}
|
||||
session.refresh(db_{{ modelname }})
|
||||
await manager.broadcast({{{ modelname }}.json()}, id=1)
|
||||
return db_{{ modelname }}
|
||||
|
||||
|
||||
@{{modelname.lower()}}_router.patch("/{{modelname.lower()}}/")
|
||||
async def patch_{{modelname.lower()}}(
|
||||
@{{ modelname }}_router.patch("/{{ modelname }}/")
|
||||
def patch_{{ modelname }}(
|
||||
*,
|
||||
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()}})
|
||||
{{ modelname }}: {{ modelname }}Update,
|
||||
) -> {{ modelname }}Read:
|
||||
"update a {{ modelname }}"
|
||||
db_{{ modelname }} = session.get({{ modelname }}, {{ modelname }}.id)
|
||||
if not db_{{ modelname }}:
|
||||
raise HTTPException(status_code=404, detail="{{ modelname }} not found")
|
||||
for key, value in {{ modelname }}.dict(exclude_unset=True).items():
|
||||
setattr(db_{{ modelname }}, key, value)
|
||||
session.add(db_{{ modelname }})
|
||||
session.commit()
|
||||
session.refresh(db_{{modelname.lower()}})
|
||||
await manager.broadcast({{{modelname.lower()}}.json()}, id=1)
|
||||
return db_{{modelname.lower()}}
|
||||
session.refresh(db_{{ modelname }})
|
||||
await manager.broadcast({{{ modelname }}.json()}, id=1)
|
||||
return db_{{ modelname }}
|
||||
|
||||
|
||||
@{{modelname.lower()}}_router.delete("/{{modelname.lower()}}/{{{modelname.lower()}}_id}")
|
||||
async def delete_{{modelname.lower()}}(
|
||||
@{{ modelname }}_router.delete("/{{ modelname }}/{{{ modelname }}_id}")
|
||||
def delete_{{ modelname }}(
|
||||
*,
|
||||
session: Session = Depends(get_session),
|
||||
{{modelname.lower()}}_id: int,
|
||||
{{ modelname }}_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()}})
|
||||
"delete a {{ modelname }}"
|
||||
{{ modelname }} = session.get({{ modelname }}, {{ modelname }}_id)
|
||||
if not {{ modelname }}:
|
||||
raise HTTPException(status_code=404, detail="{{ modelname }} not found")
|
||||
session.delete({{ modelname }})
|
||||
session.commit()
|
||||
await manager.broadcast(f"deleted {{modelname.lower()}} {{{modelname.lower()}}_id}", id=1)
|
||||
await manager.broadcast(f"deleted {{ modelname }} {{{ modelname }}_id}", id=1)
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
@{{modelname.lower()}}_router.get("/{{modelname.lower()}}s/")
|
||||
async def get_{{modelname.lower()}}s(
|
||||
@{{ modelname }}_router.get("/{{ modelname }}s/")
|
||||
def get_{{ modelname }}s(
|
||||
*,
|
||||
session: Session = Depends(get_session),
|
||||
) -> list[{{modelname}}]:
|
||||
"get all {{modelname.lower()}}s"
|
||||
return {{modelname}}Read.list(session=session)
|
||||
) -> {{ modelname }}s:
|
||||
"get all {{ modelname }}s"
|
||||
statement = select({{ modelname }})
|
||||
{{ modelname }}s = session.exec(statement).all()
|
||||
return {{ modelname }}s(__root__={{ modelname }}s)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue