fooder-api/fooder/view/product.py

39 lines
1 KiB
Python
Raw Normal View History

2023-04-01 16:19:12 +02:00
from fastapi import APIRouter, Depends, Request
from ..model.product import Product, CreateProductPayload, ListProductPayload
2024-03-25 18:22:18 +01:00
from ..controller.product import ListProduct, CreateProduct, GetProductByBarCode
2023-04-01 20:13:11 +02:00
from typing import Optional
2023-04-01 16:19:12 +02:00
router = APIRouter(tags=["product"])
2023-04-01 20:23:04 +02:00
@router.get("", response_model=ListProductPayload)
2023-04-01 16:19:12 +02:00
async def list_product(
request: Request,
controller: ListProduct = Depends(ListProduct),
limit: int = 10,
offset: int = 0,
2023-04-01 20:13:11 +02:00
q: Optional[str] = None,
2023-04-01 16:19:12 +02:00
):
return ListProductPayload(
2023-04-01 20:13:11 +02:00
products=[p async for p in controller.call(limit=limit, offset=offset, q=q)]
2023-04-01 16:19:12 +02:00
)
2023-04-01 20:23:04 +02:00
@router.post("", response_model=Product)
2023-04-01 16:19:12 +02:00
async def create_product(
request: Request,
data: CreateProductPayload,
contoller: CreateProduct = Depends(CreateProduct),
):
return await contoller.call(data)
2024-03-25 18:22:18 +01:00
@router.get("/by_barcode", response_model=Product)
async def get_by_bar_code(
request: Request,
barcode: str,
contoller: GetProductByBarCode = Depends(GetProductByBarCode),
):
return await contoller.call(barcode)