This commit is contained in:
Waylon Walker 2023-06-06 09:57:21 -05:00
parent 6b1c60a550
commit 839cbd0dc0
No known key found for this signature in database
GPG key ID: 66E2BF2B4190EFE4
17 changed files with 213 additions and 35 deletions

View file

@ -11,8 +11,8 @@ hero_router = APIRouter()
@hero_router.on_event("startup")
def on_startup(config: Config = Depends(get_config)) -> None:
SQLModel.metadata.create_all(config.database.engine)
def on_startup() -> None:
SQLModel.metadata.create_all(get_config().database.engine)
@hero_router.get("/items/")
@ -26,6 +26,12 @@ def get_hero(id: int, config: Config = Depends(get_config)) -> Hero:
return Hero().get(id=id, config=config)
@hero_router.get("/h/{id}")
def get_h(id: int, config: Config = Depends(get_config)) -> Hero:
"get one hero"
return Hero().get(id=id, config=config)
@hero_router.post("/hero/")
def post_hero(hero: Hero, config: Config = Depends(get_config)) -> Hero:
"read all the heros"

View file

@ -1,3 +1,4 @@
from rich.console import Console
import typer
import uvicorn
@ -27,3 +28,38 @@ def run(
),
):
uvicorn.run(**get_config().api_server.dict())
@api_app.command()
def status(
verbose: bool = typer.Option(
False,
callback=verbose_callback,
help="show the log messages",
),
):
import httpx
config = get_config()
host = config.api_server.host
port = config.api_server.port
url = f"http://{host}:{port}/docs"
try:
r = httpx.get(url)
if r.status_code == 200:
Console().print(f"[green]API: ([gold1]{url}[green]) is running")
else:
Console().print(f"[red]API: ([gold1]{url}[red]) is not running")
except httpx.ConnectError:
Console().print(f"[red]API: ([gold1]{url}[red]) is not running")
try:
with config.database.engine.connect():
Console().print(
f"[green]database: ([gold1]{config.database.engine}[green]) is running"
)
except Exception as e:
Console().print(
f"[red]database: ([gold1]{config.database.engine}[red]) is not running"
)

View file

@ -5,16 +5,14 @@ from typer.main import get_group
from learn_sql_model.cli.api import api_app
from learn_sql_model.cli.config import config_app
from learn_sql_model.cli.hero import hero_app
from learn_sql_model.cli.model import model_app
from learn_sql_model.cli.tui import tui_app
app = typer.Typer(
name="learn_sql_model",
help="learn-sql-model cli for managing the project",
)
app.add_typer(config_app, name="config")
app.add_typer(tui_app, name="tui")
app.add_typer(model_app, name="model")
# app.add_typer(tui_app, name="tui")
# app.add_typer(model_app, name="model")
app.add_typer(api_app, name="api")
app.add_typer(hero_app, name="hero")

View file

@ -1,15 +1,14 @@
import sys
from typing import List, Optional, Union
from pydantic_typer import expand_pydantic_args
from engorgio import engorgio
from rich.console import Console
import typer
from learn_sql_model.config import Config
from learn_sql_model.config import Config, get_config
from learn_sql_model.factories.hero import HeroFactory
from learn_sql_model.factories.pet import PetFactory
from learn_sql_model.models.hero import Hero
from learn_sql_model.models.pet import Pet
import sys
from learn_sql_model.models.hero import Hero, HeroCreate
hero_app = typer.Typer()
@ -20,9 +19,9 @@ def hero():
@hero_app.command()
@expand_pydantic_args(typer=True)
@engorgio(typer=True)
def get(
id: Optional[int] = None,
id: Optional[int] = typer.Argument(default=None),
config: Config = None,
) -> Union[Hero, List[Hero]]:
"get one hero"
@ -33,28 +32,36 @@ def get(
@hero_app.command()
@expand_pydantic_args(typer=True)
@engorgio(typer=True)
def list(
config: Config = None,
) -> Union[Hero, List[Hero]]:
"get one hero"
hero = Hero().get()
Console().print(hero)
return hero
@hero_app.command()
@engorgio(typer=True)
def create(
hero: Hero,
pet: Pet = None,
hero: HeroCreate,
config: Config = None,
) -> Hero:
"read all the heros"
config.init()
hero.pet = pet
hero = hero.post(config=config)
Console().print(hero)
@hero_app.command()
@expand_pydantic_args(typer=True)
@engorgio(typer=True)
def populate(
hero: Hero,
n: int = 10,
config: Config = None,
) -> Hero:
"read all the heros"
if config is None:
config = Config()
config = get_config()
if config.env == "prod":
Console().print("populate is not supported in production")
sys.exit(1)

View file

@ -6,8 +6,7 @@ from learn_sql_model.models.fast_model import FastModel
from learn_sql_model.models.pet import Pet
class Hero(FastModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
class HeroBase(FastModel, table=False):
name: str
secret_name: str
age: Optional[int] = None
@ -15,3 +14,23 @@ class Hero(FastModel, table=True):
pet_id: Optional[int] = Field(default=None, foreign_key="pet.id")
pet: Optional[Pet] = Relationship(back_populates="hero")
class Hero(HeroBase, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
class HeroCreate(HeroBase):
...
class HeroRead(HeroBase):
id: Optional[int] = Field(default=None, primary_key=True)
class HeroUpdate(HeroBase):
...
class HeroDelete(HeroBase):
id: Optional[int] = Field(default=None, primary_key=True)

View file

@ -1,3 +1,4 @@
from datetime import datetime
from typing import Optional, TYPE_CHECKING
from sqlmodel import Field, Relationship
@ -11,4 +12,5 @@ if TYPE_CHECKING:
class Pet(FastModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
name: str = "Jim"
birthday: Optional[datetime] = None
hero: "Hero" = Relationship(back_populates="pet")