nagasurendra commited on
Commit
ba57252
·
verified ·
1 Parent(s): afb8c72

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -16
app.py CHANGED
@@ -64,37 +64,40 @@ def get_ingredients():
64
 
65
  @app.route('/get_menu_items', methods=['POST'])
66
  def get_menu_items():
67
- ingredient = request.json.get('ingredient', '').strip().lower()
68
- logging.debug(f"Received ingredient: {ingredient}")
69
 
70
- if not ingredient:
71
- logging.debug("No ingredient provided.")
72
- return jsonify({"error": "No ingredient provided."}), 400
73
 
74
- # Query to fetch menu items where the name contains the selected ingredient
75
- soql = f"""
76
- SELECT Name, Price__c, Description__c, Image1__c, Image2__c, Veg_NonVeg__c, Section__c, Total_Ordered__c
77
- FROM Menu_Item__c
78
- WHERE LOWER(Name) LIKE '%{ingredient}%'
79
- LIMIT 200
80
- """
81
 
82
  try:
 
83
  result = sf.query(soql)
 
84
  menu_items = [
85
- {"name": record['Name'], "price": record.get('Price__c', ''), "description": record.get('Description__c', ''),
86
- "image1": record.get('Image1__c', ''), "image2": record.get('Image2__c', ''),
87
- "veg_nonveg": record.get('Veg_NonVeg__c', ''), "section": record.get('Section__c', ''),
88
  "total_ordered": record.get('Total_Ordered__c', '')}
89
  for record in result['records'] if 'Name' in record
90
  ]
91
- logging.debug(f"Fetched {len(menu_items)} menu items.")
92
  return jsonify({"menu_items": menu_items})
 
93
  except Exception as e:
94
  logging.error(f"Error while fetching menu items: {str(e)}")
95
  return jsonify({"error": f"Failed to fetch menu items: {str(e)}"}), 500
96
 
97
 
 
98
  @app.route('/submit_ingredients', methods=['POST'])
99
  def submit_ingredients():
100
  data = request.json
 
64
 
65
  @app.route('/get_menu_items', methods=['POST'])
66
  def get_menu_items():
67
+ ingredient_names = request.json.get('ingredient_names', '').strip().lower()
68
+ logging.debug(f"Received ingredient names: {ingredient_names}")
69
 
70
+ # Constructing a SOQL query where Name contains any of the ingredient names
71
+ # Split the ingredient names into separate words to match each ingredient
72
+ ingredients_list = ingredient_names.split()
73
 
74
+ # Create a dynamic WHERE clause to match the ingredient names in the Menu Item names
75
+ condition = " OR ".join([f"Name LIKE '%{ingredient}%'" for ingredient in ingredients_list])
76
+
77
+ if not condition:
78
+ logging.debug("No ingredients received.")
79
+ return jsonify({"error": "No valid ingredients provided."}), 400
 
80
 
81
  try:
82
+ soql = f"SELECT Name, Price__c, Description__c, Image1__c, Image2__c, Veg_NonVeg__c, Section__c, Total_Ordered__c FROM Menu_Item__c WHERE {condition} LIMIT 200"
83
  result = sf.query(soql)
84
+
85
  menu_items = [
86
+ {"name": record['Name'], "price": record.get('Price__c', ''), "description": record.get('Description__c', ''),
87
+ "image_url1": record.get('Image1__c', ''), "image_url2": record.get('Image2__c', ''),
88
+ "veg_nonveg": record.get('Veg_NonVeg__c', ''), "section": record.get('Section__c', ''),
89
  "total_ordered": record.get('Total_Ordered__c', '')}
90
  for record in result['records'] if 'Name' in record
91
  ]
92
+ logging.debug(f"Fetched {len(menu_items)} menu items based on ingredients.")
93
  return jsonify({"menu_items": menu_items})
94
+
95
  except Exception as e:
96
  logging.error(f"Error while fetching menu items: {str(e)}")
97
  return jsonify({"error": f"Failed to fetch menu items: {str(e)}"}), 500
98
 
99
 
100
+
101
  @app.route('/submit_ingredients', methods=['POST'])
102
  def submit_ingredients():
103
  data = request.json