File size: 1,753 Bytes
9aaec2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session
from database import SessionLocal
from models import InventoryItem, Recipe, Task
import json
from typing import Dict, Any

router = APIRouter()

def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

@router.get("/data")
async def get_all_data(db: Session = Depends(get_db)):
    """Get all data for frontend synchronization - matches original backend format"""
    
    # Get inventory
    inventory_items = db.query(InventoryItem).all()
    inventory = [
        {
            "id": item.id,
            "name": item.name,
            "unit": item.unit,
            "quantity": item.quantity,
            "category": item.category,
            "price": item.price,
            "lot_number": item.lot_number,
            "expiry_date": item.expiry_date.isoformat() if item.expiry_date else None
        }
        for item in inventory_items
    ]
    
    # Get recipes
    recipe_items = db.query(Recipe).all()
    recipes = {}
    for recipe in recipe_items:
        items_data = json.loads(recipe.items) if recipe.items else []
        yield_data = json.loads(recipe.yield_data) if recipe.yield_data else None
        recipes[recipe.name] = {
            "items": items_data,
            "yield": yield_data
        }
    
    # Get tasks
    task_items = db.query(Task).all()
    tasks = [
        {
            "id": task.id,
            "recipe": task.recipe,
            "quantity": task.quantity,
            "assignedTo": task.assigned_to,
            "status": task.status
        }
        for task in task_items
    ]
    
    return {
        "inventory": inventory,
        "recipes": recipes,
        "tasks": tasks
    }