From 4dc6a3cd39df21eba22586b4e0021201d35c813f Mon Sep 17 00:00:00 2001 From: Sammy Alyooi Date: Sat, 14 Mar 2026 10:13:36 -0700 Subject: [PATCH 1/3] added sp create trails --- app.py | 41 +++++++++++++++++++++++++++++++++++++++++ database/PL.sql | 23 +++++++++++++++++++++++ templates/trails.j2 | 25 +++++++++++++++++++++++++ 3 files changed, 89 insertions(+) diff --git a/app.py b/app.py index 2100673..22313ed 100644 --- a/app.py +++ b/app.py @@ -649,6 +649,47 @@ def delete_rentalinventory(): dbConnection.close() +# create trail + +@app.route("/trails/create", methods=["POST"]) +def create_trails(): + try: + dbConnection = db.connectDB() # this opens skiers db connection + cursor = dbConnection.cursor() + + # Get form data. will do data cleansing try/catch blocks later since we don't have int input for the following attributes: + Name = request.form["create_trail_name"] + Difficulty = request.form["create_trail_difficulty"] + TrailLength = request.form["create_trail_trailLength"] + Status = request.form["create_trail_status"] + + + # call create trails sp method + query1 = "CALL sp_CreateTrail(%s, %s, %s, %s, @t_new_id);" + cursor.execute(query1, (Name, Difficulty, TrailLength, Status)) + cursor.nextset() + # store the generated trail id for the last inserted row. + cursor.execute("SELECT @new_id;") + new_id = cursor.fetchone()[0] # id is index 0 of the row + # cursor.nextset() # Move the the next result. Assuming this to move to the next row? + dbConnection.commit() #commit transaction + print(f"""Create skiers. + ID: {new_id} + Name: {Name} + Difficulty {Difficulty} + TrailLength {TrailLength} + Status {Status} + """) + # Redirect to the updated webpage by add the path /skiers + return redirect("/trails") + except Exception as e: + print(f"Error executing quereis: {e}") + return ("An error occurred while executing trails/create this database queries. ", 500,) # ProgError, OpsError, DBError? can be more specific + finally: + # Close the DB Conneciton, if it exists: + if "dbConnection" in locals() and dbConnection: + dbConnection.close() + # -------------------------------------------------------------------------------------------------- # SkiersRentals CRUD diff --git a/database/PL.sql b/database/PL.sql index a37d5a8..c4b5f1f 100644 --- a/database/PL.sql +++ b/database/PL.sql @@ -479,3 +479,26 @@ BEGIN END // DELIMITER ; + + +-- Create Trails SP: +DROP PROCEDURE IF EXISTS sp_CreateTrail; + +DELIMITER // +create procedure sp_CreateTrail( + in t_name varchar(45), + in t_difficulty varchar(255), + in t_trailLength varchar(11), + in t_status varchar(255), + out t_id int) +BEGIN + insert INTO Trails(TrailName, Difficulty, TrailLength, Status) + VALUES (t_name, t_difficulty, t_trailLength, t_status); + + -- store the skier id of the last inserted row: + select last_insert_id() into t_id; + -- then display id of the last inserted row + select last_insert_id() AS 't_new_id'; + +END // +DELIMITER ; diff --git a/templates/trails.j2 b/templates/trails.j2 index b242b8a..a9dd62c 100644 --- a/templates/trails.j2 +++ b/templates/trails.j2 @@ -52,5 +52,30 @@ +

Create a Trail

