50 lines
1.6 KiB
Python
50 lines
1.6 KiB
Python
"""Modelos de la base de datos: alimentos y recetas del catálogo compartido."""
|
|
from sqlalchemy import Column, Float, Integer, String, JSON, DateTime, func
|
|
|
|
from db import Base
|
|
|
|
|
|
class Food(Base):
|
|
__tablename__ = "foods"
|
|
|
|
id = Column(String, primary_key=True)
|
|
name = Column(String, nullable=False, index=True)
|
|
kcal_100g = Column(Float, nullable=False)
|
|
zone = Column(String, default="otros")
|
|
portions = Column(JSON, default=list) # [{"name","grams"}]
|
|
barcode = Column(String, nullable=True)
|
|
source = Column(String, default="user") # off | user
|
|
off_id = Column(String, nullable=True, index=True)
|
|
author_token = Column(String, nullable=True)
|
|
created_at = Column(DateTime, server_default=func.now())
|
|
|
|
def to_app(self) -> dict:
|
|
"""Serializa a la forma que espera la app (Food.fromJson)."""
|
|
return {
|
|
"id": self.id,
|
|
"name": self.name,
|
|
"kcalPer100g": self.kcal_100g,
|
|
"zone": self.zone or "otros",
|
|
"portions": self.portions or [],
|
|
"origin": "remote",
|
|
}
|
|
|
|
|
|
class Recipe(Base):
|
|
__tablename__ = "recipes"
|
|
|
|
id = Column(String, primary_key=True)
|
|
name = Column(String, nullable=False, index=True)
|
|
servings = Column(Integer, default=1)
|
|
items = Column(JSON, default=list) # [{"food": {...}, "grams", "portionLabel"}]
|
|
author_token = Column(String, nullable=True)
|
|
created_at = Column(DateTime, server_default=func.now())
|
|
|
|
def to_app(self) -> dict:
|
|
return {
|
|
"id": self.id,
|
|
"name": self.name,
|
|
"servings": self.servings,
|
|
"items": self.items or [],
|
|
}
|