learn-sql-model/learn_sql_model/cli/hero.py
2023-06-09 16:04:58 -05:00

91 lines
1.7 KiB
Python

import sys
from typing import List, Optional, Union
from engorgio import engorgio
from rich.console import Console
import typer
from learn_sql_model.config import get_config
from learn_sql_model.factories.hero import HeroFactory
from learn_sql_model.models.hero import (
Hero,
HeroCreate,
HeroDelete,
HeroRead,
HeroUpdate,
)
hero_app = typer.Typer()
config = get_config()
@hero_app.callback()
def hero():
"model cli"
@hero_app.command()
@engorgio(typer=True)
def get(
hero_id: Optional[int] = typer.Argument(default=None),
) -> Union[Hero, List[Hero]]:
"get one hero"
hero = HeroRead.get(id=hero_id)
Console().print(hero)
return hero
@hero_app.command()
@engorgio(typer=True)
def list(
where: Optional[str] = None,
offset: int = 0,
limit: Optional[int] = None,
) -> Union[Hero, List[Hero]]:
"list many heros"
hero = HeroRead.list(where=where, offset=offset, limit=limit)
Console().print(hero)
return hero
@hero_app.command()
@engorgio(typer=True)
def create(
hero: HeroCreate,
) -> Hero:
"create one hero"
hero.post()
@hero_app.command()
@engorgio(typer=True)
def update(
hero: HeroUpdate,
) -> Hero:
"update one hero"
hero.update()
@hero_app.command()
@engorgio(typer=True)
def delete(
hero: HeroDelete,
) -> Hero:
"delete a hero by id"
hero.delete()
@hero_app.command()
@engorgio(typer=True)
def populate(
n: int = 10,
) -> Hero:
"Create n number of heros"
if config.env == "prod":
Console().print("populate is not supported in production")
sys.exit(1)
for hero in HeroFactory().batch(n):
hero = HeroCreate(**hero.dict())
hero.post()