2023-04-01 20:13:11 +02:00
|
|
|
from typing import AsyncIterator, Optional
|
2023-04-01 16:19:12 +02:00
|
|
|
|
|
|
|
from fastapi import HTTPException
|
|
|
|
|
|
|
|
from ..model.product import Product, CreateProductPayload
|
|
|
|
from ..domain.product import Product as DBProduct
|
|
|
|
from .base import AuthorizedController
|
|
|
|
|
|
|
|
|
|
|
|
class CreateProduct(AuthorizedController):
|
|
|
|
async def call(self, content: CreateProductPayload) -> Product:
|
|
|
|
async with self.async_session.begin() as session:
|
|
|
|
try:
|
|
|
|
product = await DBProduct.create(
|
|
|
|
session,
|
|
|
|
content.name,
|
|
|
|
content.carb,
|
|
|
|
content.protein,
|
|
|
|
content.fat,
|
|
|
|
)
|
|
|
|
return Product.from_orm(product)
|
|
|
|
except AssertionError as e:
|
|
|
|
raise HTTPException(status_code=400, detail=e.args[0])
|
|
|
|
|
|
|
|
|
|
|
|
class ListProduct(AuthorizedController):
|
2023-04-01 20:13:11 +02:00
|
|
|
async def call(
|
|
|
|
self, limit: int, offset: int, q: Optional[str]
|
|
|
|
) -> AsyncIterator[Product]:
|
2023-04-01 16:19:12 +02:00
|
|
|
async with self.async_session() as session:
|
|
|
|
async for product in DBProduct.list_all(
|
2023-04-01 20:13:11 +02:00
|
|
|
session, limit=limit, offset=offset, q=q
|
2023-04-01 16:19:12 +02:00
|
|
|
):
|
|
|
|
yield Product.from_orm(product)
|