lokesh341 commited on
Commit
9d5c08a
Β·
verified Β·
1 Parent(s): d643e03

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -67
app.py CHANGED
@@ -14,22 +14,9 @@ import requests
14
 
15
  # Initialize Flask app
16
  app = Flask(__name__)
17
- app.secret_key = os.urandom(24) # For session handling
18
 
19
- # βœ… Enable Debug Mode
20
- app.config["DEBUG"] = True
21
-
22
- # βœ… Salesforce Connection Setup
23
- try:
24
- print("Attempting to connect to Salesforce...")
25
- sf = Salesforce(username='[email protected]',
26
- password='Sati@1020',
27
- security_token='sSSjyhInIsUohKpG8sHzty2q')
28
- print("βœ… Connected to Salesforce successfully!")
29
- except Exception as e:
30
- print(f"❌ Failed to connect to Salesforce: {str(e)}")
31
-
32
- # βœ… Print Available Routes on First Request
33
  @app.before_request
34
  def print_routes():
35
  if not hasattr(app, 'printed_routes'):
@@ -46,75 +33,36 @@ def list_routes():
46
  routes.append({"endpoint": rule.endpoint, "route": str(rule)})
47
  return jsonify({"available_routes": routes})
48
 
 
 
 
 
49
 
50
- # βœ… REGISTER API: Create a new Customer in Salesforce
51
  @app.route("/register", methods=["POST"])
52
  def register():
53
  print("➑ Register API hit")
54
  data = request.json
55
- name = data.get("name")
56
- email = data.get("email")
57
- phone_number = data.get("phone")
58
-
59
- if not name or not email or not phone_number:
60
  return jsonify({"error": "Missing required fields"}), 400
 
61
 
62
- try:
63
- new_customer = sf.Customer_Login__c.create({
64
- 'Name': name,
65
- 'Email__c': email,
66
- 'Phone_Number__c': phone_number
67
- })
68
- return jsonify({"success": True, "message": "Registration successful", "customer_id": new_customer["id"]})
69
- except Exception as e:
70
- return jsonify({"error": f"Failed to register: {str(e)}"}), 500
71
-
72
- # βœ… LOGIN API: Validate user credentials
73
  @app.route("/login", methods=["POST"])
74
  def login():
75
  print("➑ Login API hit")
76
  data = request.json
77
- email = data.get("email")
78
- phone_number = data.get("phone")
79
-
80
- if not email or not phone_number:
81
  return jsonify({"error": "Missing email or phone"}), 400
 
82
 
83
- try:
84
- query = f"SELECT Id, Name FROM Customer_Login__c WHERE Email__c = '{email}' AND Phone_Number__c = '{phone_number}'"
85
- result = sf.query(query)
86
-
87
- if result["totalSize"] > 0:
88
- user = result["records"][0]
89
- session["customer_id"] = user["Id"] # Store in session
90
- return jsonify({"success": True, "message": "Login successful", "customer_id": user["Id"], "name": user["Name"]})
91
- else:
92
- return jsonify({"error": "Invalid login credentials"}), 401
93
- except Exception as e:
94
- return jsonify({"error": f"Login failed: {str(e)}"}), 500
95
-
96
- # βœ… MENU API: Fetch menu items from Salesforce
97
  @app.route("/menu", methods=["GET"])
98
  def get_menu():
99
  print("➑ Menu API hit")
100
- try:
101
- query = "SELECT Name, Price__c, Ingredients__c, Category__c FROM Menu_Item__c"
102
- result = sf.query(query)
103
-
104
- menu_items = []
105
- for item in result["records"]:
106
- menu_items.append({
107
- "name": item["Name"],
108
- "price": item["Price__c"],
109
- "ingredients": item["Ingredients__c"],
110
- "category": item["Category__c"]
111
- })
112
-
113
- return jsonify({"success": True, "menu": menu_items})
114
- except Exception as e:
115
- return jsonify({"error": f"Failed to fetch menu: {str(e)}"}), 500
116
 
117
- # βœ… START PRODUCTION SERVER
118
  if __name__ == "__main__":
119
  print("βœ… Starting Flask API Server on port 7860...")
120
  serve(app, host="0.0.0.0", port=7860)
 
14
 
15
  # Initialize Flask app
16
  app = Flask(__name__)
17
+ app.secret_key = os.urandom(24)
18
 
19
+ # βœ… Print Available Routes at Startup
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  @app.before_request
21
  def print_routes():
22
  if not hasattr(app, 'printed_routes'):
 
33
  routes.append({"endpoint": rule.endpoint, "route": str(rule)})
34
  return jsonify({"available_routes": routes})
35
 
36
+ # βœ… HOME ROUTE
37
+ @app.route("/", methods=["GET"])
38
+ def home():
39
+ return jsonify({"message": "Welcome to Biryani Hub API. Use /register, /login, /menu, or /routes to check available endpoints."})
40
 
41
+ # βœ… REGISTER API
42
  @app.route("/register", methods=["POST"])
43
  def register():
44
  print("➑ Register API hit")
45
  data = request.json
46
+ if not data or "name" not in data or "email" not in data or "phone" not in data:
 
 
 
 
47
  return jsonify({"error": "Missing required fields"}), 400
48
+ return jsonify({"success": True, "message": "Registration successful"})
49
 
50
+ # βœ… LOGIN API
 
 
 
 
 
 
 
 
 
 
51
  @app.route("/login", methods=["POST"])
52
  def login():
53
  print("➑ Login API hit")
54
  data = request.json
55
+ if not data or "email" not in data or "phone" not in data:
 
 
 
56
  return jsonify({"error": "Missing email or phone"}), 400
57
+ return jsonify({"success": True, "message": "Login successful"})
58
 
59
+ # βœ… MENU API
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  @app.route("/menu", methods=["GET"])
61
  def get_menu():
62
  print("➑ Menu API hit")
63
+ return jsonify({"success": True, "menu": [{"name": "Biryani", "price": 10}]})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
+ # βœ… START SERVER
66
  if __name__ == "__main__":
67
  print("βœ… Starting Flask API Server on port 7860...")
68
  serve(app, host="0.0.0.0", port=7860)