Add barcode scanning and nutrition lookup feature

Introduces a new /scan route and template for barcode scanning using ZXing in the browser. Adds a /nutri/<barcode> API endpoint to fetch food item nutrition data by barcode. Updates the FoodItem model to include a barcode field and a to_dict method for JSON serialization. Also updates seed data to include a barcode.
This commit is contained in:
2025-06-29 00:08:25 +02:00
parent d5e8c3fa94
commit 0919048cfd
4 changed files with 148 additions and 6 deletions

View File

@@ -36,6 +36,7 @@ class Unit(db.Model):
class FoodItem(db.Model):
__tablename__ = "food_item"
id = db.Column(db.Integer, primary_key=True)
barcode = db.Column(db.Integer)
name = db.Column(db.String(150), unique=True, nullable=False)
energy_100g = db.Column(db.Integer, nullable=False)
@@ -52,8 +53,9 @@ class FoodItem(db.Model):
protein: float,
carbs: int,
fats: int,
sugar: Optional[int] = False,
saturated_fats: Optional[int] = False,
sugar: Optional[int] = None,
saturated_fats: Optional[int] = None,
barcode: Optional[int] = None,
):
self.name = name
self.energy_100g = energy
@@ -62,3 +64,17 @@ class FoodItem(db.Model):
self.sugar_100g = sugar
self.fats_100g = fats
self.saturated_fats_100g = saturated_fats
self.barcode = barcode
def to_dict(self):
return {
"id": self.id,
"barcode": self.barcode,
"name": self.name,
"energy_100g": self.energy_100g,
"protein_100g": self.protein_100g,
"carbs_100g": self.carbs_100g,
"sugar_100g": self.sugar_100g,
"fats_100g": self.fats_100g,
"saturated_fats_100g": self.saturated_fats_100g,
}