File size: 1,884 Bytes
10e74fe
633c3b7
 
10e74fe
 
633c3b7
10e74fe
633c3b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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)