-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.sql
180 lines (148 loc) · 4.93 KB
/
backend.sql
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
-- Users Table
CREATE TABLE users (
user_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
password TEXT NOT NULL,
username VARCHAR(255) NOT NULL UNIQUE
);
-- Categories Table
CREATE TABLE categories (
category_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
category_name TEXT NOT NULL,
category_type VARCHAR(50) DEFAULT NULL
);
-- Transactions Table
CREATE TABLE transactions (
transaction_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
amount DOUBLE NOT NULL,
transaction_date DATE NOT NULL,
description TEXT DEFAULT NULL,
user_id INT DEFAULT NULL,
category_id INT DEFAULT NULL,
transaction_type VARCHAR(255) DEFAULT NULL,
FOREIGN KEY (user_id) REFERENCES users(user_id),
FOREIGN KEY (category_id) REFERENCES categories(category_id)
);
-- Budgets Table
CREATE TABLE budgets (
budget_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
category_id INT DEFAULT NULL,
amount_limit FLOAT DEFAULT NULL,
start_date DATE NOT NULL,
end_date DATE NOT NULL,
user_id INT DEFAULT NULL,
FOREIGN KEY (category_id) REFERENCES categories(category_id),
FOREIGN KEY (user_id) REFERENCES users(user_id)
);
-- SavingGoals Table
CREATE TABLE savinggoals (
goal_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
user_id INT DEFAULT NULL,
target_amount DECIMAL(10, 2) DEFAULT NULL,
start_date DATE DEFAULT NULL,
end_date DATE DEFAULT NULL,
FOREIGN KEY (user_id) REFERENCES users(user_id)
);
-- Reports Table
CREATE TABLE reports (
report_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
user_id INT DEFAULT NULL,
report_type TEXT NOT NULL,
report_date DATE NOT NULL,
generated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(user_id)
);
-- Stored Procedure to Add New User
DELIMITER $$
CREATE PROCEDURE AddNewUser(
IN first_name VARCHAR(255),
IN last_name VARCHAR(255),
IN email VARCHAR(255),
IN password TEXT,
IN username VARCHAR(255)
)
BEGIN
INSERT INTO users(first_name, last_name, email, password, username)
VALUES(first_name, last_name, email, password, username);
END$$
DELIMITER ;
-- Stored Procedure to Add a Transaction
DELIMITER $$
CREATE PROCEDURE AddTransaction(
IN user_id INT,
IN category_id INT,
IN amount DOUBLE,
IN transaction_date DATE,
IN description TEXT,
IN transaction_type VARCHAR(255)
)
BEGIN
INSERT INTO transactions(user_id, category_id, amount, transaction_date, description, transaction_type)
VALUES(user_id, category_id, amount, transaction_date, description, transaction_type);
-- Call function to update budget after transaction
CALL UpdateBudgetAfterTransaction(user_id, category_id, amount);
END$$
DELIMITER ;
-- Function to Update Budget After Transaction
DELIMITER $$
CREATE FUNCTION UpdateBudgetAfterTransaction(user_id INT, category_id INT, spent_amount DOUBLE)
RETURNS VOID
BEGIN
DECLARE remaining_budget FLOAT;
-- Get the budget for the given category and user
SELECT amount_limit - IFNULL(SUM(amount), 0)
INTO remaining_budget
FROM transactions
WHERE user_id = user_id AND category_id = category_id;
-- If remaining budget is less than the spent amount, update the budget
IF remaining_budget < 0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Budget exceeded!';
END IF;
-- Otherwise, update the budget
UPDATE budgets
SET amount_limit = amount_limit - spent_amount
WHERE user_id = user_id AND category_id = category_id;
END$$
DELIMITER ;
-- Trigger to Automatically Create Report After Adding a Transaction
DELIMITER $$
CREATE TRIGGER AfterTransactionInsert
AFTER INSERT ON transactions
FOR EACH ROW
BEGIN
INSERT INTO reports(user_id, report_type, report_date)
VALUES(NEW.user_id, CONCAT('Transaction Report for ', NEW.transaction_type), CURDATE());
END$$
DELIMITER ;
-- Function to Calculate the Total Spend per Category for a User
DELIMITER $$
CREATE FUNCTION GetTotalSpendPerCategory(user_id INT, category_id INT)
RETURNS DOUBLE
BEGIN
DECLARE total_spent DOUBLE;
SELECT IFNULL(SUM(amount), 0) INTO total_spent
FROM transactions
WHERE user_id = user_id AND category_id = category_id;
RETURN total_spent;
END$$
DELIMITER ;
-- Trigger to Validate Budget Exceedance When Adding a Transaction
DELIMITER $$
CREATE TRIGGER BeforeTransactionInsert
BEFORE INSERT ON transactions
FOR EACH ROW
BEGIN
DECLARE remaining_budget FLOAT;
-- Get the remaining budget for the given category and user
SELECT amount_limit - IFNULL(SUM(amount), 0)
INTO remaining_budget
FROM transactions
WHERE user_id = NEW.user_id AND category_id = NEW.category_id;
-- If transaction exceeds the budget, prevent the insert
IF remaining_budget < NEW.amount THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Budget exceeded!';
END IF;
END$$
DELIMITER ;