Skip to content

Commit 4553380

Browse files
authored
fixed the time, synced admin and user areamap, extend time, terminate… (#44)
… time(the terminate deletes from the table in sql) so sales ledger affected assuming that the reservation is refunded
2 parents 5c42226 + 29d78b2 commit 4553380

File tree

9 files changed

+443
-214
lines changed

9 files changed

+443
-214
lines changed

backend/app/app.py

Lines changed: 194 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,83 @@ def perform_warehouse_process():
233233
# Add the scheduled job to run the ETL process every 168 hours or 1 week
234234
scheduler.add_job(perform_warehouse_process, 'interval', hours=168)
235235

236+
def get_reservation_by_seat(seat):
237+
connection = get_db_connection(db_config)
238+
if connection:
239+
try:
240+
cursor = connection.cursor(dictionary=True)
241+
query = """
242+
SELECT Reservations.ReservationID, Users.Username, Reservations.StartTime, Reservations.EndTime, Reservations.Seat, Reservations.TableFee, Reservations.ResDate
243+
FROM Reservations
244+
JOIN Users ON Reservations.UserID = Users.UserID
245+
WHERE Reservations.Seat = %s
246+
LIMIT 1
247+
248+
"""
249+
cursor.execute (query, (seat,))
250+
result = cursor.fetchone()
251+
cursor.close()
252+
connection.close()
253+
return result
254+
except mysql.connector.Error as err:
255+
print(f"Error fetching reservation by seat: {err}")
256+
return None
257+
258+
def update_reservation_endtime(chair_id, endtime):
259+
connection = get_db_connection(db_config)
260+
try:
261+
cursor = connection.cursor()
262+
current_reservation = get_reservation_by_seat(chair_id)
263+
if current_reservation:
264+
query = """
265+
UPDATE Reservations
266+
SET EndTime = %s
267+
WHERE Seat = %s
268+
"""
269+
values = (endtime, chair_id)
270+
cursor.execute(query, values)
271+
connection.commit()
272+
cursor.close()
273+
connection.close()
274+
return {'message': 'Reservation updated successfully'}
275+
else:
276+
return {'error': 'Reservation not found for the specified seat'}
277+
except mysql.connector.Error as err:
278+
print(f"Error updating reservation: {err}")
279+
connection.rollback()
280+
return {'error': f"Error updating reservation: {err}"}
281+
282+
def remove_reservation(chair_id):
283+
connection = get_db_connection(db_config)
284+
try:
285+
cursor = connection.cursor()
286+
current_reservation = get_reservation_by_seat(chair_id)
287+
if current_reservation:
288+
query = """
289+
DELETE FROM Reservations
290+
WHERE Seat = %s
291+
"""
292+
values = (chair_id,)
293+
cursor.execute(query, values)
294+
connection.commit()
295+
cursor.close()
296+
connection.close()
297+
return {'message': 'Reservation removed successfully'}
298+
else:
299+
return {'error': 'Reservation not found for the specified seat'}
300+
except mysql.connector.Error as err:
301+
print(f"Error removing reservation: {err}")
302+
connection.rollback()
303+
return {'error': f"Error removing reservation: {err}"}
304+
305+
def is_overlapping(start_time, end_time, e_start_time, e_end_time):
306+
start_time = datetime.strptime(start_time, '%H:%M:%S')
307+
end_time = datetime.strptime(end_time, '%H:%M:%S')
308+
e_end_time = datetime.strptime(e_end_time, '%H:%M:%S')
309+
e_start_time = datetime.strptime(e_start_time, '%H:%M:%S')
310+
return start_time < e_end_time and e_start_time < end_time
311+
312+
236313
def create_reservation(user_id, reservation_data):
237314
connection = get_db_connection(db_config)
238315
if connection:
@@ -294,6 +371,110 @@ def get_all_waitlist_entries():
294371
print(f"Error fetching waitlist entries: {err}")
295372
return None
296373

374+
def move_to_completed_reservations(reservation_id):
375+
connection = get_db_connection(db_config)
376+
if connection:
377+
try:
378+
cursor = connection.cursor()
379+
380+
query = """
381+
SELECT * FROM Reservations WHERE ReservationID = %s
382+
"""
383+
cursor.execute(query, (reservation_id,))
384+
reservation = cursor.fetchone()
385+
386+
if reservation:
387+
# Convert tuple to dictionary for easier access
388+
reservation_dict = dict(zip(cursor.column_names, reservation))
389+
390+
insert_query = """
391+
INSERT INTO Completed_Reservations(ReservationID, UserID, StartTime, EndTime, Seat, TableFee, ResDate)
392+
VALUES (%s, %s, %s, %s, %s, %s, %s)
393+
"""
394+
values = (
395+
reservation_dict['ReservationID'], reservation_dict['UserID'], reservation_dict['StartTime'], reservation_dict['EndTime'], reservation_dict['Seat'], reservation_dict['TableFee'], reservation_dict['ResDate']
396+
)
397+
cursor.execute(insert_query, values)
398+
399+
delete_query = """
400+
DELETE FROM Reservations WHERE ReservationID = %s
401+
"""
402+
cursor.execute(delete_query, (reservation_id,))
403+
404+
connection.commit()
405+
cursor.close()
406+
connection.close()
407+
408+
print("Reservation moved to completed reservation")
409+
except mysql.connector.Error as err:
410+
print(f"Error moving reservation to completed reservation {err}")
411+
connection.rollback()
412+
413+
414+
@app.route('/api/check-reservations-end', methods=['POST'])
415+
def check_reservation_end_route():
416+
try:
417+
current_time = request.json.get('current_time', None)
418+
curr = datetime.strptime(current_time, '%H:%M:%S')
419+
420+
query = """
421+
SELECT * FROM Reservations WHERE EndTime <= %s
422+
"""
423+
connection = get_db_connection(db_config)
424+
cursor = connection.cursor(dictionary=True) # Use dictionary cursor
425+
426+
cursor.execute(query, (curr,))
427+
reservations = cursor.fetchall()
428+
429+
print(f"Fetched Reservations: {reservations}") # Debugging line
430+
431+
for reservation in reservations:
432+
move_to_completed_reservations(reservation['ReservationID'])
433+
434+
cursor.close()
435+
connection.close()
436+
437+
return jsonify({'message': 'Checked and processed reservations successfully.'}), 200
438+
except Exception as e:
439+
print(f"Error checking reservations end: {e}")
440+
return jsonify(error='Error checking reservations end'), 500
441+
442+
@app.route('/api/remove-reservation/<string:chair_id>', methods=['DELETE'])
443+
def remove_reservation_route(chair_id):
444+
try:
445+
result = remove_reservation(chair_id)
446+
if 'error' in result:
447+
return jsonify(result), 404
448+
else:
449+
return jsonify(result), 200
450+
except Exception as e:
451+
print(f"Error removing reservation: {e}")
452+
return jsonify(error='Error removing reservation'), 500
453+
454+
@app.route('/api/update-reservation-endtime/<string:chair_id>/<string:endtime>', methods=['PUT'])
455+
def update_endtime_route(chair_id, endtime):
456+
try:
457+
result = update_reservation_endtime(chair_id, endtime)
458+
if 'error' in result:
459+
return jsonify(result), 404
460+
else:
461+
return jsonify(result), 200
462+
except Exception as e:
463+
print(f"Error updating reservation: {e}")
464+
return jsonify(error='Error updating reservation'), 500
465+
466+
@app.route('/api/get-reservation-by-seat/<string:seat>', methods=['GET'])
467+
def get_reservation_by_seat_route(seat):
468+
try:
469+
reservation = get_reservation_by_seat(seat)
470+
if reservation:
471+
return jsonify({'reservation': reservation}), 200
472+
else:
473+
return jsonify({'error': 'Reservation not found for the specified seat'}), 404
474+
except Exception as e:
475+
print(f"Error fetching reservation by seat: {e}")
476+
return jsonify({'error': str(e)}), 500
477+
297478
@app.route('/api/get-waitlist-entries', methods=['GET'])
298479
def get_waitlist_entries_route():
299480
try:
@@ -319,11 +500,22 @@ def create_waitlist_entry_route():
319500
return jsonify(message='Error creating waitlist entry'), 500
320501

321502

503+
322504
@app.route('/api/create-reservation', methods=['POST'])
323505
def create_reservation_route():
324506
try:
325507
data = request.get_json()
326-
user_id = data.get('user_id') # Change this to fetch the user_id from your authentication mechanism
508+
user_id = data.get('user_id')
509+
start_time = data.get('starttime')
510+
end_time = data.get('endtime')
511+
tablefee = data.get('tablefee')
512+
seat = data.get('seat')
513+
514+
existing_reservation = get_reservation_by_seat(seat)
515+
516+
if existing_reservation and is_overlapping(start_time, end_time, existing_reservation['StartTime'], existing_reservation['EndTime']):
517+
return jsonify({'error': 'Seat already booked for this time range!'}), 400
518+
327519
create_reservation(user_id, data)
328520
return jsonify({'message': 'Reservation created successfully'}), 200
329521
except Exception as e:
@@ -427,43 +619,7 @@ def get_user_by_id_route(user_id):
427619
except Exception as e:
428620
print(f"Error fetching user by ID: {e}")
429621
return jsonify({'error': str(e)}), 500
430-
431-
# @app.route('/api/update-account/', methods=['PUT'])
432-
# def update_account(user_id):
433-
# try:
434-
# updated_data = request.get_json()
435-
# update_user_details(user_id, updated_data)
436-
# updated_user = get_user_by_id(user_id)
437-
# return jsonify({'message': 'Account updated successfully', 'updated_user': updated_user}), 200
438-
# except Exception as e:
439-
# print(f"Error updating account: {e}")
440-
# return jsonify(message='Error updating account'), 500
441-
442-
# @app.route('/api/get-reservations/<int:user_id>', methods=['GET'])
443-
# def get_reservations(user_id):
444-
# try:
445-
# reservations = get_user_reservations(user_id)
446-
# return jsonify(reservations)
447-
# except Exception as e:
448-
# print(f"Error fetching reservations: {e}")
449-
# return jsonify(error='Error fetching reservations'), 500
450-
451-
# @app.route('/api/reservations', methods=['POST'])
452-
# def make_reservation():
453-
# try:
454-
# data = request.get_json()
455-
# user_id = 5 # Replace with the actual user ID; you need to identify the user somehow
456-
# result = create_reservation(user_id, data)
457-
# return jsonify(result)
458-
# except Exception as e:
459-
# print(f"Error creating reservation: {e}")
460-
# return jsonify(error='Error creating reservation'), 500
461-
462-
463-
# Additional user-related functions
464-
465-
# Reservation-related functions (as per your existing code)
466-
# ...
622+
467623

468624
# Main route for testing
469625
@app.route('/')

backend/db/init.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ CREATE TABLE Reservations (
3131

3232
);
3333

34+
CREATE TABLE Completed_Reservations (
35+
ReservationID INT,
36+
UserID INT,
37+
ResDate VARCHAR(225),
38+
StartTime TIME NOT NULL,
39+
EndTime TIME NOT NULL,
40+
Seat VARCHAR(50),
41+
TableFee DECIMAL(10,2) DEFAULT 0.00,
42+
FOREIGN KEY (UserID) REFERENCES Users(UserID) ON DELETE CASCADE
43+
44+
);
45+
46+
3447

3548
CREATE TABLE QR_Codes (
3649
QRCodeID INT AUTO_INCREMENT PRIMARY KEY,

0 commit comments

Comments
 (0)