Esempio n. 1
0
async def fetch(user_id: int, id_: int) -> Optional[Hero]:
    query = (
        HeroModel.select()
        .where(HeroModel.c.user_id == user_id)
        .where(HeroModel.c.id == id_)
    )

    result = await database.fetch_one(query)
    return Hero.parse_obj(dict(result)) if result else None
Esempio n. 2
0
async def persist(user_id: int, dto: CreateHeroDto) -> Hero:
    values = {**dto.dict(), "user_id": user_id}
    query = HeroModel.insert().values(**values)

    try:
        last_record_id = await database.execute(query)
    except UniqueViolationError:
        raise HeroNotUniqueError()

    return Hero.parse_obj({**values, "id": last_record_id})
Esempio n. 3
0
async def fetch_all_by_user(user_id: int) -> Iterable[Hero]:
    query = HeroModel.select().where(HeroModel.c.user_id == user_id)

    results = await database.fetch_all(query)
    return (Hero.parse_obj(dict(result)) for result in results)
Esempio n. 4
0
async def fetch_all() -> Iterable[Hero]:
    query = HeroModel.select()

    results = await database.fetch_all(query)
    return (Hero.parse_obj(dict(result)) for result in results)