+
+ + + + + + + + + + + + + + + + +
+ {% endblock %} \ No newline at end of file From 6a4e5ae3752387fc076075f0614aee3dc406d401 Mon Sep 17 00:00:00 2001 From: Sammy Alyooi Date: Sun, 15 Mar 2026 07:42:09 -0700 Subject: [PATCH 2/3] added update, joins, view --- database/DML.sql | 129 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 88 insertions(+), 41 deletions(-) diff --git a/database/DML.sql b/database/DML.sql index 99c1d43..892bb0c 100644 --- a/database/DML.sql +++ b/database/DML.sql @@ -7,60 +7,36 @@ -- create Skiers input INSERT INTO `Skiers` (`Name`, `Address`, `Phone`, `Email`, `Ability`) VALUES ( @Name, @Address, @Phone, @Email, @Ability); - -- Read all Skiers SELECT * FROM `Skiers`; - -- Read a specific Skier by ID +-- SET @SkierID = 3; SELECT * FROM `Skiers` WHERE `SkierID` = @SkierID - --- Update a Skier's information -UPDATE `Skiers` -SET `Name` = @Name, `Address` = @Address, `Phone` = @Phone, `Email` = @Email, `Ability` = @Ability -WHERE `SkierID` = @SkierID; - -- Delete a Skier +-- SET @SkierID = 5; DELETE FROM `Skiers` WHERE `SkierID` = @SkierID; +-- Update a Skier +SET @SkierID = 1; +SET @Name = "John Smith"; +SET @Address = "123 1ST St"; +SET @Phone = "1234567891"; +SET @Email = "js@example.com"; +SET @Ability = "Expert"; ------------------------------------------------------- --- Trails CRUD Operations --- ----------------------------------------------------- --- create Trails input -INSERT INTO `Trails` (`Name`, `Difficulty`, `Length`, `Status`) -VALUES ( @Name, @Difficulty, @Length, @Status); +UPDATE Skiers + SET Name = @Name, + Address = @Address, + Phone = @Phone, + Email = @Email, + Ability = @Ability + WHERE SkierID = @SkierID; --- Read all Trails -SELECT * FROM `Trails`; --- Read a specific Trail by ID -SELECT * FROM `Trails` WHERE `TrailID` = @TrailID; --- Update a Trail's information -UPDATE `Trails` -SET `Name` = @Name, `Difficulty` = @Difficulty, `Length` = @Length, `Status` = @Status -WHERE `TrailID` = @TrailID; --- Delete a Trail -DELETE FROM `Trails` WHERE `TrailID` = @TrailID; --- --------------------------------------------------- ---SkiersTrails CRUD Operations --- ----------------------------------------------------- --- create SkiersTrails input -INSERT INTO `SkiersTrails` (`Skiers_SkierID`, `Trails_TrailID`) VALUES ( @Skiers_SkierID, @Trails_TrailID); --- Read all SkiersTrails -SELECT * FROM `SkiersTrails`; --- Read a specific SkiersTrails item by ID -SELECT * FROM `SkiersTrails` WHERE `SkiersTrailsID` = @SkiersTrailsID; --- Update a SkiersTrails item's information -UPDATE `SkiersTrails` -SET `Skiers_SkierID` = @Skiers_SkierID, `Trails_TrailID` = @Trails_TrailID -WHERE `SkiersTrailsID` = @SkiersTrailsID; --- Delete a SkiersTrails item -DELETE FROM `SkiersTrails` WHERE `SkiersTrailsID` = @SkiersTrailsID; ------------------------------------------------------ -- Lifts CRUD Operations -- ----------------------------------------------------- -- create Lifts input INSERT INTO `Lifts` (`LiftNum`, `LiftName`, `TravelTime`, `ChairCapacity`, `Status`, `Altitude`) VALUES ( @LiftNum, @LiftName, @TravelTime, @ChairCapacity, @Status, @Altitude); - -- Read all Lifts SELECT * FROM `Lifts`; -- Read a specific Lift by ID @@ -88,8 +64,68 @@ WHERE `SkiersLiftsID` = @SkiersLiftsID; -- Delete a SkiersLifts item DELETE FROM `SkiersLifts` WHERE `SkiersLiftsID` = @SkiersLiftsID; +SELECT s.SkierID, s.Name, p.Type, t.TrailName, t.Difficulty + FROM Passes AS p + JOIN Skiers AS s + ON p.PassID = s.SkierID + JOIN SkiersTrails AS st + ON s.SkierID = st.Skiers_SkierID + JOIN Trails AS t + ON st.Trails_TrailID = t.TrailID + WHERE p.Type = "Full Season" + GROUP By s.SkierID, s.Name + HAVING AVG (t.TrailLength) > 1200; ------------------------------------------------------ ---Passes CRUD Operations +-- Trails CRUD Operations +-- ----------------------------------------------------- +-- create Trails input +INSERT INTO `Trails` (`TrailName`, `Difficulty`, `TrailLength`, `Status`) +VALUES ( @TrailName, @Difficulty, @TrailLength, @Status); +-- Read all Trails +SELECT * FROM `Trails`; +-- Read a specific Trail by ID +-- SET @TrailID = 4; +SELECT * FROM `Trails` WHERE `TrailID` = @TrailID; +-- Update a Trail's information +-- SET @TrailID = 3; +UPDATE `Trails` +SET `Name` = @Name, `Difficulty` = @Difficulty, `TrailLength` = @TrailLength, `Status` = @Status +WHERE `TrailID` = @TrailID; +-- Delete a Trail +-- SET @TrailID = 2; +DELETE FROM `Trails` WHERE `TrailID` = @TrailID; + +-- --------------------------------------------------- +--SkiersTrails CRUD Operations +-- ----------------------------------------------------- +-- create SkiersTrails input +INSERT INTO `SkiersTrails` (`Skiers_SkierID`, `Trails_TrailID`) VALUES ( @Skiers_SkierID, @Trails_TrailID); +-- Read all SkiersTrails +SELECT * FROM `SkiersTrails`; +-- Read a specific SkiersTrails item by ID +SELECT * FROM `SkiersTrails` WHERE `SkiersTrailsID` = @SkiersTrailsID; +-- Update a SkiersTrails item's information +UPDATE `SkiersTrails` +SET `Skiers_SkierID` = @Skiers_SkierID, `Trails_TrailID` = @Trails_TrailID +WHERE `SkiersTrailsID` = @SkiersTrailsID; +-- Delete a SkiersTrails item +DELETE FROM `SkiersTrails` WHERE `SkiersTrailsID` = @SkiersTrailsID; + +-- View to return Expert Skiers whose average Lift Altitude above 8000 +CREATE VIEW ExpertSkiersWithHighAlt AS +SELECT Skiers.SkierID, Skiers.Name, Lifts.LiftName, Lifts.Altitude + FROM Skiers + JOIN SkiersLifts + ON Skiers.SkierID = SkiersLifts.Skiers_SkierID + JOIN Lifts + ON SkiersLifts.Lifts_LiftID = Lifts.LiftID + WHERE Skiers.Ability = "Expert" + GROUP By Skiers.SkierID, Skiers.Name + HAVING AVG(Lifts.Altitude) > 8000; + + SELECT * FROM ExpertSkiersWithHighAlt; +------------------------------------------------------ +-- Passes CRUD Operations -- ----------------------------------------------------- -- create Passes input INSERT INTO `Passes` (`Type`, `PurchaseDate`, `ExpirationDate`, `Skiers_SkierID`) @@ -104,6 +140,7 @@ SET `Type` = @Type, `PurchaseDate` = @PurchaseDate, `ExpirationDate` = @Expirati WHERE `PassID` = @PassID; -- Delete a Pass DELETE FROM `Passes` WHERE `PassID` = @PassID; + ------------------------------------------------------ -- Rentals CRUD Operations -- ----------------------------------------------------- @@ -119,6 +156,7 @@ SET `Type` = @Type WHERE `RentalID` = @RentalID; -- Delete a RentalInventory item DELETE FROM `RentalInventory` WHERE `RentalID` = @RentalID; + ------------------------------------------------------ -- SkiersRentals CRUD Operations -- ----------------------------------------------------- @@ -135,3 +173,12 @@ WHERE `SkiersRentalsID` = @SkiersRentalsID; -- Delete a SkiersRentals item DELETE FROM `SkiersRentals` WHERE `SkiersRentalsID` = @SkiersRentalsID; +-- Return all expert skiers and the equipment type they rented: +SELECT DISTINCT Skiers.Name, Skiers.Ability, RentalInventory.Type + FROM Skiers + JOIN SkiersRentals + ON Skiers.SkierID = SkiersRentals.Skiers_SkierID + JOIN RentalInventory + ON SkiersRentals.RentalInventory_RentalID = RentalInventory.RentalID + WHERE Skiers.Ability = "Expert"; + From 8faee3a62d59d4676467486e63c7a35b492d870e Mon Sep 17 00:00:00 2001 From: Sammy Alyooi Date: Sun, 15 Mar 2026 09:12:02 -0700 Subject: [PATCH 3/3] add citations to README --- README.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f2533d5..04a1b85 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ # Most code is based on and adapted from the CS 340 starter code including: 1. app routes and app.py adapted to fit our needs -2. tempates +2. templates adapted to improve UX 3. style.css copied @@ -55,6 +55,18 @@ Links to an external site. 7 # I Source URL: https://www.google.com/search?sourceid=chrome&udm=50&aep=42&source=chrome.crn.rb Links to an external site. +# Date: 3/11/26 + # Prompts used to debug logic for to delete skierslifts button error handling + # Provided gemini my roll back error handling results and asked it to show exist handler options. +4 # I Source URL: https://www.google.com/search?sourceid=chrome&udm=50&aep=42&source=chrome.crn.rb +Links to an external site. + +# Date: 3/12/26 +4 # Prompts used to consult on the usage of cursor.nextset() in Trails + # Requested scenarios where the cursor moves the next result set. + # I Source URL: https://www.google.com/search?sourceid=chrome&udm=50&aep=42&source=chrome.crn.rb +Links to an external site. + # Date: 3/13/26 # Prompts used to add logic for to update rentals button 4 # I provided my route and asked for an update form that only appears after the button is pressed and is automatically filled with the row ID @@ -69,3 +81,4 @@ Links to an external site. Links to an external site. +