from fastapi import APIRouter, status, Depends, BackgroundTasks, HTTPException from fastapi.responses import JSONResponse from typing import List, Optional from core.database import get_db from core.security import get_current_user from orders.services import create_meal, get_meals, get_meal, update_meal, delete_meal, create_user_order, get_user_orders from orders.schemas import OrderCreate, OrderBase, Order, MealBase, MealCreate, MealUpdate, Meal from users.schemas import User from sqlalchemy.orm import Session from services.recommendation_service import MealRecommender order_router = APIRouter( prefix="/orders", tags=["Orders"], responses={404: {"description": "Not found"}, 401: {"description": "Unauthorized"}} ) meal_router = APIRouter( prefix="/meals", tags=["Meals"], responses={404: {"description": "Not found"}, 401: {"description": "Unauthorized"}} ) @meal_router.get("/health", tags=["Health"]) def health_check(): return {"status": "ok"} @meal_router.post("/", response_model=MealBase, status_code=status.HTTP_201_CREATED) async def meal_create(data: MealCreate, db: Session = Depends(get_db)): new_meal = create_meal(db, data) id, name = new_meal.id, new_meal.name return JSONResponse( content={"id": id, "name": name}, status_code=status.HTTP_200_OK ) @meal_router.get("/", response_model=List[MealBase]) def meals_get(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): meals = get_meals(db, skip=skip, limit=limit) return JSONResponse( content=[{"id": meal.id, "name": meal.name} for meal in meals], status_code=status.HTTP_200_OK ) @meal_router.get("/{meal_id}", response_model=MealBase) def meal_get(meal_id: int, db: Session = Depends(get_db)): meal = get_meal(db, meal_id) if not meal: raise HTTPException(status_code=404, detail="Meal not found") return JSONResponse( content={"id": meal.id, "name": meal.name}, status_code=status.HTTP_200_OK ) @meal_router.put("/{meal_id}", response_model=MealBase) def meal_update(meal_id: int, data: MealUpdate, db: Session = Depends(get_db)): meal = update_meal(db, meal_id, data) if not meal: raise HTTPException(status_code=404, detail="Meal not found") return JSONResponse( content={"id": meal.id, "name": meal.name}, status_code=status.HTTP_200_OK ) @meal_router.delete("/{meal_id}", response_model=MealBase) def meal_delete(meal_id: int, db: Session = Depends(get_db)): meal = delete_meal(db, meal_id) if not meal: raise HTTPException(status_code=404, detail="Meal not found") return meal @order_router.post("/", response_model=OrderBase, status_code=status.HTTP_201_CREATED) def create_order(order: OrderCreate, current_user: OrderBase = Depends(get_current_user), db: Session = Depends(get_db)): new_order = create_user_order(db, order, current_user.id) return JSONResponse( content={"id": new_order.id, "meal_id": new_order.meal_id, "quantity": new_order.quantity}, status_code=status.HTTP_200_OK ) @order_router.get("/", response_model=List[OrderBase]) def get_orders(current_user: OrderBase = Depends(get_current_user), skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): orders = get_user_orders(db, current_user.id, skip=skip, limit=limit) return JSONResponse( content=[{"id": order.id, "meal_id": order.meal_id, "quantity": order.quantity} for order in orders], status_code=status.HTTP_200_OK ) @meal_router.get("/recommendations/", response_model=List[MealBase]) async def get_recommendations( background_tasks: BackgroundTasks, current_user: User = Depends(get_current_user), db: Session = Depends(get_db) ): recommender = MealRecommender(db) recommendations = recommender.get_recommendations(current_user) # Schedule model retraining in the background background_tasks.add_task(recommender.train_model) return recommendations