forked from subinkim/CS121FinalProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-routines.sql
More file actions
146 lines (129 loc) · 3.97 KB
/
setup-routines.sql
File metadata and controls
146 lines (129 loc) · 3.97 KB
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
-- UDF
-- Function to calculate the total value of inventory based on price and quantity
DROP FUNCTION IF EXISTS calculate_inventory_value;
DELIMITER !
CREATE FUNCTION calculate_inventory_value(prod_id INTEGER)
RETURNS DECIMAL DETERMINISTIC
BEGIN
DECLARE totalInventoryPrice DECIMAL(10,2);
SELECT SUM(price * inventory) INTO totalInventoryPrice
FROM product NATURAL JOIN store
WHERE product_id = prod_id;
RETURN totalInventoryPrice;
END !
DELIMITER ;
-- Function to calculate the total value of the cart
DROP FUNCTION IF EXISTS calculate_cart_total;
DELIMITER !
CREATE FUNCTION calculate_cart_total(p_username VARCHAR(20))
RETURNS DECIMAL DETERMINISTIC
BEGIN
DECLARE totalCartPrice DECIMAL(10,2);
SELECT SUM(price * num_items) INTO totalCartPrice
FROM cart NATURAL JOIN product
WHERE username=p_username;
RETURN totalCartPrice;
END !
DELIMITER ;
-- PROCEDURE
-- Procedure to update inventory for a specific product in the Store table
-- This is an admin privelage
DROP PROCEDURE IF EXISTS update_inventory;
DELIMITER !
CREATE PROCEDURE update_inventory (
IN product_id INTEGER,
IN new_inventory INTEGER
)
BEGIN
UPDATE store
SET inventory = new_inventory
WHERE product_id = product_id;
END !
DELIMITER ;
-- Procedure to remove items from cart for a specific user and add the
-- info to purchase_history table
DROP PROCEDURE IF EXISTS move_cart_to_purchase_history;
DELIMITER !
CREATE PROCEDURE move_cart_to_purchase_history(
IN p_username VARCHAR(20)
)
BEGIN
-- Insert items from cart to purchase_history
INSERT INTO purchase_history (username, purchase_time, product_id, num_items)
SELECT username, NOW() AS purchase_time, product_id, num_items
FROM cart WHERE username = p_username;
-- Delete items from cart
DELETE FROM cart WHERE username = p_username;
END !
DELIMITER ;
-- Procedure to add item to cart and check that there is enough inventory left
DROP PROCEDURE IF EXISTS add_item_cart;
DELIMITER !
CREATE PROCEDURE add_item_cart(
IN p_username VARCHAR(20),
IN p_product_id INTEGER
)
BEGIN
-- Check if the product exists
DECLARE available_quantity INT;
-- Check if the requested quantity is available in the store
SELECT inventory INTO available_quantity
FROM store WHERE product_id = p_product_id;
IF available_quantity = 0 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Product is out of stock';
ELSE
-- Insert the item into the cart
INSERT INTO cart (username, product_id, num_items)
VALUES (p_username, p_product_id, 1)
ON DUPLICATE KEY UPDATE num_items = num_items + 1;
END IF;
END !
DELIMITER ;
-- Procedure to decrease the number of an item in cart
DROP PROCEDURE IF EXISTS decrease_item_cart;
DELIMITER !
CREATE PROCEDURE decrease_item_cart(
IN p_username VARCHAR(20),
IN p_product_id INTEGER
)
BEGIN
UPDATE cart
SET num_items = num_items - 1
WHERE product_id = p_product_id AND
username = p_username;
END !
DELIMITER ;
-- Procedure to delete an item from the cart
DROP PROCEDURE IF EXISTS delete_item_cart;
DELIMITER !
CREATE PROCEDURE delete_item_cart(
IN p_username VARCHAR(20),
IN p_product_id INTEGER
)
BEGIN
DELETE FROM cart
WHERE product_id = p_product_id AND
username = p_username;
END !
DELIMITER ;
-- TRIGGER
DROP TRIGGER IF EXISTS after_cart_checkout;
DELIMITER !
-- after_cart_checkout trigger which gets triggered BEFORE
-- every time a new row is inserted into purchase_history
CREATE TRIGGER after_cart_checkout BEFORE INSERT ON purchase_history FOR EACH ROW
BEGIN
DECLARE num_items_var INTEGER;
DECLARE curr_inventory INTEGER;
-- Fetch cart items for the inserted user
SELECT num_items, inventory
INTO num_items_var, curr_inventory
FROM cart NATURAL JOIN store
WHERE username = NEW.username
AND product_id = NEW.product_id;
UPDATE store
SET inventory = inventory - num_items_var
WHERE product_id = NEW.product_id;
END !
DELIMITER ;