-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.sql
395 lines (323 loc) · 9.32 KB
/
script.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
USE BookShoppingCartMvc_Dapper;
GO
-- Genre data
SET IDENTITY_INSERT [dbo].[Genre] ON
GO
INSERT [dbo].[Genre] ([Id], [GenreName]) VALUES (1, N'Romance')
GO
INSERT [dbo].[Genre] ([Id], [GenreName]) VALUES (2, N'Action')
GO
INSERT [dbo].[Genre] ([Id], [GenreName]) VALUES (3, N'Thriller')
GO
INSERT [dbo].[Genre] ([Id], [GenreName]) VALUES (4, N'Crime')
GO
INSERT [dbo].[Genre] ([Id], [GenreName]) VALUES (5, N'SelfHelp')
GO
INSERT [dbo].[Genre] ([Id], [GenreName]) VALUES (6, N'Programming')
GO
SET IDENTITY_INSERT [dbo].[Genre] OFF
GO
-- Book Data
INSERT INTO book (BookName, AuthorName, Price, GenreId)
VALUES
('Pride and Prejudice', 'Jane Austen', 12.99, 1),
('The Notebook', 'Nicholas Sparks', 11.99, 1),
('Outlander', 'Diana Gabaldon', 14.99, 1),
('Me Before You', 'Jojo Moyes', 10.99, 1),
('The Fault in Our Stars', 'John Green', 9.99, 1);
-- Inserting rows for Action genre
INSERT INTO book (BookName, AuthorName, Price, GenreId)
VALUES
('The Bourne Identity', 'Robert Ludlum', 14.99, 2),
('Die Hard', 'Roderick Thorp', 13.99, 2),
('Jurassic Park', 'Michael Crichton', 15.99, 2),
('The Da Vinci Code', 'Dan Brown', 12.99, 2),
('The Hunger Games', 'Suzanne Collins', 11.99, 2);
-- Inserting rows for Thriller genre
INSERT INTO book (BookName, AuthorName, Price, GenreId)
VALUES
('Gone Girl', 'Gillian Flynn', 11.99, 3),
('The Girl with the Dragon Tattoo', 'Stieg Larsson', 10.99, 3),
('The Silence of the Lambs', 'Thomas Harris', 12.99, 3),
('Before I Go to Sleep', 'S.J. Watson', 9.99, 3),
('The Girl on the Train', 'Paula Hawkins', 13.99, 3);
-- Inserting rows for Crime genre
INSERT INTO book (BookName, AuthorName, Price, GenreId)
VALUES
('The Godfather', 'Mario Puzo', 13.99, 4),
('The Girl with the Dragon Tattoo', 'Stieg Larsson', 12.99, 4),
('The Cuckoo''s Calling', 'Robert Galbraith', 14.99, 4),
('In Cold Blood', 'Truman Capote', 11.99, 4),
('The Silence of the Lambs', 'Thomas Harris', 15.99, 4);
-- Inserting rows for SelfHelp genre
INSERT INTO book (BookName, AuthorName, Price, GenreId)
VALUES
('The 7 Habits of Highly Effective People', 'Stephen R. Covey', 9.99, 5),
('How to Win Friends and Influence People', 'Dale Carnegie', 8.99, 5),
('Atomic Habits', 'James Clear', 10.99, 5),
('The Subtle Art of Not Giving a F\*ck', 'Mark Manson', 7.99, 5),
('You Are a Badass', 'Jen Sincero', 11.99, 5);
-- Inserting rows for Programming genre
INSERT INTO book (BookName, AuthorName, Price, GenreId)
VALUES
('Clean Code', 'Robert C. Martin', 19.99, 6),
('Design Patterns', 'Erich Gamma', 17.99, 6),
('Code Complete', 'Steve McConnell', 21.99, 6),
('The Pragmatic Programmer', 'Andrew Hunt', 18.99, 6),
('Head First Design Patterns', 'Eric Freeman', 20.99, 6);
-- update stock
INSERT INTO Stock
(BookId,Quantity)
SELECT
b.Id,
10
FROM Book b
WHERE NOT EXISTS (
SELECT * FROM [Stock]
);
-- Order status
GO
SET IDENTITY_INSERT [dbo].[OrderStatus] ON
GO
INSERT [dbo].[OrderStatus] ([Id], [StatusId], [StatusName]) VALUES (1, 1, N'Pending')
GO
INSERT [dbo].[OrderStatus] ([Id], [StatusId], [StatusName]) VALUES (2, 2, N'Shipped')
GO
INSERT [dbo].[OrderStatus] ([Id], [StatusId], [StatusName]) VALUES (3, 3, N'Delivered')
GO
INSERT [dbo].[OrderStatus] ([Id], [StatusId], [StatusName]) VALUES (4, 4, N'Cancelled')
GO
INSERT [dbo].[OrderStatus] ([Id], [StatusId], [StatusName]) VALUES (5, 5, N'Returned')
GO
INSERT [dbo].[OrderStatus] ([Id], [StatusId], [StatusName]) VALUES (6, 6, N'Refund')
GO
SET IDENTITY_INSERT [dbo].[OrderStatus] OFF
GO
-- Stored procedures
USE [BookShoppingCartMvc_Dapper]
GO
CREATE OR ALTER PROCEDURE [dbo].[AddItemToCart]
@BookId INT,
@Quantity INT,
@UserId NVARCHAR(450)
AS
BEGIN
BEGIN TRANSACTION;
BEGIN TRY
IF @UserId is null or @UserId = ''
BEGIN
RAISERROR('User is not logged in', 16, 1);
RETURN -1;
END
DECLARE @CartId INT;
SELECT @CartId = Id
FROM ShoppingCart
WHERE UserId = @UserId;
IF @CartId IS NULL
begin
INSERT INTO ShoppingCart(UserId,IsDeleted)
VALUES (@UserId,0);
SET @CartId = SCOPE_IDENTITY();
end
-- Check if item already exists in cart
DECLARE @ExistingCartDetailId INT;
DECLARE @ExistingQuantity INT;
SELECT
@ExistingCartDetailId = Id,
@ExistingQuantity = Quantity
FROM CartDetail
where ShoppingCartId=@CartId and BookId=@BookId
-- Get book price for new cart items
DECLARE @UnitPrice DECIMAL(18,2);
SELECT @UnitPrice = Price
FROM Book
WHERE Id = @BookId;
-- Update existing cart item or insert new
if @ExistingCartDetailId is not null
begin
UPDATE CartDetail
set Quantity = Quantity+1
Where Id= @ExistingCartDetailId;
end
else
begin
INSERT INTO CartDetail (ShoppingCartId, BookId, Quantity, UnitPrice)
VALUES (@CartId, @BookId, @Quantity, @UnitPrice);
end
-- Get total cart item count for return
DECLARE @CartItemCount INT;
SELECT
@CartItemCount=Count(1)
FROM CartDetail
Where ShoppingCartId=@CartId
COMMIT TRANSACTION;
RETURN @CartItemCount;
END TRY
BEGIN CATCH
IF @@TRANCOUNT>0
ROLLBACK TRANSACTION;
THROW;
END CATCH
end
GO
create or alter procedure RemoveCartItem
@UserId nvarchar(100),
@BookId int
as
begin
-- Ensure @UserId is not null
if (@UserId is null or @UserId = '')
begin
raiserror('@UserId is null',16,1);
end
-- check whether a user has cart or not
declare @CartId INT;
select
@CartId=s.Id
from ShoppingCart s
where s.UserId = @UserId;
if @CartId is null
begin
raiserror('Invalid cart',16,1);
end
declare @CartItemQuantity int;
declare @CartDetailId int;
select
@CartDetailId = cd.Id,
@CartItemQuantity = cd.Quantity
from CartDetail cd
where cd.ShoppingCartId=@CartId and cd.BookId=@BookId;
if @CartItemQuantity is null
begin
raiserror('No items in cart',16,1);
end
else if(@CartItemQuantity = 1)
begin
delete from CartDetail
where Id=@CartDetailId;
end
else
begin
update CartDetail
set Quantity = Quantity - 1
where Id=@CartDetailId;
end
-- return the cart items
declare @CartItemCount int;
select
@CartItemCount = Count(1)
from CartDetail cd
where cd.ShoppingCartId= @CartId;
return @CartItemCount;
end
go
-- checkout script
CREATE OR ALTER PROCEDURE Checkout
@Name NVARCHAR(30),
@Email NVARCHAR(30),
@MobileNumber NVARCHAR(15),
@Address NVARCHAR(200),
@PaymentMethod NVARCHAR(20),
@UserId NVARCHAR(200)
AS
BEGIN
SET NOCOUNT ON;
-- Input validation (existing checks)
IF (@Name IS NULL OR @Name = '')
THROW 50001, 'Name cannot be null', 1;
IF (@Email IS NULL OR @Email = '')
THROW 50002, 'Email cannot be null', 1;
IF (@MobileNumber IS NULL OR @MobileNumber = '')
THROW 50003, 'Mobile Number cannot be null', 1;
IF (@Address IS NULL OR @Address = '')
THROW 50004, 'Address cannot be null', 1;
IF (@PaymentMethod IS NULL OR @PaymentMethod = '')
THROW 50005, 'Payment Method cannot be null', 1;
IF (@UserId IS NULL OR @UserId = '')
THROW 50006, 'User ID cannot be null', 1;
BEGIN TRY
BEGIN TRANSACTION;
-- Get Cart Information
DECLARE @CartId INT;
SELECT @CartId = Id
FROM ShoppingCart
WHERE UserId = @UserId;
IF @CartId IS NULL
THROW 50007, 'Invalid cart', 1;
IF NOT EXISTS (SELECT 1 FROM CartDetail WHERE ShoppingCartId = @CartId)
THROW 50008, 'Cart is empty', 1;
-- Check Stock Availability
IF EXISTS (
SELECT 1
FROM CartDetail cd
INNER JOIN Stock s ON cd.BookId = s.BookId
WHERE cd.ShoppingCartId = @CartId AND cd.Quantity > s.Quantity
)
THROW 50009, 'Insufficient stock for one or more items', 1;
-- Get Pending Order Status
DECLARE @PendingOrderStatusId INT;
SELECT @PendingOrderStatusId = Id
FROM OrderStatus
WHERE StatusName = 'Pending';
IF @PendingOrderStatusId IS NULL
THROW 50010, 'No ''Pending'' order status found', 1;
-- Create Order
INSERT INTO [Order]
(CreateDate, [Address], Email, IsDeleted, IsPaid, MobileNumber, [Name], OrderStatusId, PaymentMethod, UserId)
VALUES
(CURRENT_TIMESTAMP, @Address, @Email, 0, 0, @Name, @MobileNumber, @PendingOrderStatusId, @PaymentMethod, @UserId);
DECLARE @CreatedOrderId INT = SCOPE_IDENTITY();
-- Create Order Details
INSERT INTO OrderDetail
(BookId, OrderId, Quantity, UnitPrice)
SELECT
cd.BookId,
@CreatedOrderId,
cd.Quantity,
cd.UnitPrice
FROM CartDetail cd
WHERE cd.ShoppingCartId = @CartId;
-- Update Stock
UPDATE Stock
SET Quantity = Stock.Quantity - OrderDetail.Quantity
FROM Stock
INNER JOIN OrderDetail ON Stock.BookId = OrderDetail.BookId
WHERE OrderDetail.OrderId = @CreatedOrderId;
-- Remove Cart Items
DELETE FROM CartDetail
WHERE ShoppingCartId = @CartId;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION;
THROW;
END CATCH
END
-- get books for home page
create or alter procedure [dbo].[USP_GetBooksForHomePage]
@search_term nvarchar(30)='',
@genre_id int = 0
as
begin
set @search_term = case when @search_term='' then 'show_all' else @search_term end;
SELECT
b.Id,
b.[Image],
b.AuthorName,
b.BookName,
b.GenreId,
b.Price,
g.GenreName,
COALESCE(s.Quantity,0) as Quantity
FROM Book b
inner join Genre g
on b.GenreId = g.Id
left outer join Stock s
on b.Id = s.BookId
Where
(@search_term='show_all' OR CONTAINS(b.BookName,@search_term))
AND (@genre_id = 0 OR b.GenreId = @genre_id)
ORDER BY b.BookName ASC;
end
GO