lokesh341 commited on
Commit
c1d8f2a
Β·
verified Β·
1 Parent(s): 3480da8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -10
app.py CHANGED
@@ -12,7 +12,7 @@ from waitress import serve
12
  from simple_salesforce import Salesforce
13
  import requests
14
 
15
- # Initialize Flask app
16
  app = Flask(__name__, template_folder="templates")
17
  app.secret_key = os.urandom(24)
18
 
@@ -29,6 +29,15 @@ try:
29
  except Exception as e:
30
  print(f"❌ Failed to connect to Salesforce: {str(e)}")
31
 
 
 
 
 
 
 
 
 
 
32
  # βœ… ROUTE: List All Routes for Debugging
33
  @app.route("/routes", methods=["GET"])
34
  def list_routes():
@@ -40,38 +49,79 @@ def list_routes():
40
  # βœ… HOME ROUTE
41
  @app.route("/", methods=["GET"])
42
  def home():
43
- return jsonify({"message": "Welcome to Biryani Hub API. Use /register, /login, /menu, /index, or /routes to check available endpoints."})
44
 
45
- # βœ… RENDER INDEX.HTML FOR REGISTER & LOGIN
46
  @app.route("/index", methods=["GET"])
47
  def index_page():
48
  return render_template("index.html")
49
 
50
- # βœ… REGISTER API
 
 
 
 
 
51
  @app.route("/register", methods=["POST"])
52
  def register():
53
  print("➑ Register API hit")
54
  data = request.json
55
  if not data or "name" not in data or "email" not in data or "phone" not in data:
56
  return jsonify({"error": "Missing required fields"}), 400
57
- return jsonify({"success": True, "message": "Registration successful"})
58
 
59
- # βœ… LOGIN API
 
 
 
 
 
 
 
 
 
 
60
  @app.route("/login", methods=["POST"])
61
  def login():
62
  print("➑ Login API hit")
63
  data = request.json
64
  if not data or "email" not in data or "phone" not in data:
65
  return jsonify({"error": "Missing email or phone"}), 400
66
- return jsonify({"success": True, "message": "Login successful"})
67
 
68
- # βœ… MENU API
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  @app.route("/menu", methods=["GET"])
70
  def get_menu():
71
  print("➑ Menu API hit")
72
- return jsonify({"success": True, "menu": [{"name": "Biryani", "price": 10}]})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
- # βœ… START SERVER
75
  if __name__ == "__main__":
76
  print("βœ… Starting Flask API Server on port 7860...")
77
  serve(app, host="0.0.0.0", port=7860)
 
12
  from simple_salesforce import Salesforce
13
  import requests
14
 
15
+ # βœ… Initialize Flask App
16
  app = Flask(__name__, template_folder="templates")
17
  app.secret_key = os.urandom(24)
18
 
 
29
  except Exception as e:
30
  print(f"❌ Failed to connect to Salesforce: {str(e)}")
31
 
32
+ # βœ… Print All Available Routes at Startup
33
+ @app.before_request
34
+ def print_routes():
35
+ if not hasattr(app, 'printed_routes'):
36
+ app.printed_routes = True
37
+ print("\nβœ… Available Routes:")
38
+ for rule in app.url_map.iter_rules():
39
+ print(f"➑ {rule}")
40
+
41
  # βœ… ROUTE: List All Routes for Debugging
42
  @app.route("/routes", methods=["GET"])
43
  def list_routes():
 
49
  # βœ… HOME ROUTE
50
  @app.route("/", methods=["GET"])
51
  def home():
52
+ return jsonify({"message": "Welcome to Biryani Hub API. Use /register, /login, /menu, /index, /dashboard, or /routes to check available endpoints."})
53
 
54
+ # βœ… RENDER INDEX.HTML (Register & Login Page)
55
  @app.route("/index", methods=["GET"])
56
  def index_page():
57
  return render_template("index.html")
58
 
59
+ # βœ… RENDER DASHBOARD
60
+ @app.route("/dashboard", methods=["GET"])
61
+ def dashboard():
62
+ return render_template("dashboard.html")
63
+
64
+ # βœ… REGISTER API: Create a new Customer in Salesforce
65
  @app.route("/register", methods=["POST"])
66
  def register():
67
  print("➑ Register API hit")
68
  data = request.json
69
  if not data or "name" not in data or "email" not in data or "phone" not in data:
70
  return jsonify({"error": "Missing required fields"}), 400
 
71
 
72
+ try:
73
+ new_customer = sf.Customer_Login__c.create({
74
+ 'Name': data["name"],
75
+ 'Email__c': data["email"],
76
+ 'Phone_Number__c': data["phone"]
77
+ })
78
+ return jsonify({"success": True, "message": "Registration successful", "customer_id": new_customer["id"]})
79
+ except Exception as e:
80
+ return jsonify({"error": f"Failed to register: {str(e)}"}), 500
81
+
82
+ # βœ… LOGIN API: Validate User Credentials
83
  @app.route("/login", methods=["POST"])
84
  def login():
85
  print("➑ Login API hit")
86
  data = request.json
87
  if not data or "email" not in data or "phone" not in data:
88
  return jsonify({"error": "Missing email or phone"}), 400
 
89
 
90
+ try:
91
+ query = f"SELECT Id, Name FROM Customer_Login__c WHERE Email__c = '{data['email']}' AND Phone_Number__c = '{data['phone']}'"
92
+ result = sf.query(query)
93
+
94
+ if result["totalSize"] > 0:
95
+ user = result["records"][0]
96
+ session["customer_id"] = user["Id"] # Store in session
97
+ return jsonify({"success": True, "message": "Login successful", "customer_id": user["Id"], "name": user["Name"]})
98
+ else:
99
+ return jsonify({"error": "Invalid login credentials"}), 401
100
+ except Exception as e:
101
+ return jsonify({"error": f"Login failed: {str(e)}"}), 500
102
+
103
+ # βœ… MENU API: Fetch Menu Items from Salesforce
104
  @app.route("/menu", methods=["GET"])
105
  def get_menu():
106
  print("➑ Menu API hit")
107
+ try:
108
+ query = "SELECT Name, Price__c, Ingredients__c, Category__c FROM Menu_Item__c"
109
+ result = sf.query(query)
110
+
111
+ menu_items = []
112
+ for item in result["records"]:
113
+ menu_items.append({
114
+ "name": item["Name"],
115
+ "price": item["Price__c"],
116
+ "ingredients": item["Ingredients__c"],
117
+ "category": item["Category__c"]
118
+ })
119
+
120
+ return jsonify({"success": True, "menu": menu_items})
121
+ except Exception as e:
122
+ return jsonify({"error": f"Failed to fetch menu: {str(e)}"}), 500
123
 
124
+ # βœ… START PRODUCTION SERVER
125
  if __name__ == "__main__":
126
  print("βœ… Starting Flask API Server on port 7860...")
127
  serve(app, host="0.0.0.0", port=7860)