Testys commited on
Commit
91aa74e
1 Parent(s): 9f525f5

After testing, making improvement to avoid same meal to be created more than once

Browse files
Files changed (2) hide show
  1. orders/routes.py +18 -5
  2. orders/services.py +2 -0
orders/routes.py CHANGED
@@ -40,15 +40,21 @@ async def meal_create(data: MealCreate, db: Session = Depends(get_db)):
40
 
41
  @meal_router.get("/", response_model=List[MealBase])
42
  def meals_get(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
43
- return get_meals(db, skip=skip, limit=limit)
44
-
 
 
 
45
 
46
  @meal_router.get("/{meal_id}", response_model=MealBase)
47
  def meal_get(meal_id: int, db: Session = Depends(get_db)):
48
  meal = get_meal(db, meal_id)
49
  if not meal:
50
  raise HTTPException(status_code=404, detail="Meal not found")
51
- return meal
 
 
 
52
 
53
 
54
  @meal_router.put("/{meal_id}", response_model=MealBase)
@@ -56,7 +62,10 @@ def meal_update(meal_id: int, data: MealUpdate, db: Session = Depends(get_db)):
56
  meal = update_meal(db, meal_id, data)
57
  if not meal:
58
  raise HTTPException(status_code=404, detail="Meal not found")
59
- return meal
 
 
 
60
 
61
 
62
  @meal_router.delete("/{meal_id}", response_model=MealBase)
@@ -78,7 +87,11 @@ def create_order(order: OrderCreate, current_user: OrderBase = Depends(get_curre
78
 
79
  @order_router.get("/", response_model=List[OrderBase])
80
  def get_orders(current_user: OrderBase = Depends(get_current_user), skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
81
- return get_user_orders(db, current_user.id, skip=skip, limit=limit)
 
 
 
 
82
 
83
 
84
  @meal_router.get("/recommendations/", response_model=List[MealBase])
 
40
 
41
  @meal_router.get("/", response_model=List[MealBase])
42
  def meals_get(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
43
+ meals = get_meals(db, skip=skip, limit=limit)
44
+ return JSONResponse(
45
+ content=[{"id": meal.id, "name": meal.name} for meal in meals],
46
+ status_code=status.HTTP_200_OK
47
+ )
48
 
49
  @meal_router.get("/{meal_id}", response_model=MealBase)
50
  def meal_get(meal_id: int, db: Session = Depends(get_db)):
51
  meal = get_meal(db, meal_id)
52
  if not meal:
53
  raise HTTPException(status_code=404, detail="Meal not found")
54
+ return JSONResponse(
55
+ content={"id": meal.id, "name": meal.name},
56
+ status_code=status.HTTP_200_OK
57
+ )
58
 
59
 
60
  @meal_router.put("/{meal_id}", response_model=MealBase)
 
62
  meal = update_meal(db, meal_id, data)
63
  if not meal:
64
  raise HTTPException(status_code=404, detail="Meal not found")
65
+ return JSONResponse(
66
+ content={"id": meal.id, "name": meal.name},
67
+ status_code=status.HTTP_200_OK
68
+ )
69
 
70
 
71
  @meal_router.delete("/{meal_id}", response_model=MealBase)
 
87
 
88
  @order_router.get("/", response_model=List[OrderBase])
89
  def get_orders(current_user: OrderBase = Depends(get_current_user), skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
90
+ orders = get_user_orders(db, current_user.id, skip=skip, limit=limit)
91
+ return JSONResponse(
92
+ content=[{"id": order.id, "meal_id": order.meal_id, "quantity": order.quantity} for order in orders],
93
+ status_code=status.HTTP_200_OK
94
+ )
95
 
96
 
97
  @meal_router.get("/recommendations/", response_model=List[MealBase])
orders/services.py CHANGED
@@ -8,6 +8,8 @@ def get_meals(db: Session, skip: int = 0, limit: int = 100) -> List[Meal]:
8
 
9
  def create_meal(db: Session, meal: MealCreate) -> Meal:
10
  db_meal = Meal(**meal.dict())
 
 
11
  db.add(db_meal)
12
  db.commit()
13
  db.refresh(db_meal)
 
8
 
9
  def create_meal(db: Session, meal: MealCreate) -> Meal:
10
  db_meal = Meal(**meal.dict())
11
+ if db.query(Meal).filter(Meal.name == db_meal.name).first():
12
+ raise ValueError("Meal with this name already exists")
13
  db.add(db_meal)
14
  db.commit()
15
  db.refresh(db_meal)