-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary Triggers.sql
More file actions
183 lines (164 loc) · 5.57 KB
/
Library Triggers.sql
File metadata and controls
183 lines (164 loc) · 5.57 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
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
create trigger trgReturnBook
on Library.BorrowRecords after update
as begin
-- Log return Book
insert into Library.Log(user_type, user_id, action)
select 'student', i.accountID, CONCAT('Returned book editionID = ', i.editionID)
from inserted i
join deleted d ON i.recordID = d.recordID
where d.status = 'not returned' AND i.status = 'returned';
-- set New Fine if needed
insert into Library.Fines(accountID, amount)
select i.accountID, 1000 from inserted i join deleted d on i.accountID = d.accountID
where i.returnDate > i.dueDate and i.status = 'returned' and d.status = 'not returned';
-- deactivate Account if needed
update Library.Account
set isActive = 0
from Library.Account join inserted i on Library.Account.accountID = i.accountID
join Library.getAllAccountsTotalFine() g on i.accountID = g.accountID
where isActive = 1 and g.sum > 2000;
end;
GO
create trigger trgLogAddBook
ON Library.Books after insert
as begin
insert into Library.Log(user_type, user_id, action)
select 'librarian', NULL, CONCAT('Book added: ', title)
from inserted;
end;
GO
create trigger trgLimitBorrow
ON Library.BorrowRecords
instead of insert
as begin
if exists (
select 1 from Library.BorrowRecords
where accountID in (select accountID from inserted) AND status = 'not returned'
group by accountID
having COUNT(*) >= 3
)
begin
print('Cannot borrow more than 3 books simultaneously.');
ROLLBACK;
end
else
begin
insert into Library.BorrowRecords(accountID, editionID, librarianID, borrowDate, dueDate, status)
select accountID, editionID, librarianID, borrowDate, dueDate, status from inserted;
end;
end;
GO
create trigger trgLogBorrowBook
ON Library.BorrowRecords after insert
as begin
insert into Library.Log(user_type, user_id, action)
select 'student', accountID, CONCAT('Borrowed book editionID = ', editionID)
from inserted;
end;
GO
create trigger trgLogExtendDueDate
ON Library.BorrowRecords
after update
as begin
insert into Library.Log(user_type, user_id, action)
select 'student', i.accountID, CONCAT('Extended due date for editionID = ', i.editionID)
from inserted i
join deleted d ON i.recordID = d.recordID
where i.dueDate > d.dueDate AND i.status = 'not returned';
end;
GO
create trigger trgLogPayFine
ON Library.Fines
after update
as begin
insert into Library.Log(user_type, user_id, action)
select 'student', accountID, CONCAT('Paid fineID = ', fineID)
from inserted
where status = 'paid';
end;
GO
create trigger trgLogAddPublisher
ON Library.Publisher
after insert
as begin
insert into Library.Log(user_type, user_id, action)
select 'librarian', NULL, CONCAT('Publisher added: ', name)
from inserted;
end;
GO
create trigger trgLogAddBookEdition
ON Library.BooksAvailable
after insert
as begin
insert into Library.Log(user_type, user_id, action)
select 'librarian', NULL, CONCAT('Added edition: BookID = ', bookID, ', Edition = ', editionNumber)
from inserted;
end;
GO
create trigger trgLogAddCategory
ON Library.Category
after insert
AS
begin
insert into Library.Log(user_type, user_id, action)
select 'librarian', NULL, CONCAT('Category added: ', name)
from inserted;
end;
GO
create trigger trgLogShelfLocationChange
ON Library.ShelfLocation
after insert, update
AS
begin
DECLARE @action VARCHAR(255);
-- Log for inserted records
if exists (select * from inserted) AND NOT exists (select * from deleted)
begin
insert into Library.Log(user_type, user_id, action)
select
'librarian',
NULL, -- Assuming a librarian performs this action
CONCAT('set location for editionID = ', i.editionID, ' to Aisle: ', i.aisle, ', Shelf: ', i.shelfNumber)
from inserted i;
end
-- Log for updated records
if exists (select * from inserted) AND exists (select * from deleted)
begin
insert into Library.Log(user_type, user_id, action)
select
'librarian',
NULL,
CONCAT('Changed location for editionID = ', i.editionID, ' from Aisle: ', d.aisle, ', Shelf: ', d.shelfNumber, ' to Aisle: ', i.aisle, ', Shelf: ', i.shelfNumber)
from inserted i
join deleted d ON i.locationID = d.locationID;
end
end;
GO
create trigger Library.trgReactivateAccountOnFinePayment
ON Library.Fines
after update
AS
begin
DECLARE @accountID INT;
DECLARE @totalFine INT;
DECLARE @deactivationThreshold INT = 2000; -- Same threshold from your other trigger
-- Check if a fine was just paid
if update(status) AND exists (select 1 from inserted i join deleted d ON i.fineID = d.fineID where i.status = 'paid' AND d.status = 'notPaid')
begin
-- Get the account ID for the paid fine
select @accountID = accountID from inserted;
-- Calculate the remaining total fine for this account
set @totalFine = ISNULL(Library.getTotalFineByAccID(@accountID), 0);
-- if the account is currently inactive and the total fine is now below the threshold, reactivate it
if exists (select 1 from Library.Account where accountID = @accountID AND isActive = 0) AND @totalFine < @deactivationThreshold
begin
update Library.Account
set isActive = 1
where accountID = @accountID;
-- Log the reactivation
insert into Library.Log (user_type, user_id, action)
VALUES ('system', @accountID, 'Account has been automatically reactivated due to fine payment.');
end
end
end;
GO