40 lines
976 B
Python
40 lines
976 B
Python
from __future__ import annotations
|
|
|
|
from decimal import Decimal
|
|
|
|
from fastapi import FastAPI, HTTPException
|
|
from pydantic import BaseModel, ConfigDict
|
|
|
|
from app.matcher import CatalogMatcher
|
|
|
|
|
|
class CatalogItemInfo(BaseModel):
|
|
model_config = ConfigDict(extra="forbid")
|
|
|
|
sku: str
|
|
name: str
|
|
unit: str
|
|
price: Decimal
|
|
|
|
|
|
def register_test_info_route(
|
|
application: FastAPI, matcher: CatalogMatcher
|
|
) -> None:
|
|
items_by_sku = {item.sku: item for item in matcher.items}
|
|
|
|
@application.get(
|
|
"/get_info_by/{sku}",
|
|
response_model=CatalogItemInfo,
|
|
include_in_schema=False,
|
|
)
|
|
def get_info_by(sku: str) -> CatalogItemInfo:
|
|
item = items_by_sku.get(sku.strip().upper())
|
|
if item is None:
|
|
raise HTTPException(status_code=404, detail="SKU not found")
|
|
return CatalogItemInfo(
|
|
sku=item.sku,
|
|
name=item.name,
|
|
unit=item.unit,
|
|
price=item.price,
|
|
)
|