@@ -233,6 +233,83 @@ def perform_warehouse_process():
233
233
# Add the scheduled job to run the ETL process every 168 hours or 1 week
234
234
scheduler .add_job (perform_warehouse_process , 'interval' , hours = 168 )
235
235
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
+
236
313
def create_reservation (user_id , reservation_data ):
237
314
connection = get_db_connection (db_config )
238
315
if connection :
@@ -294,6 +371,110 @@ def get_all_waitlist_entries():
294
371
print (f"Error fetching waitlist entries: { err } " )
295
372
return None
296
373
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
+
297
478
@app .route ('/api/get-waitlist-entries' , methods = ['GET' ])
298
479
def get_waitlist_entries_route ():
299
480
try :
@@ -319,11 +500,22 @@ def create_waitlist_entry_route():
319
500
return jsonify (message = 'Error creating waitlist entry' ), 500
320
501
321
502
503
+
322
504
@app .route ('/api/create-reservation' , methods = ['POST' ])
323
505
def create_reservation_route ():
324
506
try :
325
507
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
+
327
519
create_reservation (user_id , data )
328
520
return jsonify ({'message' : 'Reservation created successfully' }), 200
329
521
except Exception as e :
@@ -427,43 +619,7 @@ def get_user_by_id_route(user_id):
427
619
except Exception as e :
428
620
print (f"Error fetching user by ID: { e } " )
429
621
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
+
467
623
468
624
# Main route for testing
469
625
@app .route ('/' )
0 commit comments