This commit is contained in:
Waylon Walker 2023-06-09 16:04:58 -05:00
parent 1a0bf1adb9
commit c3db85a209
No known key found for this signature in database
GPG key ID: 66E2BF2B4190EFE4
21 changed files with 647 additions and 658 deletions

View file

@ -21,7 +21,6 @@ class FastModel(SQLModel):
def post(self, config: "Config" = None) -> None:
if config is None:
config = get_config()
self.pre_post()
@ -36,7 +35,6 @@ class FastModel(SQLModel):
self, id: int = None, config: "Config" = None, where=None
) -> Optional["FastModel"]:
if config is None:
config = get_config()
self.pre_get()

View file

@ -1,10 +1,11 @@
from typing import Optional
from fastapi import HTTPException
import httpx
from pydantic import BaseModel
from sqlmodel import Field, Relationship, SQLModel, Session, select
from learn_sql_model.config import Config
from learn_sql_model.config import config, get_session
from learn_sql_model.models.pet import Pet
@ -25,14 +26,13 @@ class Hero(HeroBase, table=True):
class HeroCreate(HeroBase):
...
def post(self, config: Config) -> Hero:
config.init()
with Session(config.database.engine) as session:
db_hero = Hero.from_orm(self)
session.add(db_hero)
session.commit()
session.refresh(db_hero)
return db_hero
def post(self) -> Hero:
r = httpx.post(
f"{config.api_client.url}/hero/",
json=self.dict(),
)
if r.status_code != 200:
raise RuntimeError(f"{r.status_code}:\n {r.text}")
class HeroRead(HeroBase):
@ -41,10 +41,8 @@ class HeroRead(HeroBase):
@classmethod
def get(
cls,
config: Config,
id: int,
) -> Hero:
with config.database.session as session:
hero = session.get(Hero, id)
if not hero:
@ -54,25 +52,25 @@ class HeroRead(HeroBase):
@classmethod
def list(
self,
config: Config,
where=None,
offset=0,
limit=None,
session: Session = get_session,
) -> Hero:
# with config.database.session as session:
with config.database.session as session:
statement = select(Hero)
if where != "None":
from sqlmodel import text
statement = select(Hero)
if where != "None" and where is not None:
from sqlmodel import text
statement = statement.where(text(where))
statement = statement.offset(offset).limit(limit)
heroes = session.exec(statement).all()
statement = statement.where(text(where))
statement = statement.offset(offset).limit(limit)
heroes = session.exec(statement).all()
return heroes
class HeroUpdate(SQLModel):
# id is required to get the hero
# id is required to update the hero
id: int
# all other fields, must match the model, but with Optional default None
@ -84,30 +82,22 @@ class HeroUpdate(SQLModel):
pet_id: Optional[int] = Field(default=None, foreign_key="pet.id")
pet: Optional[Pet] = Relationship(back_populates="hero")
def update(self, config: Config) -> Hero:
with Session(config.database.engine) as session:
db_hero = session.get(Hero, self.id)
if not db_hero:
raise HTTPException(status_code=404, detail="Hero not found")
hero_data = self.dict(exclude_unset=True)
for key, value in hero_data.items():
if value is not None:
setattr(db_hero, key, value)
session.add(db_hero)
session.commit()
session.refresh(db_hero)
return db_hero
def update(self) -> Hero:
r = httpx.patch(
f"{config.api_client.url}/hero/",
json=self.dict(),
)
if r.status_code != 200:
raise RuntimeError(f"{r.status_code}:\n {r.text}")
class HeroDelete(BaseModel):
id: int
def delete(self, config: Config) -> Hero:
config.init()
with Session(config.database.engine) as session:
hero = session.get(Hero, self.id)
if not hero:
raise HTTPException(status_code=404, detail="Hero not found")
session.delete(hero)
session.commit()
return {"ok": True}
def delete(self) -> Hero:
r = httpx.delete(
f"{config.api_client.url}/hero/{self.id}",
)
if r.status_code != 200:
raise RuntimeError(f"{r.status_code}:\n {r.text}")
return {"ok": True}

View file

@ -1,99 +0,0 @@
from typing import Optional
from fastapi import HTTPException
from pydantic import BaseModel
from sqlmodel import Field, Relationship, SQLModel, Session, select
from learn_sql_model.config import Config
from learn_sql_model.models.pet import Pet
class newBase(SQLModel, table=False):
class new(newBase, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
class newCreate(newBase):
...
def post(self, config: Config) -> new:
config.init()
with Session(config.database.engine) as session:
db_new = new.from_orm(self)
session.add(db_new)
session.commit()
session.refresh(db_new)
return db_new
class newRead(newBase):
id: int
@classmethod
def get(
cls,
config: Config,
id: int,
) -> new:
with config.database.session as session:
new = session.get(new, id)
if not new:
raise HTTPException(status_code=404, detail="new not found")
return new
@classmethod
def list(
self,
config: Config,
where=None,
offset=0,
limit=None,
) -> new:
with config.database.session as session:
statement = select(new)
if where != "None":
from sqlmodel import text
statement = statement.where(text(where))
statement = statement.offset(offset).limit(limit)
newes = session.exec(statement).all()
return newes
class newUpdate(SQLModel):
# id is required to get the new
id: int
# all other fields, must match the model, but with Optional default None
def update(self, config: Config) -> new:
with Session(config.database.engine) as session:
db_new = session.get(new, self.id)
if not db_new:
raise HTTPException(status_code=404, detail="new not found")
new_data = self.dict(exclude_unset=True)
for key, value in new_data.items():
if value is not None:
setattr(db_new, key, value)
session.add(db_new)
session.commit()
session.refresh(db_new)
return db_new
class newDelete(BaseModel):
id: int
def delete(self, config: Config) -> new:
config.init()
with Session(config.database.engine) as session:
new = session.get(new, self.id)
if not new:
raise HTTPException(status_code=404, detail="new not found")
session.delete(new)
session.commit()
return {"ok": True}