File size: 2,921 Bytes
4ee0abb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2333542
 
4ee0abb
 
 
2333542
4ee0abb
 
 
 
2333542
4ee0abb
 
2333542
 
 
 
 
4ee0abb
 
 
2333542
 
 
 
 
4ee0abb
 
 
2333542
 
 
 
 
4ee0abb
2333542
4ee0abb
 
 
2333542
4ee0abb
2333542
 
 
4ee0abb
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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