add fiber and new pydantic

This commit is contained in:
doman 2023-07-30 20:18:42 +02:00
parent 73b481ad82
commit d11478df01
13 changed files with 49 additions and 8 deletions

1
.dockerignore Normal file
View file

@ -0,0 +1 @@
!negotiator/

View file

@ -17,6 +17,7 @@ class CreateProduct(AuthorizedController):
content.carb,
content.protein,
content.fat,
content.fiber,
)
return Product.from_orm(product)
except AssertionError as e:

View file

@ -52,6 +52,14 @@ class Diary(Base, CommonMixin):
"""
return sum(meal.fat for meal in self.meals)
@property
def fiber(self) -> float:
"""fiber.
:rtype: float
"""
return sum(meal.fat for meal in self.meals)
@classmethod
def query(cls, user_id: int) -> Select:
"""get_all."""

View file

@ -61,6 +61,14 @@ class Entry(Base, CommonMixin):
"""
return self.amount * self.product.fat
@property
def fiber(self) -> float:
"""fiber.
:rtype: float
"""
return self.amount * self.product.fiber
@classmethod
async def create(
cls, session: AsyncSession, meal_id: int, product_id: int, grams: float

View file

@ -51,6 +51,14 @@ class Meal(Base, CommonMixin):
"""
return sum(entry.fat for entry in self.entries)
@property
def fiber(self) -> float:
"""fiber.
:rtype: float
"""
return sum(entry.fiber for entry in self.entries)
@classmethod
async def create(
cls,

View file

@ -14,6 +14,7 @@ class Product(Base, CommonMixin):
protein: Mapped[float]
carb: Mapped[float]
fat: Mapped[float]
fiber: Mapped[float]
@property
def calories(self) -> float:
@ -21,7 +22,7 @@ class Product(Base, CommonMixin):
:rtype: float
"""
return self.protein * 4 + self.carb * 4 + self.fat * 9
return self.protein * 4 + self.carb * 4 + self.fat * 9 + self.fiber * 2
@classmethod
async def list_all(
@ -39,15 +40,23 @@ class Product(Base, CommonMixin):
@classmethod
async def create(
cls, session: AsyncSession, name: str, carb: float, protein: float, fat: float
cls,
session: AsyncSession,
name: str,
carb: float,
protein: float,
fat: float,
fiber: float,
) -> "Product":
# validation here
assert carb <= 100, "carb must be less than 100"
assert protein <= 100, "protein must be less than 100"
assert fat <= 100, "fat must be less than 100"
assert fiber <= 100, "fiber must be less than 100"
assert carb >= 0, "carb must be greater than 0"
assert protein >= 0, "protein must be greater than 0"
assert fat >= 0, "fat must be greater than 0"
assert fiber >= 0, "fiber must be greater than 0"
assert carb + protein + fat <= 100, "total must be less than 100"
# to avoid duplicates in the database keep name as lower

View file

@ -15,6 +15,7 @@ class Diary(BaseModel):
protein: float
carb: float
fat: float
fiber: float
class Config:
orm_mode = True
from_attributes = True

View file

@ -15,9 +15,10 @@ class Entry(BaseModel):
protein: float
carb: float
fat: float
fiber: float
class Config:
orm_mode = True
from_attributes = True
class CreateEntryPayload(BaseModel):

View file

@ -13,11 +13,12 @@ class Meal(BaseModel):
protein: float
carb: float
fat: float
fiber: float
entries: List[Entry]
diary_id: int
class Config:
orm_mode = True
from_attributes = True
class CreateMealPayload(BaseModel):

View file

@ -11,9 +11,10 @@ class Product(BaseModel):
protein: float
carb: float
fat: float
fiber: float
class Config:
orm_mode = True
from_attributes = True
class CreateProductPayload(BaseModel):
@ -23,6 +24,7 @@ class CreateProductPayload(BaseModel):
protein: float
carb: float
fat: float
fiber: float
class ListProductPayload(BaseModel):

View file

@ -5,7 +5,7 @@ class User(BaseModel):
username: str
class Config:
orm_mode = True
from_attributes = True
class CreateUserPayload(BaseModel):

View file

@ -1,4 +1,4 @@
from pydantic import BaseSettings
from pydantic_settings import BaseSettings
from typing import List

View file

@ -1,5 +1,6 @@
fastapi
pydantic
pydantic_settings
sqlalchemy[postgresql_asyncpg]
uvicorn[standard]
asyncpg