fooder-api/fooder/model/product.py

52 lines
1.4 KiB
Python

from typing import Annotated
from fooder.model.base import (
ObjModelMixin,
Calories,
)
from pydantic import BaseModel, Field
from fooder.utils.calories import calculate_calories
MacronutrientPer100g = Annotated[float, Field(ge=0, le=100)]
class ProductModel(ObjModelMixin, BaseModel):
name: str
protein: MacronutrientPer100g
carb: MacronutrientPer100g
fat: MacronutrientPer100g
fiber: MacronutrientPer100g
calories: Calories
barcode: str | None = None
class ProductCreateModel(BaseModel):
name: str
protein: MacronutrientPer100g
carb: MacronutrientPer100g
fat: MacronutrientPer100g
fiber: MacronutrientPer100g
calories: Calories | None = None
barcode: str | None = None
@property
def resolved_calories(self) -> float:
return (
self.calories
if self.calories is not None
else calculate_calories(
protein=self.protein,
carb=self.carb,
fat=self.fat,
fiber=self.fiber,
)
)
class ProductUpdateModel(BaseModel):
name: str | None = None
protein: MacronutrientPer100g | None = None
carb: MacronutrientPer100g | None = None
fat: MacronutrientPer100g | None = None
fiber: MacronutrientPer100g | None = None
calories: Calories | None = None
barcode: str | None = None