diff --git a/controller.py b/controller.py index 7e6a00e9c163416c92fab8981e50dd3e2c52c41d..46199758f48044dd66e8e8c48824f09f201f94d3 100644 --- a/controller.py +++ b/controller.py @@ -1,4 +1,6 @@ import sqlite3 + +from prettytable import from_db_cursor from userStories.story1 import checkIfRoastedBy @@ -141,6 +143,7 @@ class coffeeControl: """ con = self.con cursor = self.cursor + showCoffeesAndRoasteries(cursor) coffeeid = 0 while coffeeid == 0: # Takes input from user @@ -169,6 +172,35 @@ class coffeeControl: con.commit() print("Note posted ✅") +def showCoffeesAndRoasteries(cursor): + """ + :param cursor: Cursor object that is connected to the database + Prints a list of coffees in the database and their respective roastery. + If a view of coffees and roastery does not exists, the cursor creates a view. + """ + print("\nList of coffees and their respective roasteries in the database:") + + createView = """ + CREATE VIEW IF NOT EXISTS CoffeeAndRoastery(CoffeeID, CoffeeName, Price, RoasteryName) + AS SELECT RC.CoffeeID, RC.CoffeeName, RC.Price, R.RoasteryName + FROM RoastedCoffee AS RC INNER JOIN RoastedBy AS RB ON RC.CoffeeID = RB.CoffeeID + INNER JOIN Roastery AS R ON R.RoasteryID = RB.RoasteryID + ORDER BY RC.CoffeeID; + """ + + coffeeAndRoastery=""" + SELECT CoffeeName, RoasteryName + From CoffeeAndRoastery + """ + try: + cursor.executescript(createView) + cursor.execute(coffeeAndRoastery) + table = from_db_cursor(cursor) + print(table) + except Exception as e: + print("Something went wrong when generating tables with coffee and roasteries") + print(e) + def checkIfUserExists(email, password, db): """Checks if a user exists in the database by sending a query to the database.