-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
743 lines (617 loc) · 29.1 KB
/
app.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
from flask import Flask, render_template, json, redirect
from flask_mysqldb import MySQL
from flask import request
import os
import database.db_connector as db
# Configuration
app = Flask(__name__, template_folder="templates")
mysql = MySQL(app)
db_connection = db.connect_to_database()
#comes from module, is essentially MySQL(app) from starter code
# Routes
@app.route('/')
def root():
return render_template("main.j2")
@app.route('/bsg-people')
def bsg_people():
return "This is the bsg-people route."
# Customers Routes
@app.route('/customers', methods = ["POST", "GET"])
def customers():
# Write the query and save it to a variable
if request.method == "GET":
query = "SELECT * FROM Customers"
cursor = db.execute_query(db_connection=db_connection, query=query)
try:
# The cursor.fetchall() function tells the cursor object to return all
# the results from the previously executed
results = cursor.fetchall()
# Sends the results back to the web browser.
return render_template("customers.j2", customers = results)
# For testing purposes to see json.
# results = json.dumps(cursor.fetchall())
# return result
except:
print("ERROR")
finally:
cursor.close()
if request.method == "POST":
# runs if user presses add button
if request.form.get("Add_Customer"):
# grabs user form inputs
name = request.form["name"]
email = request.form["email"]
phone = request.form["phone"]
# no null inputs
query = "INSERT INTO Customers (name, email, phone) VALUES (%s, %s, %s)"
cursor = db.execute_query(db_connection=db_connection, query=query, query_params=(name,email,phone))
results = cursor.fetchall()
# redirect back to customers page
return redirect("/customers")
@app.route("/delete_customer/<int:customerID>")
def delete_customer(customerID):
#mySQL query to delete the person with passed ID
query = "DELETE FROM Customers WHERE customerID = '%s';"
cursor = db.execute_query(db_connection=db_connection, query=query, query_params=(customerID,)) #keep comma, required idk why
return redirect("/customers")
@app.route("/edit_customer/<int:customerID>", methods=["POST", "GET"])
def edit_customer(customerID):
if request.method == "GET":
query = "SELECT * FROM Customers WHERE customerID = %s" % (customerID)
cursor = db.execute_query(db_connection=db_connection, query=query)
data = cursor.fetchall()
return render_template("customers_edit.j2", data=data)
if request.method == "POST":
# runs if user presses 'Edit Customer' button
if request.form.get("Edit_Customer"):
# grabs user form inputs
id = request.form["customerID"]
name = request.form["name"]
email = request.form["email"]
phone = request.form["phone"]
# no null inputs
query = "UPDATE Customers SET Customers.name = %s, Customers.email = %s, Customers.phone = %s WHERE Customers.customerID = %s;"
cursor = db.execute_query(db_connection=db_connection, query=query, query_params=(name,email,phone, id))
results = cursor.fetchall()
# redirect back to customers page
return redirect("/customers")
# Boardgames Routes
@app.route('/boardgames', methods=["POST", "GET"])
def boardgames():
if request.method == "GET":
try:
# Select only the necessary columns
query = """
SELECT BoardGames.gameID, BoardGames.title, Categories.categoryName,
BoardGames.playerCount, BoardGames.gameCost, BoardGames.quantity
FROM BoardGames
JOIN Categories ON BoardGames.categoryID = Categories.categoryID;
"""
cursor = db.execute_query(db_connection=db_connection, query=query)
results = cursor.fetchall()
# Query to grab categoryID and categoryName for the dropdown
query2 = "SELECT categoryID, categoryName FROM Categories;"
cursor = db.execute_query(db_connection=db_connection, query=query2)
categories_data = cursor.fetchall()
# Query to grab customer IDs for dropdown (if still relevant)
query3 = "SELECT customerID, name FROM Customers;"
cursor = db.execute_query(db_connection=db_connection, query=query3)
customers_data = cursor.fetchall()
return render_template("boardgames.j2", boardgames=results, categories=categories_data)
except:
print("ERROR")
finally:
cursor.close()
if request.method == "POST":
if request.form.get("Add_BoardGame"):
# Retrieve form data
title = request.form["title"]
categoryID = request.form["categoryID"]
playerCount = request.form["playerCount"]
gameCost = request.form["gameCost"]
quantity = request.form["quantity"]
# Insert new board game
query = """
INSERT INTO BoardGames (title, categoryID, playerCount, gameCost, quantity)
VALUES (%s, %s, %s, %s, %s);
"""
db.execute_query(db_connection=db_connection, query=query, query_params=(title, categoryID, playerCount, gameCost, quantity))
return redirect("/boardgames")
@app.route("/delete_boardgame/<int:gameID>")
def delete_boardgame(gameID):
# Delete the board game by its ID
query = "DELETE FROM BoardGames WHERE gameID = %s;"
db.execute_query(db_connection=db_connection, query=query, query_params=(gameID,))
return redirect("/boardgames")
@app.route("/edit_boardgame/<int:gameID>", methods=["POST", "GET"])
def edit_boardgame(gameID):
if request.method == "GET":
# Fetch the specific board game for editing
query = """
SELECT BoardGames.gameID, BoardGames.title, BoardGames.categoryID,
Categories.categoryName, BoardGames.playerCount,
BoardGames.gameCost, BoardGames.quantity
FROM BoardGames
JOIN Categories ON BoardGames.categoryID = Categories.categoryID
WHERE gameID = %s;
"""
cursor = db.execute_query(db_connection=db_connection, query=query, query_params=(gameID,))
data = cursor.fetchall()
# Query to grab all categories for the dropdown
query2 = "SELECT categoryID, categoryName FROM Categories;"
cursor = db.execute_query(db_connection=db_connection, query=query2)
categories_data = cursor.fetchall()
return render_template("boardgames_edit.j2", data=data, categories=categories_data)
if request.method == "POST":
if request.form.get("Edit_BoardGame"):
# Retrieve updated form data
title = request.form["title"]
categoryID = request.form["categoryID"]
playerCount = request.form["playerCount"]
gameCost = request.form["gameCost"]
quantity = request.form["quantity"]
# Update board game details
query = """
UPDATE BoardGames
SET title = %s, categoryID = %s, playerCount = %s, gameCost = %s, quantity = %s
WHERE gameID = %s;
"""
db.execute_query(db_connection=db_connection, query=query, query_params=(title, categoryID, playerCount, gameCost, quantity, gameID))
# Updates the orders entry with total price.
update_totalprice = """
UPDATE Orders
SET Orders.orderAmount = (
SELECT SUM(BoardGames.gameCost * OrderDetails.quantity)
FROM OrderDetails
JOIN BoardGames ON OrderDetails.gameID = BoardGames.gameID
WHERE OrderDetails.orderID = Orders.orderID
)
WHERE Orders.orderID IN (
SELECT OrderDetails.orderID
FROM OrderDetails
WHERE OrderDetails.gameID = %s
)
""" % (gameID)
cursor = db.execute_query(db_connection=db_connection, query=update_totalprice)
return redirect("/boardgames")
@app.route('/categories', methods=["POST", "GET"])
def categories():
if request.method == "GET":
try:
# Select necessary columns for categories
query = """
SELECT categoryID, categoryName, description
FROM Categories;
"""
cursor = db.execute_query(db_connection=db_connection, query=query)
results = cursor.fetchall()
return render_template("categories.j2", categories=results)
except:
print("ERROR")
finally:
cursor.close()
if request.method == "POST":
if request.form.get("Add_Category"):
# Retrieve form data
categoryName = request.form["categoryName"]
description = request.form["description"]
# Insert new category
query = """
INSERT INTO Categories (categoryName, description)
VALUES (%s, %s);
"""
db.execute_query(db_connection=db_connection, query=query, query_params=(categoryName, description))
return redirect("/categories")
@app.route("/delete_category/<int:categoryID>")
def delete_category(categoryID):
# Delete the category by its ID
query = "DELETE FROM Categories WHERE categoryID = %s;"
db.execute_query(db_connection=db_connection, query=query, query_params=(categoryID,))
return redirect("/categories")
@app.route("/edit_category/<int:categoryID>", methods=["POST", "GET"])
def edit_category(categoryID):
if request.method == "GET":
# Fetch the specific category for editing
query = """
SELECT categoryID, categoryName, description
FROM Categories
WHERE categoryID = %s;
"""
cursor = db.execute_query(db_connection=db_connection, query=query, query_params=(categoryID,))
data = cursor.fetchall()
return render_template("categories_edit.j2", data=data)
if request.method == "POST":
if request.form.get("Edit_Category"):
# Retrieve updated form data
categoryName = request.form["categoryName"]
description = request.form["description"]
# Update category details
query = """
UPDATE Categories
SET categoryName = %s, description = %s
WHERE categoryID = %s;
"""
db.execute_query(db_connection=db_connection, query=query, query_params=(categoryName, description, categoryID))
return redirect("/categories")
# Orders
@app.route("/orders", methods=["POST", "GET"])
def orders():
# Write the query and save it to a variable
if request.method == "GET":
try:
query = """
SELECT Orders.*, Customers.name
FROM Orders
Join Customers
WHERE (Orders.customerID = Customers.customerID);
"""
cursor = db.execute_query(db_connection=db_connection, query=query)
# The cursor.fetchall() function tells the cursor object to return all
# the results from the previously executed
results = cursor.fetchall()
# Query to grab customer ID's for dropdown.
query2 = "SELECT customerID, name from Customers"
cursor = db.execute_query(db_connection=db_connection, query=query2)
customerid_data = cursor.fetchall()
# Sends the results back to the web browser.
return render_template("orders.j2", orders = results, customers = customerid_data)
# For testing purposes to see json.
# results = json.dumps(cursor.fetchall())
# return results
except:
print("ERROR")
finally:
cursor.close()
if request.method == "POST":
# runs if user presses add button
if request.form.get("Add_Order"):
# grabs user form inputs
customer = request.form["customer"]
orderDate = request.form["orderDate"]
# no null inputs
query = "INSERT INTO Orders (customerID, orderDate) VALUES (%s, %s)"
cursor = db.execute_query(db_connection=db_connection, query=query, query_params=(customer,orderDate))
results = cursor.fetchall()
# redirect back to customers page
return redirect("/orders")
@app.route("/delete_order/<int:orderID>")
def delete_order(orderID):
#mySQL query to delete the person with passed ID
query = "DELETE FROM Orders WHERE orderID = '%s';"
cursor = db.execute_query(db_connection=db_connection, query=query, query_params=(orderID,)) #keep comma, required idk why
return redirect("/orders")
@app.route("/edit_order/<int:orderID>", methods=["POST", "GET"])
def edit_order(orderID):
if request.method == "GET":
# Fetch the specific order for editing
query = """
SELECT Orders.orderID, Orders.customerID, Orders.orderDate, Orders.orderAmount, Customers.name
FROM Orders
JOIN Customers ON Orders.customerID = Customers.customerID
WHERE Orders.orderID = %s;
"""
cursor = db.execute_query(db_connection=db_connection, query=query, query_params=(orderID,))
data = cursor.fetchall()
# Query to grab all customers for the dropdown
query2 = "SELECT customerID, name FROM Customers;"
cursor = db.execute_query(db_connection=db_connection, query=query2)
customers_data = cursor.fetchall()
return render_template("orders_edit.j2", data=data, customers=customers_data)
if request.method == "POST":
# runs if user presses 'Edit Customer' button
if request.form.get("Edit_Order"):
# grabs user form inputs
orderID = request.form["orderID"]
orderDate = request.form["orderDate"]
# no null inputs
query = "UPDATE Orders SET Orders.orderDate = %s WHERE Orders.orderID = %s;"
cursor = db.execute_query(db_connection=db_connection, query=query, query_params=(orderDate, orderID))
results = cursor.fetchall()
return redirect("/orders")
# OrderDetails
@app.route('/orderdetails', methods = ["POST", "GET"])
def orderdetails():
# Write the query and save it to a variable
if request.method == "GET":
try:
query = """
SELECT OrderDetails.*, BoardGames.title, Customers.name
FROM OrderDetails
Join BoardGames
ON (OrderDetails.gameID = BoardGames.gameID)
JOIN Orders
ON (Orders.orderID = OrderDetails.orderID)
JOIN Customers
ON (Customers.customerID = Orders.customerID);
"""
cursor = db.execute_query(db_connection=db_connection, query=query)
# The cursor.fetchall() function tells the cursor object to return all
# the results from the previously executed
results = cursor.fetchall()
# Query to grab order ID for dropdown.
query2 = """
SELECT orderID, Orders.customerID, name
FROM Orders
JOIN Customers
ON Orders.customerID = Customers.customerID;
"""
cursor = db.execute_query(db_connection=db_connection, query=query2)
order_data = cursor.fetchall()
# Query to grab game ID for dropdown.
query3 = "SELECT gameID, title from BoardGames"
cursor = db.execute_query(db_connection=db_connection, query=query3)
game_data = cursor.fetchall()
# Sends the results back to the web browser.
return render_template("orderdetails.j2", orderdetails = results, orders = order_data, games = game_data)
# For testing purposes to see json.
# results = json.dumps(cursor.fetchall())
# return results
except:
print("ERROR")
finally:
cursor.close()
if request.method == "POST":
# runs if user presses add button
if request.form.get("Add_Order_Detail"):
# grabs user form inputs
order = request.form["order"]
game = request.form["game"]
quantity = request.form["quantity"]
# no null inputs
query = "INSERT INTO OrderDetails (orderID, gameID, quantity) VALUES (%s, %s, %s)"
cursor = db.execute_query(db_connection=db_connection, query=query, query_params=(order,game,quantity))
# Updates the orders entry with total price.
update_totalprice = """
UPDATE Orders
SET Orders.orderAmount = (
SELECT SUM(BoardGames.gameCost * OrderDetails.quantity)
FROM OrderDetails
JOIN BoardGames ON OrderDetails.gameID = BoardGames.gameID
WHERE OrderDetails.orderID = Orders.orderID
)
WHERE Orders.orderID = %s
""" % (order)
cursor = db.execute_query(db_connection=db_connection, query=update_totalprice)
results = cursor.fetchall()
# redirect back to customers page
return redirect("/orderdetails")
@app.route("/delete_orderdetail/<int:orderDetailID>/<int:orderID>")
def delete_orderdetail(orderDetailID, orderID):
#mySQL query to delete the orderDetail with passed ID
query = "DELETE FROM OrderDetails WHERE orderDetailID = '%s';"
cursor = db.execute_query(db_connection=db_connection, query=query, query_params=(orderDetailID,)) #keep comma, required idk why
update_totalprice = """
UPDATE Orders
SET Orders.orderAmount = (
SELECT IFNULL(SUM(BoardGames.gameCost * OrderDetails.quantity), 0)
FROM OrderDetails
JOIN BoardGames ON OrderDetails.gameID = BoardGames.gameID
WHERE OrderDetails.orderID = Orders.orderID
)
WHERE Orders.orderID = %s
""" % (orderID)
cursor = db.execute_query(db_connection=db_connection, query=update_totalprice)
return redirect("/orderdetails")
@app.route("/edit_orderdetail/<int:orderDetailID>", methods=["POST", "GET"])
def edit_orderdetail(orderDetailID):
if request.method == "GET":
# Fetch the specific order detail with the game title included
query = """
SELECT OrderDetails.orderDetailID, OrderDetails.orderID, OrderDetails.gameID,
OrderDetails.quantity, BoardGames.title
FROM OrderDetails
JOIN BoardGames
ON (OrderDetails.gameID = BoardGames.gameID)
WHERE OrderDetails.orderDetailID = %s;
"""
cursor = db.execute_query(db_connection=db_connection, query=query, query_params=(orderDetailID,))
data = cursor.fetchall()
# Fetch all games for the dropdown
query2 = """
SELECT BoardGames.title, BoardGames.gameID
FROM BoardGames;
"""
cursor = db.execute_query(db_connection=db_connection, query=query2)
game_data = cursor.fetchall()
return render_template("orderdetails_edit.j2", data=data, gamedata=game_data)
if request.method == "POST":
# runs if user presses 'Edit Customer' button
if request.form.get("Edit_OrderDetail"):
# grabs user form inputs
orderDetailID = request.form["orderDetailID"]
orderID = request.form["orderID"]
gameID = request.form["gameID"]
quantity = request.form["quantity"]
# no null inputs
query = "UPDATE OrderDetails SET OrderDetails.orderID = %s, OrderDetails.gameID = %s, OrderDetails.quantity = %s WHERE OrderDetails.orderDetailID = %s;"
cursor = db.execute_query(db_connection=db_connection, query=query, query_params=(orderID, gameID, quantity, orderDetailID))
results = cursor.fetchall()
# Updates the orders entry with new quantity.
update_totalprice = """
UPDATE Orders
SET Orders.orderAmount = (
SELECT SUM(BoardGames.gameCost * OrderDetails.quantity)
FROM OrderDetails
JOIN BoardGames ON OrderDetails.gameID = BoardGames.gameID
WHERE OrderDetails.orderID = Orders.orderID
)
WHERE Orders.orderID = %s
""" % (orderID)
cursor = db.execute_query(db_connection=db_connection, query=update_totalprice)
return redirect("/orderdetails")
@app.route('/rentals', methods=["POST", "GET"])
def rentals():
if request.method == "GET":
try:
# Select necessary columns for rentals
query = """
SELECT Rentals.rentalID, Rentals.customerID, Rentals.rentalDate, Rentals.returnDate, Rentals.rentalCost, Customers.name AS customerName
FROM Rentals
LEFT JOIN Customers ON Rentals.customerID = Customers.customerID;
"""
cursor = db.execute_query(db_connection=db_connection, query=query)
results = cursor.fetchall()
# Query to grab customer IDs for dropdown
query2 = "SELECT customerID, name FROM Customers;"
cursor = db.execute_query(db_connection=db_connection, query=query2)
customers_data = cursor.fetchall()
return render_template("rentals.j2", rentals=results, customers=customers_data)
except Exception as e:
print(f"Error fetching rentals: {e}")
finally:
cursor.close()
if request.method == "POST":
if request.form.get("Add_Rental"):
# Retrieve form data
customerID = request.form["customerID"]
rentalDate = request.form["rentalDate"]
returnDate = request.form.get("returnDate") or None # Set to None if empty
rentalCost = request.form["rentalCost"]
# Insert new rental
query = """
INSERT INTO Rentals (customerID, rentalDate, returnDate, rentalCost)
VALUES (%s, %s, %s, %s);
"""
db.execute_query(db_connection=db_connection, query=query, query_params=(customerID, rentalDate, returnDate, rentalCost))
return redirect("/rentals")
@app.route("/delete_rental/<int:rentalID>")
def delete_rental(rentalID):
# Delete the rental by its ID
query = "DELETE FROM Rentals WHERE rentalID = %s;"
db.execute_query(db_connection=db_connection, query=query, query_params=(rentalID,))
return redirect("/rentals")
@app.route("/edit_rental/<int:rentalID>", methods=["POST", "GET"])
def edit_rental(rentalID):
if request.method == "GET":
try:
# Fetch the specific rental for editing
query = """
SELECT rentalID, customerID, rentalDate, returnDate, rentalCost
FROM Rentals
WHERE rentalID = %s;
"""
cursor = db.execute_query(db_connection=db_connection, query=query, query_params=(rentalID,))
data = cursor.fetchall()
# Query to grab customer IDs for dropdown
query2 = "SELECT customerID, name FROM Customers;"
cursor = db.execute_query(db_connection=db_connection, query=query2)
customers_data = cursor.fetchall()
return render_template("rentals_edit.j2", data=data, customers=customers_data)
except Exception as e:
print(f"Error fetching rental for editing: {e}")
finally:
cursor.close()
if request.method == "POST":
if request.form.get("Edit_Rental"):
# Retrieve updated form data
rentalID = request.form["rentalID"]
customerID = request.form["customerID"]
rentalDate = request.form["rentalDate"]
returnDate = request.form.get("returnDate") or None # Set to None if empty
rentalCost = request.form["rentalCost"]
# Update rental details
query = """
UPDATE Rentals
SET customerID = %s, rentalDate = %s, returnDate = %s, rentalCost = %s
WHERE rentalID = %s;
"""
db.execute_query(db_connection=db_connection, query=query, query_params=(customerID, rentalDate, returnDate, rentalCost, rentalID))
return redirect("/rentals")
@app.route('/rentaldetails', methods=["POST", "GET"])
def rentaldetails():
if request.method == "GET":
try:
# Fetch rental details
query = """
SELECT RentalDetails.rentalDetailID, RentalDetails.rentalID, RentalDetails.gameID,
BoardGames.title AS gameTitle, RentalDetails.rentalDuration, RentalDetails.rentalPrice
FROM RentalDetails
JOIN BoardGames ON RentalDetails.gameID = BoardGames.gameID;
"""
cursor = db.execute_query(db_connection=db_connection, query=query)
results = cursor.fetchall()
# Fetch rentals for dropdown
query2 = "SELECT rentalID FROM Rentals;"
cursor = db.execute_query(db_connection=db_connection, query=query2)
rentals_data = cursor.fetchall()
# Fetch games for dropdown
query3 = "SELECT gameID, title FROM BoardGames;"
cursor = db.execute_query(db_connection=db_connection, query=query3)
games_data = cursor.fetchall()
return render_template("rentaldetails.j2", rentaldetails=results, rentals=rentals_data, games=games_data)
except Exception as e:
print(f"ERROR: {e}")
finally:
cursor.close()
if request.method == "POST":
if request.form.get("Add_RentalDetail"):
# Retrieve form data
rentalID = request.form["rentalID"]
gameID = request.form["gameID"]
rentalDuration = request.form["rentalDuration"]
rentalPrice = request.form["rentalPrice"]
# Insert new rental detail
query = """
INSERT INTO RentalDetails (rentalID, gameID, rentalDuration, rentalPrice)
VALUES (%s, %s, %s, %s);
"""
db.execute_query(db_connection=db_connection, query=query, query_params=(rentalID, gameID, rentalDuration, rentalPrice))
return redirect("/rentaldetails")
@app.route("/delete_rentaldetail/<int:rentalDetailID>")
def delete_rentaldetail(rentalDetailID):
try:
# Delete the rental detail by its ID
query = "DELETE FROM RentalDetails WHERE rentalDetailID = %s;"
db.execute_query(db_connection=db_connection, query=query, query_params=(rentalDetailID,))
return redirect("/rentaldetails")
except Exception as e:
print(f"ERROR: {e}")
@app.route("/edit_rentaldetail/<int:rentalDetailID>", methods=["POST", "GET"])
def edit_rentaldetail(rentalDetailID):
if request.method == "GET":
try:
# Fetch specific rental detail for editing
query = """
SELECT RentalDetails.rentalDetailID, RentalDetails.rentalID, RentalDetails.gameID,
RentalDetails.rentalDuration, RentalDetails.rentalPrice, BoardGames.title AS gameTitle
FROM RentalDetails
JOIN BoardGames ON RentalDetails.gameID = BoardGames.gameID
WHERE RentalDetails.rentalDetailID = %s;
"""
cursor = db.execute_query(db_connection=db_connection, query=query, query_params=(rentalDetailID,))
data = cursor.fetchall()
# Fetch rentals for dropdown
query2 = "SELECT rentalID FROM Rentals;"
cursor = db.execute_query(db_connection=db_connection, query=query2)
rentals_data = cursor.fetchall()
# Fetch games for dropdown
query3 = "SELECT gameID, title FROM BoardGames;"
cursor = db.execute_query(db_connection=db_connection, query=query3)
games_data = cursor.fetchall()
return render_template("rentaldetails_edit.j2", data=data, rentals=rentals_data, games=games_data)
except Exception as e:
print(f"ERROR: {e}")
finally:
cursor.close()
if request.method == "POST":
if request.form.get("Edit_RentalDetail"):
# Retrieve updated form data
rentalDetailID = request.form["rentalDetailID"]
rentalID = request.form["rentalID"]
gameID = request.form["gameID"]
rentalDuration = request.form["rentalDuration"]
rentalPrice = request.form["rentalPrice"]
# Update rental detail
query = """
UPDATE RentalDetails
SET rentalID = %s, gameID = %s, rentalDuration = %s, rentalPrice = %s
WHERE rentalDetailID = %s;
"""
db.execute_query(db_connection=db_connection, query=query, query_params=(rentalID, gameID, rentalDuration, rentalPrice, rentalDetailID))
return redirect("/rentaldetails")
# Listener
if __name__ == "__main__":
port = int(os.environ.get('PORT', 57294))
# ^^^^
# You can replace this number with any valid port
app.run(port=port, debug=True)