|
from fastapi import APIRouter, status, Depends, HTTPException |
|
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 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)): |
|
return create_meal(db, data) |
|
|
|
|
|
@meal_router.get("/", response_model=List[MealBase]) |
|
def meals_get(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): |
|
return get_meals(db, skip=skip, limit=limit) |
|
|
|
|
|
@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 meal |
|
|
|
|
|
@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 meal |
|
|
|
|
|
@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)): |
|
return create_user_order(db, order, current_user.id) |
|
|
|
|
|
@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)): |
|
return get_user_orders(db, current_user.id, skip=skip, limit=limit) |
|
|
|
|
|
@meal_router.get("/recommendations/", response_model=List[MealBase]) |
|
async def get_recommendations(current_user: OrderBase = Depends(get_current_user), db: Session = Depends(get_db)): |
|
recommender = MealRecommender() |
|
recommendations = recommender.get_recommendations(current_user) |
|
return recommendations |
|
|
|
|