1
from __future__ import annotations
6
from fastapi import APIRouter, Query, Request, Depends
7
from fastapi_filter import FilterDepends
9
from app.basic.product.schemas import (
15
ExceptionResponseSchema,
17
from app.basic.product.services import ProductService
19
product_router = APIRouter(
20
responses={"400": {"model": ExceptionResponseSchema}},
24
@product_router.get("", response_model=ProductListSchema)
25
async def product_list(
26
model_filter: ProductFilter = FilterDepends(ProductFilter),
27
size: int = Query(ge=1, le=100, default=100),
28
service: ProductService = Depends()
30
data = await service.list(model_filter, size)
31
cursor = model_filter.lsn__gt
32
return {'size': len(data), 'cursor': cursor, 'data': data}
35
@product_router.post("", response_model=ProductScheme)
36
async def product_create(schema: ProductCreateScheme, service: ProductService = Depends()):
37
return await service.create(obj=schema)
40
@product_router.get("/{product_id}")
41
async def product_get(product_id: uuid.UUID, service: ProductService = Depends()) -> typing.Union[None, ProductScheme]:
42
return await service.get(id=product_id)
45
@product_router.put("/{product_id}", response_model=ProductScheme)
46
async def product_update(product_id: uuid.UUID, schema: ProductUpdateScheme, service: ProductService = Depends()):
47
return await service.update(id=product_id, obj=schema)
50
@product_router.delete("/{product_id}")
51
async def product_delete(product_id: uuid.UUID, service: ProductService = Depends()):
52
await service.delete(id=product_id)
54
@product_router.get("/barcode/{barcode}", response_model=ProductScheme)
55
async def product_by_barcode(barcode: str, service: ProductService = Depends()):
56
return await service.product_by_barcode(barcode=barcode)