|
from flask import Flask, jsonify, request |
|
from flask_cors import CORS |
|
import datetime |
|
|
|
app = Flask(__name__) |
|
CORS(app) |
|
|
|
|
|
bookings = {} |
|
|
|
|
|
time_slots = [f"{hour}:00" for hour in range(12, 23)] |
|
|
|
|
|
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 |
|
|
|
|
|
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] |
|
|
|
|
|
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 |
|
|
|
|
|
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) |