tables / app.py
Subbu1304's picture
Update app.py
633c3b7 verified
from flask import Flask, jsonify, request
from flask_cors import CORS
import datetime
app = Flask(__name__)
CORS(app) # Enable CORS for Hugging Face compatibility
# In-memory storage for table bookings
bookings = {} # {table_id: {time_slot: booked}}
# Available time slots (12 PM to 10 PM)
time_slots = [f"{hour}:00" for hour in range(12, 23)]
# Initialize bookings for 5 tables
for table_id in range(1, 6):
bookings[table_id] = {}
@app.route("/api/availability/<int:table_id>", methods=["GET"])
def get_availability(table_id):
if table_id not in bookings:
return jsonify({"error": "Invalid table ID"}), 404
# Get current hour (for live time comparison)
current_hour = datetime.datetime.now().hour
booked_slots = bookings[table_id].keys()
available_slots = [slot for slot in time_slots if slot not in booked_slots and int(slot.split(":")[0]) >= current_hour]
# Find next available slot
next_slot = available_slots[0] if available_slots else None
return jsonify({
"table_id": table_id,
"available_slots": available_slots,
"next_slot": next_slot
})
@app.route("/api/book", methods=["POST"])
def book_table():
data = request.get_json()
table_id = data.get("table_id")
time_slot = data.get("time_slot")
if not table_id or not time_slot:
return jsonify({"success": False, "message": "Missing table_id or time_slot"}), 400
if table_id not in bookings:
return jsonify({"success": False, "message": "Invalid table ID"}), 404
if time_slot in bookings[table_id]:
return jsonify({"success": False, "message": "Time slot already booked"}), 400
# Book the slot
bookings[table_id][time_slot] = True
return jsonify({"success": True, "message": "Table booked successfully"})
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=5000)