93 lines
2.6 KiB
Django/Jinja
93 lines
2.6 KiB
Django/Jinja
from typing import Optional
|
|
|
|
from fastapi import Depends, HTTPException
|
|
import httpx
|
|
from pydantic import BaseModel
|
|
from sqlmodel import Field, Relationship, SQLModel, Session, select
|
|
|
|
from learn_sql_model.config import config, get_session
|
|
from learn_sql_model.models.pet import Pet
|
|
|
|
|
|
class {{modelname}}Base(SQLModel, table=False):
|
|
|
|
|
|
class {{modelname}}({{modelname}}Base, table=True):
|
|
id: Optional[int] = Field(default=None, primary_key=True)
|
|
|
|
|
|
class {{modelname}}Create({{modelname}}Base):
|
|
...
|
|
|
|
def post(self) -> {{modelname}}:
|
|
r = httpx.post(
|
|
f"{config.api_client.url}/{{modelname.lower()}}/",
|
|
json=self.dict(),
|
|
)
|
|
if r.status_code != 200:
|
|
raise RuntimeError(f"{r.status_code}:\n {r.text}")
|
|
|
|
|
|
class {{modelname}}Read({{modelname}}Base):
|
|
id: int
|
|
|
|
@classmethod
|
|
def get(
|
|
cls,
|
|
id: int,
|
|
) -> {{modelname}}:
|
|
with config.database.session as session:
|
|
{{modelname.lower()}} = session.get({{modelname}}, id)
|
|
if not {{modelname.lower()}}:
|
|
raise HTTPException(status_code=404, detail="{{modelname}} not found")
|
|
return {{modelname.lower()}}
|
|
|
|
@classmethod
|
|
def list(
|
|
self,
|
|
where=None,
|
|
offset=0,
|
|
limit=None,
|
|
session: Session = get_session,
|
|
) -> {{modelname}}:
|
|
# with config.database.session as session:
|
|
|
|
statement = select({{modelname}})
|
|
if where != "None" and where is not None:
|
|
from sqlmodel import text
|
|
|
|
statement = statement.where(text(where))
|
|
statement = statement.offset(offset).limit(limit)
|
|
{{modelname.lower()}}es = session.exec(statement).all()
|
|
return {{modelname.lower()}}es
|
|
|
|
|
|
class {{modelname}}Update(SQLModel):
|
|
# id is required to update the {{modelname.lower()}}
|
|
id: int
|
|
|
|
# all other fields, must match the model, but with Optional default None
|
|
|
|
pet_id: Optional[int] = Field(default=None, foreign_key="pet.id")
|
|
pet: Optional[Pet] = Relationship(back_populates="{{modelname.lower()}}")
|
|
|
|
def update(self) -> {{modelname}}:
|
|
r = httpx.patch(
|
|
f"{config.api_client.url}/{{modelname.lower()}}/",
|
|
json=self.dict(),
|
|
)
|
|
if r.status_code != 200:
|
|
raise RuntimeError(f"{r.status_code}:\n {r.text}")
|
|
|
|
|
|
class {{modelname}}Delete(BaseModel):
|
|
id: int
|
|
|
|
def delete(self) -> {{modelname}}:
|
|
r = httpx.delete(
|
|
f"{config.api_client.url}/{{modelname.lower()}}/{self.id}",
|
|
)
|
|
if r.status_code != 200:
|
|
raise RuntimeError(f"{r.status_code}:\n {r.text}")
|
|
return {"ok": True}
|
|
|