Subbu1304 commited on
Commit
633c3b7
·
verified ·
1 Parent(s): 81e586c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -29
app.py CHANGED
@@ -1,33 +1,57 @@
1
  from flask import Flask, jsonify, request
2
- from datetime import datetime, timedelta
 
3
 
4
  app = Flask(__name__)
 
5
 
6
- # Sample data for tables
7
- tables = {
8
- 1: {"status": "Available", "available_time": None},
9
- 2: {"status": "Reserved", "available_time": datetime.now() + timedelta(minutes=10)},
10
- 3: {"status": "Reserved", "available_time": datetime.now() + timedelta(minutes=20)},
11
- }
12
-
13
- @app.route('/get_table_status/<int:table_id>', methods=['GET'])
14
- def get_table_status(table_id):
15
- if table_id in tables:
16
- table = tables[table_id]
17
- if table['status'] == 'Reserved' and table['available_time']:
18
- available_in = (table['available_time'] - datetime.now()).seconds // 60
19
- return jsonify({
20
- "status": "Reserved",
21
- "available_in": available_in,
22
- "message": f"Table {table_id} will be available in {available_in} minutes."
23
- })
24
- else:
25
- return jsonify({
26
- "status": "Available",
27
- "message": f"Table {table_id} is available."
28
- })
29
- else:
30
- return jsonify({"error": "Table not found"}), 404
31
-
32
- if __name__ == '__main__':
33
- app.run(debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from flask import Flask, jsonify, request
2
+ from flask_cors import CORS
3
+ import datetime
4
 
5
  app = Flask(__name__)
6
+ CORS(app) # Enable CORS for Hugging Face compatibility
7
 
8
+ # In-memory storage for table bookings
9
+ bookings = {} # {table_id: {time_slot: booked}}
10
+
11
+ # Available time slots (12 PM to 10 PM)
12
+ time_slots = [f"{hour}:00" for hour in range(12, 23)]
13
+
14
+ # Initialize bookings for 5 tables
15
+ for table_id in range(1, 6):
16
+ bookings[table_id] = {}
17
+
18
+ @app.route("/api/availability/<int:table_id>", methods=["GET"])
19
+ def get_availability(table_id):
20
+ if table_id not in bookings:
21
+ return jsonify({"error": "Invalid table ID"}), 404
22
+
23
+ # Get current hour (for live time comparison)
24
+ current_hour = datetime.datetime.now().hour
25
+ booked_slots = bookings[table_id].keys()
26
+ available_slots = [slot for slot in time_slots if slot not in booked_slots and int(slot.split(":")[0]) >= current_hour]
27
+
28
+ # Find next available slot
29
+ next_slot = available_slots[0] if available_slots else None
30
+
31
+ return jsonify({
32
+ "table_id": table_id,
33
+ "available_slots": available_slots,
34
+ "next_slot": next_slot
35
+ })
36
+
37
+ @app.route("/api/book", methods=["POST"])
38
+ def book_table():
39
+ data = request.get_json()
40
+ table_id = data.get("table_id")
41
+ time_slot = data.get("time_slot")
42
+
43
+ if not table_id or not time_slot:
44
+ return jsonify({"success": False, "message": "Missing table_id or time_slot"}), 400
45
+
46
+ if table_id not in bookings:
47
+ return jsonify({"success": False, "message": "Invalid table ID"}), 404
48
+
49
+ if time_slot in bookings[table_id]:
50
+ return jsonify({"success": False, "message": "Time slot already booked"}), 400
51
+
52
+ # Book the slot
53
+ bookings[table_id][time_slot] = True
54
+ return jsonify({"success": True, "message": "Table booked successfully"})
55
+
56
+ if __name__ == "__main__":
57
+ app.run(debug=True, host="0.0.0.0", port=5000)