-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArtist Queries.sql
More file actions
1346 lines (1138 loc) · 40.3 KB
/
Copy pathArtist Queries.sql
File metadata and controls
1346 lines (1138 loc) · 40.3 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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--DROP PROCEDURE IF EXISTS AddNewArtist;
--CREATE PROCEDURE AddNewArtist
-- @Username VARCHAR(50),
-- @Password VARCHAR(50),
-- @BirthYear INT,
-- @Name VARCHAR(100),
-- @Email VARCHAR(100),
-- @Location VARCHAR(100),
-- @SubscriptionDate DATE,
-- @ArtName VARCHAR(100)
--AS
--BEGIN
-- DECLARE @UserID INT;
-- BEGIN TRY
-- -- Start transaction
-- BEGIN TRANSACTION;
-- -- Check if the Username is unique
-- IF EXISTS (SELECT 1 FROM Users WHERE Username = @Username)
-- BEGIN
-- RAISERROR('Username already exists.', 16, 1);
-- ROLLBACK TRANSACTION;
-- RETURN;
-- END
-- -- Check if the Email is unique
-- IF EXISTS (SELECT 1 FROM Users WHERE Email = @Email)
-- BEGIN
-- RAISERROR('Email already exists.', 16, 1);
-- ROLLBACK TRANSACTION;
-- RETURN;
-- END
-- -- Insert into Users table
-- INSERT INTO Users (Username, Password, BirthYear, Name, Email, Location, UserType)
-- VALUES (@Username, @Password, @BirthYear, @Name, @Email, @Location, 'Artist');
-- -- Get the UserID of the newly inserted user
-- SET @UserID = SCOPE_IDENTITY();
-- -- Insert into Premium table
-- INSERT INTO Premium (UserID, SubscriptionDate)
-- VALUES (@UserID, @SubscriptionDate);
-- -- Insert into Artist table
-- INSERT INTO Artist (artist_id, [Art Name])
-- VALUES (@UserID, @ArtName);
-- -- Insert into Wallets table
-- INSERT INTO Wallets (user_id, balance)
-- VALUES (@UserID, 1000.00); -- Initial balance
-- -- Commit transaction
-- COMMIT TRANSACTION;
-- END TRY
-- BEGIN CATCH
-- -- Rollback transaction in case of error
-- ROLLBACK TRANSACTION;
-- -- Raise the error again to notify the caller
-- THROW;
-- END CATCH
--END;
--EXEC AddNewArtist
-- @Username = 'gha',
-- @Password = 'password123',
-- @BirthYear = 1890,
-- @Name = 'Taylor Swift',
-- @Email = 'john.de@example.com',
-- @Location = 'California',
-- @SubscriptionDate = '2024-07-10',
-- @ArtName = 'Taylor';
---------------------------------------------------------------------------------
--DROP PROCEDURE IF EXISTS AddNewMusic;
--CREATE PROCEDURE AddNewMusic
-- @title VARCHAR(255),
-- @duration TIME,
-- @genre VARCHAR(255),
-- @zone VARCHAR(50) = NULL,
-- @age_category VARCHAR(50) = NULL,
-- @can_add_to_playlist BIT = 1,
-- @album_id INT = NULL, -- Existing parameter for album_id
-- @artist_id INT -- New parameter for artist_id
--AS
--BEGIN
-- -- Check if album_id exists in Album table
-- IF @album_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM Album WHERE album_id = @album_id)
-- BEGIN
-- -- If album_id is invalid, set it to NULL
-- SET @album_id = NULL;
-- END
-- -- Insert the new music record
-- INSERT INTO Music (title, duration, genre, zone, age_category, can_add_to_playlist, album_id)
-- VALUES (@title, @duration, @genre, @zone, @age_category, @can_add_to_playlist, @album_id);
-- -- Get the ID of the newly inserted music record
-- DECLARE @new_music_id INT;
-- SET @new_music_id = SCOPE_IDENTITY();
-- -- Insert the record into the Music_Artist table
-- INSERT INTO Music_Artist (music_id, artist_id)
-- VALUES (@new_music_id, @artist_id);
--END;
--EXEC AddNewMusic
-- @title = 'New Song with Album',
-- @duration = '00:04:20',
-- @genre = 'Jazz',
-- @zone = 'EU',
-- @age_category = 'Adult',
-- @can_add_to_playlist = 1,
-- @album_id = 5, -- Assuming 5 is a valid album_id
-- @artist_id = 1; -- Assuming 7 is a valid artist_id
----------------------------------------------------------------------------------------------------------------------
--DROP PROCEDURE IF EXISTS AddNewAlbum;
--CREATE PROCEDURE AddNewAlbum
-- @AlbumTitle VARCHAR(255),
-- @ReleaseDate DATE,
-- @AlbumGenre VARCHAR(255),
-- @AlbumZone VARCHAR(255),
-- @AlbumAgeCategory VARCHAR(255),
-- @ArtistID INT,
-- @MusicTracks dbo.MusicTrackType READONLY
--AS
--BEGIN
-- DECLARE @AlbumID INT;
-- BEGIN TRY
-- -- Start transaction
-- BEGIN TRANSACTION;
-- -- Insert into Album table
-- INSERT INTO Album (title, release_date, genre, zone, age_category)
-- VALUES (@AlbumTitle, @ReleaseDate, @AlbumGenre, @AlbumZone, @AlbumAgeCategory);
-- -- Get the AlbumID of the newly inserted album
-- SET @AlbumID = SCOPE_IDENTITY();
-- -- Insert into Album_Artist table
-- INSERT INTO Album_Artist (album_id, artist_id)
-- VALUES (@AlbumID, @ArtistID);
-- -- Insert into Music table and capture inserted IDs
-- CREATE TABLE #InsertedMusic (music_id INT, title VARCHAR(255));
-- INSERT INTO Music (title, duration, genre, zone, age_category, can_add_to_playlist, album_id)
-- OUTPUT inserted.music_id, inserted.title INTO #InsertedMusic
-- SELECT title, duration, genre, zone, age_category, can_add_to_playlist, @AlbumID
-- FROM @MusicTracks;
-- -- Insert into Music_Artist table using captured IDs
-- INSERT INTO Music_Artist (music_id, artist_id)
-- SELECT music_id, @ArtistID
-- FROM #InsertedMusic;
-- -- Drop temporary table
-- DROP TABLE #InsertedMusic;
-- -- Commit transaction
-- COMMIT TRANSACTION;
-- END TRY
-- BEGIN CATCH
-- -- Rollback transaction in case of error
-- ROLLBACK TRANSACTION;
-- -- Raise the error again to notify the caller
-- THROW;
-- END CATCH
--END;
---- Define the table type parameter and populate with example tracks
--DECLARE @Tracks dbo.MusicTrackType;
--INSERT INTO @Tracks (title, duration, genre, zone, age_category, can_add_to_playlist)
--VALUES
-- ('Track dddaaa', '00:03:30', 'JAZZ', 'Zone 1', '18+', 1),
-- ('Track bjj22', '00:04:00', 'Rock', 'Zone 1', '18+', 1),
-- ('Track 33c', '00:02:45', 'Rock', 'Zone 1', '18+', 1);
---- Execute the stored procedure AddNewAlbum
--EXEC AddNewAlbum
-- @AlbumTitle = 'SSS',
-- @ReleaseDate = '2022-09-11',
-- @AlbumGenre = 'Classic',
-- @AlbumZone = 'Zone 0',
-- @AlbumAgeCategory = '2+',
-- @ArtistID = 2, -- Assuming 5 is a valid artist_id
-- @MusicTracks = @Tracks;
----------------------------------------------------------------------------------------------------------------------
--CREATE PROCEDURE RemoveSong
-- @MusicID INT
--AS
--BEGIN
-- BEGIN TRY
-- -- Start transaction
-- BEGIN TRANSACTION;
-- -- Check if the music exists
-- IF NOT EXISTS (SELECT 1 FROM Music WHERE music_id = @MusicID)
-- BEGIN
-- -- Rollback transaction
-- ROLLBACK TRANSACTION;
-- -- Raise an error
-- THROW 50001, 'Music record not found', 1;
-- RETURN;
-- END
-- -- Delete from Music table
-- DELETE FROM Music WHERE music_id = @MusicID;
-- -- Commit transaction
-- COMMIT TRANSACTION;
-- END TRY
-- BEGIN CATCH
-- -- Rollback transaction in case of error
-- IF @@TRANCOUNT > 0
-- ROLLBACK TRANSACTION;
-- -- Get error information
-- DECLARE @ErrorMessage NVARCHAR(4000), @ErrorSeverity INT, @ErrorState INT;
-- SELECT
-- @ErrorMessage = ERROR_MESSAGE(),
-- @ErrorSeverity = ERROR_SEVERITY(),
-- @ErrorState = ERROR_STATE();
-- -- Raise the error again to notify the caller
-- RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState);
-- END CATCH
--END;
--EXEC RemoveSong @MusicID = 1; -- Replace 1 with the actual MusicID of the song to be removed
------------------------------------------------------------------------------------------------------------------
--CREATE PROCEDURE RemoveAlbum
-- @AlbumID INT
--AS
--BEGIN
-- BEGIN TRY
-- -- Start transaction
-- BEGIN TRANSACTION;
-- -- Check if the album exists
-- IF NOT EXISTS (SELECT 1 FROM Album WHERE album_id = @AlbumID)
-- BEGIN
-- -- Raise an error and rollback transaction
-- THROW 50001, 'Album record not found', 1;
-- END
-- -- Delete the album (this will cascade delete related records)
-- DELETE FROM Album WHERE album_id = @AlbumID;
-- -- Commit transaction
-- COMMIT TRANSACTION;
-- END TRY
-- BEGIN CATCH
-- -- Rollback transaction in case of error
-- ROLLBACK TRANSACTION;
-- -- Return error message
-- DECLARE @ErrorMessage NVARCHAR(4000) = ERROR_MESSAGE();
-- DECLARE @ErrorSeverity INT = ERROR_SEVERITY();
-- DECLARE @ErrorState INT = ERROR_STATE();
-- RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState);
-- END CATCH
--END;
--EXEC RemoveAlbum @AlbumID = 8;
------------------------------------------------------------------------------------------------------------------
--DROP PROCEDURE IF EXISTS AddNewConcertWithTickets
--CREATE PROCEDURE AddNewConcertWithTickets
-- @ArtistID INT,
-- @ConcertDate DATE,
-- @ConcertLocation NVARCHAR(255),
-- @RegularTicketPrice DECIMAL(10, 2),
-- @VIPTicketPrice DECIMAL(10, 2),
-- @PremiumTicketPrice DECIMAL(10, 2),
-- @NumRegularTickets INT,
-- @NumVIPTickets INT,
-- @NumPremiumTickets INT
--AS
--BEGIN
-- BEGIN TRY
-- -- Start transaction
-- BEGIN TRANSACTION;
-- -- Verify artist exists
-- IF NOT EXISTS (SELECT 1 FROM Artist WHERE artist_id = @ArtistID)
-- BEGIN
-- THROW 50002, 'Artist ID is invalid.', 1;
-- END
-- -- Check if the artist already has a concert on the given date
-- IF EXISTS (
-- SELECT 1
-- FROM Concert_Artists ca
-- INNER JOIN Concerts c ON ca.concert_id = c.concert_id
-- WHERE ca.artist_id = @ArtistID AND c.date = @ConcertDate
-- )
-- BEGIN
-- THROW 50003, 'Artist already has a concert scheduled on this date.', 1;
-- END
-- -- Insert new concert
-- INSERT INTO Concerts (
-- date, location, is_cancelled,
-- NumRegularTickets, NumVIPTickets, NumPremiumTickets,
-- RegularTicketPrice, VIPTicketPrice, PremiumTicketPrice
-- )
-- VALUES (
-- @ConcertDate, @ConcertLocation, 0,
-- @NumRegularTickets, @NumVIPTickets, @NumPremiumTickets,
-- @RegularTicketPrice, @VIPTicketPrice, @PremiumTicketPrice
-- );
-- -- Get the ID of the newly inserted concert
-- DECLARE @NewConcertID INT = SCOPE_IDENTITY();
-- -- Insert into Concert_Artists
-- INSERT INTO Concert_Artists (concert_id, artist_id)
-- VALUES (@NewConcertID, @ArtistID);
-- -- Insert Regular Tickets
-- DECLARE @i INT = 1;
-- WHILE @i <= @NumRegularTickets
-- BEGIN
-- INSERT INTO Tickets (user_id, concert_id, is_valid, is_purchased, ticket_type)
-- VALUES (NULL, @NewConcertID, 1, 0, 'Regular');
-- SET @i = @i + 1;
-- END
-- -- Insert VIP Tickets
-- SET @i = 1;
-- WHILE @i <= @NumVIPTickets
-- BEGIN
-- INSERT INTO Tickets (user_id, concert_id, is_valid, is_purchased, ticket_type)
-- VALUES (NULL, @NewConcertID, 1, 0, 'VIP');
-- SET @i = @i + 1;
-- END
-- -- Insert Premium Tickets
-- SET @i = 1;
-- WHILE @i <= @NumPremiumTickets
-- BEGIN
-- INSERT INTO Tickets (user_id, concert_id, is_valid, is_purchased, ticket_type)
-- VALUES (NULL, @NewConcertID, 1, 0, 'Premium');
-- SET @i = @i + 1;
-- END
-- -- Commit transaction
-- COMMIT TRANSACTION;
-- END TRY
-- BEGIN CATCH
-- -- Rollback transaction in case of error
-- ROLLBACK TRANSACTION;
-- -- Raise the error again to notify the caller
-- THROW;
-- END CATCH
--END;
-- Example of executing the procedure
--EXEC AddNewConcertWithTickets
-- @ArtistID = 2,
-- @ConcertDate = '2020-10-05',
-- @ConcertLocation = 'Concert Hall XX',
-- @RegularTicketPrice = 60.00,
-- @VIPTicketPrice = 12.00,
-- @PremiumTicketPrice = 200.00,
-- @NumRegularTickets = 1,
-- @NumVIPTickets = 1,
-- @NumPremiumTickets = 2;
------------------------------------------------------------------------------------------------------------------
--CREATE PROCEDURE GetSongsByArtist
-- @artist_id INT
--AS
--BEGIN
-- -- Check if the artist_id is valid
-- IF NOT EXISTS (SELECT 1 FROM Artist WHERE artist_id = @artist_id)
-- BEGIN
-- RAISERROR ('Invalid artist ID.', 16, 1);
-- RETURN;
-- END
-- -- Check if the artist has any songs published
-- IF NOT EXISTS (
-- SELECT 1
-- FROM Music_Artist ma
-- WHERE ma.artist_id = @artist_id
-- )
-- BEGIN
-- RAISERROR ('The artist has no music published.', 16, 1);
-- RETURN;
-- END
-- -- Get all songs published by the valid artist
-- SELECT
-- m.music_id,
-- m.title,
-- m.duration,
-- m.genre,
-- m.zone,
-- m.age_category,
-- m.album_id
-- FROM
-- Music m
-- INNER JOIN
-- Music_Artist ma ON m.music_id = ma.music_id
-- WHERE
-- ma.artist_id = @artist_id;
--END;
---- Declare variables for testing
--DECLARE @artist_id INT = 1;
---- Execute the stored procedure
--EXEC GetSongsByArtist @artist_id;
------------------------------------------------------------------------------------------------------------------
--CREATE PROCEDURE GetAlbumsByArtist
-- @artist_id INT
--AS
--BEGIN
-- -- Check if the artist_id is valid
-- IF NOT EXISTS (SELECT 1 FROM Artist WHERE artist_id = @artist_id)
-- BEGIN
-- RAISERROR ('Invalid artist ID.', 16, 1);
-- RETURN;
-- END
-- -- Check if the artist has any albums published
-- IF NOT EXISTS (
-- SELECT 1
-- FROM Album_Artist aa
-- WHERE aa.artist_id = @artist_id
-- )
-- BEGIN
-- RAISERROR ('The artist has no albums published.', 16, 1);
-- RETURN;
-- END
-- -- Get albums by the artist
-- SELECT
-- a.album_id,
-- a.title AS album_title,
-- a.release_date,
-- a.genre AS album_genre
-- FROM
-- Album a
-- INNER JOIN
-- Album_Artist aa ON a.album_id = aa.album_id
-- WHERE
-- aa.artist_id = @artist_id;
--END;
------------------------------------------------------------------------------------------------------------------
--CREATE PROCEDURE GetMusicByAlbum
-- @album_id INT
--AS
--BEGIN
-- -- Check if the album_id is valid
-- IF NOT EXISTS (SELECT 1 FROM Album WHERE album_id = @album_id)
-- BEGIN
-- RAISERROR ('Invalid album ID.', 16, 1);
-- RETURN;
-- END
-- -- Retrieve music tracks of the album
-- SELECT
-- m.music_id,
-- m.title AS music_title,
-- m.duration,
-- m.genre AS music_genre,
-- m.zone,
-- m.age_category
-- FROM
-- Music m
-- WHERE
-- m.album_id = @album_id;
--END;
------------------------------------------------------------------------------------------------------------------
--DROP PROCEDURE IF EXISTS SendMessage;
--CREATE PROCEDURE SendMessage
-- @SenderID INT,
-- @ReceiverID INT,
-- @MessageContent NVARCHAR(MAX)
--AS
--BEGIN
-- BEGIN TRY
-- -- Start transaction
-- BEGIN TRANSACTION;
-- -- Check if SenderID exists in Users table
-- IF NOT EXISTS (SELECT 1 FROM Users WHERE ID = @SenderID)
-- BEGIN
-- THROW 50000, 'Sender ID is invalid.', 1;
-- END;
-- -- Check if ReceiverID exists in Users table
-- IF NOT EXISTS (SELECT 1 FROM Users WHERE ID = @ReceiverID)
-- BEGIN
-- THROW 50001, 'Receiver ID is invalid.', 1;
-- END;
-- -- Check if the users are friends and the friendship request is accepted
-- IF NOT EXISTS (
-- SELECT 1
-- FROM Friends
-- WHERE (UserID = @SenderID AND FriendID = @ReceiverID AND Status = 'Accepted')
-- OR (UserID = @ReceiverID AND FriendID = @SenderID AND Status = 'Accepted')
-- )
-- BEGIN
-- THROW 50002, 'Users are not friends or friendship request is not accepted.', 1;
-- END;
-- -- Insert the message into the Chat table
-- INSERT INTO Chat (sender_id, receiver_id, message_content, sent_at)
-- VALUES (@SenderID, @ReceiverID, @MessageContent, GETDATE());
-- -- Commit transaction
-- COMMIT TRANSACTION;
-- END TRY
-- BEGIN CATCH
-- -- Rollback transaction in case of error
-- ROLLBACK TRANSACTION;
-- -- Raise the error again to notify the caller
-- THROW;
-- END CATCH
--END;
---- Example test case
--DECLARE @SenderID INT = 2;
--DECLARE @ReceiverID INT = 1;
--DECLARE @MessageContent NVARCHAR(MAX) = 'Hello, friend!';
--EXEC SendMessage @SenderID, @ReceiverID, @MessageContent;
------------------------------------------------------------------------------------------------------------------
--DROP PROCEDURE IF EXISTS AddToFavoriteSongs
--CREATE PROCEDURE AddToFavoriteSongs
-- @user_id INT,
-- @song_id INT
--AS
--BEGIN
-- BEGIN TRY
-- -- Start transaction
-- BEGIN TRANSACTION;
-- -- Check if the song exists
-- IF EXISTS (SELECT 1 FROM Music WHERE music_id = @song_id)
-- BEGIN
-- -- Check if the user exists and is a Premium user
-- IF EXISTS (SELECT 1 FROM Premium WHERE UserID = @user_id)
-- BEGIN
-- -- Check if the song is already in the user's favorite songs
-- IF NOT EXISTS (SELECT 1 FROM Favorite_Songs WHERE user_id = @user_id AND song_id = @song_id)
-- BEGIN
-- -- Insert the song into the Favorite_Songs table
-- INSERT INTO Favorite_Songs (user_id, song_id)
-- VALUES (@user_id, @song_id);
-- -- Commit transaction
-- COMMIT TRANSACTION;
-- END
-- ELSE
-- BEGIN
-- -- Song is already in the favorite list
-- ROLLBACK TRANSACTION;
-- RAISERROR ('Song is already in the favorite list.', 16, 1);
-- END
-- END
-- ELSE
-- BEGIN
-- -- User does not exist or is not a Premium user
-- ROLLBACK TRANSACTION;
-- RAISERROR ('User does not exist or is not a Premium user.', 16, 1);
-- END
-- END
-- ELSE
-- BEGIN
-- -- Song does not exist
-- ROLLBACK TRANSACTION;
-- RAISERROR ('Song does not exist.', 16, 1);
-- END
-- END TRY
-- BEGIN CATCH
-- -- Rollback transaction in case of error
-- IF @@TRANCOUNT > 0
-- ROLLBACK TRANSACTION;
-- -- Retrieve error information
-- DECLARE @ErrorMessage NVARCHAR(4000);
-- DECLARE @ErrorSeverity INT;
-- DECLARE @ErrorState INT;
-- SELECT
-- @ErrorMessage = ERROR_MESSAGE(),
-- @ErrorSeverity = ERROR_SEVERITY(),
-- @ErrorState = ERROR_STATE();
-- -- Raise the error again to notify the caller
-- RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState);
-- END CATCH
--END;
---- Testing the AddToFavoriteSongs procedure
--DECLARE @user_id INT = 2;
--DECLARE @song_id INT = 34;
---- Execute the procedure
--EXEC AddToFavoriteSongs @user_id, @song_id;
---------------------------------------------------------------------------------------------------------
--CREATE PROCEDURE AddToFavoritePlaylists
-- @user_id INT,
-- @playlist_id INT
--AS
--BEGIN
-- BEGIN TRY
-- -- Start transaction
-- BEGIN TRANSACTION;
-- -- Check if the playlist exists
-- IF EXISTS (SELECT 1 FROM Playlist WHERE playlist_id = @playlist_id)
-- BEGIN
-- -- Check if the user exists
-- IF EXISTS (SELECT 1 FROM Premium WHERE UserID = @user_id)
-- BEGIN
-- -- Check if the playlist is already in the user's favorite playlists
-- IF NOT EXISTS (SELECT 1 FROM Favorite_Playlists WHERE user_id = @user_id AND playlist_id = @playlist_id)
-- BEGIN
-- -- Insert the playlist into the Favorite_Playlists table
-- INSERT INTO Favorite_Playlists (user_id, playlist_id)
-- VALUES (@user_id, @playlist_id);
-- -- Commit transaction
-- COMMIT TRANSACTION;
-- END
-- ELSE
-- BEGIN
-- -- Playlist is already in the favorites
-- ROLLBACK TRANSACTION;
-- RAISERROR ('Playlist is already in the favorites.', 16, 1);
-- END
-- END
-- ELSE
-- BEGIN
-- -- User does not exist
-- ROLLBACK TRANSACTION;
-- RAISERROR ('User does not exist.', 16, 1);
-- END
-- END
-- ELSE
-- BEGIN
-- -- Playlist does not exist
-- ROLLBACK TRANSACTION;
-- RAISERROR ('Playlist does not exist.', 16, 1);
-- END
-- END TRY
-- BEGIN CATCH
-- -- Rollback transaction in case of error
-- IF @@TRANCOUNT > 0
-- ROLLBACK TRANSACTION;
-- -- Retrieve error information
-- DECLARE @ErrorMessage NVARCHAR(4000);
-- DECLARE @ErrorSeverity INT;
-- DECLARE @ErrorState INT;
-- SELECT
-- @ErrorMessage = ERROR_MESSAGE(),
-- @ErrorSeverity = ERROR_SEVERITY(),
-- @ErrorState = ERROR_STATE();
-- -- Raise the error again to notify the caller
-- RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState);
-- END CATCH
--END;
---- Testing the AddToFavoritePlaylists procedure
--DECLARE @user_id INT = 2;
--DECLARE @playlist_id INT = 1;
---- Execute the procedure
--EXEC AddToFavoritePlaylists @user_id, @playlist_id;
---------------------------------------------------------------------------------------------------------
--!!!
--CREATE PROCEDURE AddSongToPlaylist
-- @user_id INT,
-- @playlist_id INT,
-- @music_id INT
--AS
--BEGIN
-- BEGIN TRY
-- -- Start transaction
-- BEGIN TRANSACTION;
-- -- Check if the song exists
-- IF EXISTS (SELECT 1 FROM Music WHERE music_id = @music_id)
-- BEGIN
-- -- Check if the song can be added to the playlist
-- IF (SELECT can_add_to_playlist FROM Music WHERE music_id = @music_id) = 1
-- BEGIN
-- -- Check if the user exists and is a Premium user
-- IF EXISTS (SELECT 1 FROM Premium WHERE UserID = @user_id)
-- BEGIN
-- -- Check if the song is already in the playlist
-- IF NOT EXISTS (SELECT 1 FROM Playlist_Music WHERE playlist_id = @playlist_id AND music_id = @music_id)
-- BEGIN
-- -- Insert the song into the Playlist_Music table
-- INSERT INTO Playlist_Music (playlist_id, music_id)
-- VALUES (@playlist_id, @music_id);
-- -- Commit transaction
-- COMMIT TRANSACTION;
-- END
-- ELSE
-- BEGIN
-- -- Song is already in the playlist
-- ROLLBACK TRANSACTION;
-- RAISERROR ('Song is already in the playlist.', 16, 1);
-- END
-- END
-- ELSE
-- BEGIN
-- -- User does not exist or is not a Premium user
-- ROLLBACK TRANSACTION;
-- RAISERROR ('User does not exist or is not a Premium user.', 16, 1);
-- END
-- END
-- ELSE
-- BEGIN
-- -- Song cannot be added to playlist
-- ROLLBACK TRANSACTION;
-- RAISERROR ('Song cannot be added to the playlist.', 16, 1);
-- END
-- END
-- ELSE
-- BEGIN
-- -- Song does not exist
-- ROLLBACK TRANSACTION;
-- RAISERROR ('Song does not exist.', 16, 1);
-- END
-- END TRY
-- BEGIN CATCH
-- -- Rollback transaction in case of error
-- IF @@TRANCOUNT > 0
-- ROLLBACK TRANSACTION;
-- -- Retrieve error information
-- DECLARE @ErrorMessage NVARCHAR(4000);
-- DECLARE @ErrorSeverity INT;
-- DECLARE @ErrorState INT;
-- SELECT
-- @ErrorMessage = ERROR_MESSAGE(),
-- @ErrorSeverity = ERROR_SEVERITY(),
-- @ErrorState = ERROR_STATE();
-- -- Raise the error again to notify the caller
-- RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState);
-- END CATCH
--END;
---- Testing the AddSongToPlaylist procedure
--DECLARE @user_id INT = 2;
--DECLARE @playlist_id INT = 1;
--DECLARE @music_id INT = 28;
---- Execute the procedure
--EXEC AddSongToPlaylist @user_id, @playlist_id, @music_id;
---------------------------------------------------------------------------------------------------------
--CREATE PROCEDURE SearchSongs
-- @search_keyword VARCHAR(255) = NULL,
-- @artist_name VARCHAR(100) = NULL,
-- @desired_genre VARCHAR(255) = NULL,
-- @desired_zone VARCHAR(50) = NULL,
-- @desired_age_category VARCHAR(50) = NULL
--AS
--BEGIN
-- -- Temporary table to store results
-- CREATE TABLE #TempSongs (
-- music_id INT,
-- title VARCHAR(255),
-- duration TIME,
-- genre VARCHAR(255),
-- zone VARCHAR(50),
-- age_category VARCHAR(50),
-- artist_name VARCHAR(100)
-- );
-- INSERT INTO #TempSongs
-- SELECT M.music_id, M.title, M.duration, M.genre, M.zone, M.age_category, U.Name AS artist_name
-- FROM Music M
-- INNER JOIN Music_Artist MA ON M.music_id = MA.music_id
-- INNER JOIN Artist A ON MA.artist_id = A.artist_id
-- INNER JOIN Premium P ON A.artist_id = P.UserID
-- INNER JOIN Users U ON P.UserID = U.ID
-- WHERE (@search_keyword IS NULL OR M.title LIKE '%' + @search_keyword + '%')
-- AND (@artist_name IS NULL OR U.Name LIKE '%' + @artist_name + '%')
-- AND (@desired_genre IS NULL OR M.genre = @desired_genre)
-- AND (@desired_zone IS NULL OR M.zone = @desired_zone)
-- AND (@desired_age_category IS NULL OR M.age_category = @desired_age_category);
-- -- Check if the temporary table is empty
-- IF NOT EXISTS (SELECT 1 FROM #TempSongs)
-- BEGIN
-- -- Raise an error or return a message
-- RAISERROR ('No songs found matching the search criteria.', 16, 1);
-- RETURN;
-- END
-- -- Return the results
-- SELECT * FROM #TempSongs;
-- -- Clean up
-- DROP TABLE #TempSongs;
--END
--GO
--EXEC SearchSongs @desired_genre = 'Rock';
---------------------------------------------------------------------------------------------------------
--CREATE PROCEDURE SearchAlbums
-- @search_keyword VARCHAR(255) = NULL,
-- @artist_name VARCHAR(100) = NULL,
-- @desired_genre VARCHAR(255) = NULL,
-- @desired_zone VARCHAR(255) = NULL,
-- @desired_age_category VARCHAR(255) = NULL
--AS
--BEGIN
-- -- Temporary table to store results
-- CREATE TABLE #TempAlbums (
-- album_id INT,
-- title VARCHAR(255),
-- release_date DATE,
-- genre VARCHAR(255),
-- zone VARCHAR(255),
-- age_category VARCHAR(255),
-- artist_name VARCHAR(100)
-- );
-- INSERT INTO #TempAlbums
-- SELECT A.album_id, A.title, A.release_date, A.genre, A.zone, A.age_category, U.Name AS artist_name
-- FROM Album A
-- INNER JOIN Album_Artist AA ON A.album_id = AA.album_id
-- INNER JOIN Artist AR ON AA.artist_id = AR.artist_id
-- INNER JOIN Premium P ON AR.artist_id = P.UserID
-- INNER JOIN Users U ON P.UserID = U.ID
-- WHERE (@search_keyword IS NULL OR A.title LIKE '%' + @search_keyword + '%')
-- AND (@artist_name IS NULL OR U.Name LIKE '%' + @artist_name + '%')
-- AND (@desired_genre IS NULL OR A.genre = @desired_genre)
-- AND (@desired_zone IS NULL OR A.zone = @desired_zone)
-- AND (@desired_age_category IS NULL OR A.age_category = @desired_age_category);
-- -- Check if the temporary table is empty
-- IF NOT EXISTS (SELECT 1 FROM #TempAlbums)
-- BEGIN
-- -- Raise an error or return a message
-- RAISERROR ('No albums found matching the search criteria.', 16, 1);
-- RETURN;
-- END
-- -- Return the results
-- SELECT * FROM #TempAlbums;
-- -- Clean up
-- DROP TABLE #TempAlbums;
--END
--GO
--EXEC SearchAlbums @desired_age_category = '2+';
---------------------------------------------------------------------------------------------------------
--DROP PROCEDURE IF EXISTS GetUpcomingConcertDetails
--CREATE PROCEDURE GetUpcomingConcertDetails
--AS
--BEGIN
-- SELECT
-- u.Name AS artist,
-- c.location,
-- c.date,
-- c.NumRegularTickets,
-- c.RegularTicketPrice,
-- c.NumVIPTickets,
-- c.VIPTicketPrice,
-- c.NumPremiumTickets,
-- c.PremiumTicketPrice,
-- c.concert_id
-- FROM
-- Concerts c
-- JOIN
-- Concert_Artists ca ON c.concert_id = ca.concert_id
-- JOIN
-- Artist a ON ca.artist_id = a.artist_id
-- JOIN
-- Premium p ON a.artist_id = p.UserID
-- JOIN
-- Users u ON p.UserID = u.ID
-- WHERE
-- c.is_cancelled = 0
-- AND c.date >= GETDATE(); -- Ensures the date has not expired
--END;
--EXEC GetUpcomingConcertDetails;
---------------------------------------------------------------------------------------------------------
--DROP PROCEDURE IF EXISTS GetArtistConcertDetails
--CREATE PROCEDURE GetArtistConcertDetails
-- @ArtistID INT
--AS
--BEGIN
-- SELECT
-- c.location,
-- CONVERT(NVARCHAR, c.date, 101) AS concert_date, -- Always show the date formatted as mm/dd/yyyy
-- CASE
-- WHEN c.is_cancelled = 1 THEN 'Cancelled'
-- WHEN c.date < GETDATE() THEN 'Expired'
-- ELSE 'Pending'
-- END AS concert_status
-- FROM
-- Concerts c
-- JOIN
-- Concert_Artists ca ON c.concert_id = ca.concert_id
-- JOIN
-- Artist a ON ca.artist_id = a.artist_id
-- WHERE
-- a.artist_id = @ArtistID
-- ORDER BY
-- c.date DESC; -- Order by concert date, soonest first
--END;
--EXEC GetArtistConcertDetails @ArtistID=1;
---------------------------------------------------------------------------------------------------
--DROP PROCEDURE IF EXISTS BuyTicket
--CREATE PROCEDURE BuyTicket
-- @user_id INT,
-- @concert_id INT,
-- @ticket_type VARCHAR(50)
--AS
--BEGIN
-- -- Start a transaction
-- BEGIN TRANSACTION;
-- -- Declare variables to hold ticket prices, wallet balance, and available tickets
-- DECLARE @ticket_price DECIMAL(10, 2);
-- DECLARE @balance DECIMAL(10, 2);
-- DECLARE @available_tickets INT;
-- DECLARE @num_tickets_column NVARCHAR(50);
-- DECLARE @ticket_id INT;
-- -- Determine the ticket price and available tickets based on the ticket type
-- IF @ticket_type = 'Regular'
-- BEGIN
-- SELECT @ticket_price = RegularTicketPrice,
-- @available_tickets = NumRegularTickets
-- FROM Concerts
-- WHERE concert_id = @concert_id;
-- SET @num_tickets_column = 'NumRegularTickets';
-- END
-- ELSE IF @ticket_type = 'VIP'
-- BEGIN
-- SELECT @ticket_price = VIPTicketPrice,
-- @available_tickets = NumVIPTickets