Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,57 @@
|
|
| 1 |
from flask import Flask, jsonify, request
|
| 2 |
-
from
|
|
|
|
| 3 |
|
| 4 |
app = Flask(__name__)
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|