Testys commited on
Commit
4ee0abb
1 Parent(s): a7a70f2

Upload 4 files

Browse files
Files changed (4) hide show
  1. orders/models.py +25 -0
  2. orders/routes.py +61 -0
  3. orders/schemas.py +33 -0
  4. orders/services.py +44 -0
orders/models.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # models.py
2
+ from sqlalchemy import Column, ForeignKey, Integer, String, Float, JSON, ARRAY
3
+ from sqlalchemy.orm import relationship
4
+ from core.database import Base
5
+
6
+ class Meal(Base):
7
+ __tablename__ = "meals"
8
+
9
+ id = Column(Integer, primary_key=True, index=True)
10
+ name = Column(String, index=True)
11
+ description = Column(String)
12
+ price = Column(Float)
13
+
14
+ orders = relationship("Order", back_populates="meal")
15
+
16
+ class Order(Base):
17
+ __tablename__ = "orders"
18
+
19
+ id = Column(Integer, primary_key=True, index=True)
20
+ user_id = Column(Integer, ForeignKey("users.id"))
21
+ meal_id = Column(Integer, ForeignKey("meals.id"))
22
+ quantity = Column(Integer)
23
+
24
+ user = relationship("User", back_populates="orders")
25
+ meal = relationship("Meal", back_populates="orders")
orders/routes.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter, status, Depends, HTTPException
2
+ from typing import List, Optional
3
+ from core.database import get_db
4
+ from core.security import get_current_user
5
+ from orders.services import create_meal, get_meals, get_meal, update_meal, delete_meal, create_user_order, get_user_orders
6
+ from orders.schemas import OrderCreate, OrderBase, Order, MealBase, MealCreate, MealUpdate, Meal
7
+ from sqlalchemy.orm import Session
8
+ from services.recommendation_service import MealRecommender
9
+
10
+
11
+ order_router = APIRouter(
12
+ prefix="/orders",
13
+ tags=["Orders"],
14
+ responses={404: {"description": "Not found"}, 401: {"description": "Unauthorized"}}
15
+ )
16
+
17
+ meal_router = APIRouter(
18
+ prefix="/meals",
19
+ tags=["Meals"],
20
+ responses={404: {"description": "Not found"}, 401: {"description": "Unauthorized"}}
21
+ )
22
+
23
+
24
+ @meal_router.get("/health", tags=["Health"])
25
+ def health_check():
26
+ return {"status": "ok"}
27
+
28
+ @meal_router.get("/", response_model=MealBase)
29
+ async def meal_create(data: MealCreate, db: Session = Depends(get_db)):
30
+ return create_meal(db, data)
31
+
32
+ @meal_router.get("/", response_model=List[MealBase])
33
+ def meals_get(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
34
+ return get_meals(db, skip=skip, limit=limit)
35
+
36
+ @meal_router.get("/{meal_id}", response_model=MealBase)
37
+ def meal_get(meal_id: int, db: Session = Depends(get_db)):
38
+ return get_meal(db, meal_id)
39
+
40
+ @meal_router.put("/{meal_id}", response_model=MealBase)
41
+ def meal_update(meal_id: int, data: MealUpdate, db: Session = Depends(get_db)):
42
+ return update_meal(db, meal_id, data)
43
+
44
+ @meal_router.delete("/{meal_id}", response_model=MealBase)
45
+ def meal_delete(meal_id: int, db: Session = Depends(get_db)):
46
+ return delete_meal(db, meal_id)
47
+
48
+ @order_router.post("/", response_model=OrderBase)
49
+ def create_order(order: OrderCreate, current_user: OrderBase = Depends(get_current_user), db: Session = Depends(get_db)):
50
+ return create_user_order(db, order, current_user.id)
51
+
52
+ @order_router.get("/", response_model=List[OrderBase])
53
+ def read_user_orders(skip: int = 0, limit: int = 100, current_user: OrderBase = Depends(get_current_user), db: Session = Depends(get_db)):
54
+ return get_user_orders(db, user_id=current_user.id, skip=skip, limit=limit)
55
+
56
+ @meal_router.get("/recommendations/", response_model=List[MealBase])
57
+ async def get_recommendations(current_user: OrderBase = Depends(get_current_user), db: Session = Depends(get_db)):
58
+ recommender = MealRecommender()
59
+ recommendations = recommender.get_recommendations(current_user)
60
+ return recommendations
61
+
orders/schemas.py CHANGED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pydantic import BaseModel
2
+
3
+
4
+ class MealBase(BaseModel):
5
+ name: str
6
+ description: str
7
+ price: float
8
+
9
+ class MealCreate(MealBase):
10
+ pass
11
+
12
+ class MealUpdate(MealBase):
13
+ pass
14
+
15
+ class Meal(MealBase):
16
+ id: int
17
+
18
+ class Config:
19
+ orm_mode = True
20
+
21
+ class OrderBase(BaseModel):
22
+ quantity: int
23
+
24
+ class OrderCreate(OrderBase):
25
+ meal_id: int
26
+
27
+ class Order(OrderBase):
28
+ id: int
29
+ user_id = int
30
+ meal_id = int
31
+
32
+ class Config:
33
+ orm_mode = True
orders/services.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sqlalchemy.orm import Session
2
+ from typing import List
3
+ from orders.schemas import MealCreate, MealUpdate, OrderCreate
4
+ from orders.models import Meal, Order
5
+
6
+ def get_meals(db: Session, skip: int = 0, limit: int = 100):
7
+ return db.query(Meal).offset(skip).limit(limit).all()
8
+
9
+ def create_meal(db: Session, meal: MealCreate):
10
+ db_meal = Meal(**meal.dict())
11
+ db.add(db_meal)
12
+ db.commit()
13
+ db.refresh(db_meal)
14
+ return db_meal
15
+
16
+ def get_meal(db: Session, meal_id: int):
17
+ return db.query(Meal).filter(Meal.id == meal_id).first()
18
+
19
+ def update_meal(db: Session, meal_id: int, meal: MealUpdate):
20
+ db_meal = db.query(Meal).filter(Meal.id == meal_id).first()
21
+ if db_meal:
22
+ update_data = meal.dict(exclude_unset=True)
23
+ for key, value in update_data.items():
24
+ setattr(db_meal, key, value)
25
+ db.commit()
26
+ db.refresh(db_meal)
27
+ return db_meal
28
+
29
+ def delete_meal(db: Session, meal_id: int):
30
+ db_meal = db.query(Meal).filter(Meal.id == meal_id).first()
31
+ if db_meal:
32
+ db.delete(db_meal)
33
+ db.commit()
34
+ return db_meal
35
+
36
+ def create_user_order(db: Session, order: OrderCreate, user_id: int):
37
+ db_order = Order(**order.dict(), user_id=user_id)
38
+ db.add(db_order)
39
+ db.commit()
40
+ db.refresh(db_order)
41
+ return db_order
42
+
43
+ def get_user_orders(db: Session, user_id: int, skip: int = 0, limit: int = 100):
44
+ return db.query(Order).filter(Order.user_id == user_id).offset(skip).limit(limit).all()