-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.cpp
More file actions
4144 lines (3301 loc) · 149 KB
/
user.cpp
File metadata and controls
4144 lines (3301 loc) · 149 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
#include "user.h"
#include "qboxlayout.h"
#include "qdatetime.h"
#include "qsqldatabase.h"
#include "ui_user.h"
#include <QInputDialog>
#include <QMessageBox>
#include <QSqlRecord>
#include <QHBoxLayout> // Include necessary headers
#include <QLineEdit>
#include <QComboBox>
#include <QCheckBox>
#include <QMessageBox>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlRecord>
#include <QSqlField>
#include <QSqlTableModel>
#include <QDebug>
#include <QVariantList>
#include "user.h"
#include <QDebug>
QString servername ;
QString dbname ;
QSqlDatabase db;
QString dsn;
int UserID =10;
int MusicID =-1;
QString UserType;
QString QPushButtonStyle ="QPushButton {"
" border-radius: 15px;"
" background-color: rgb(172, 172, 172);"
" font: 11pt 'Segoe Print';"
"}"
"QPushButton {"
" color: rgb(0, 0, 0);"
"}"
"QPushButton {"
" border-radius: 15px;"
" color: rgb(255, 255, 255);"
" font: bold 13pt 'Segoe Print';"
" background-color: #000000;"
" border: 2px solid #2fbd59;"
" padding: 5px 15px;"
" box-shadow: 3px 3px 5px rgba(0, 0, 0, 0.5);"
"}"
"QPushButton:hover {"
" background-color: #555555;"
"}";
QString QLableStyle = "QLabel {"
" border-radius: 15px;"
" color: rgb(255, 255, 255);"
" font: bold 13pt 'Segoe Print';"
" background-color: #333333;"
" border: 2px solid #2fbd59;"
" padding: 5px 15px;"
" border: 1px solid #888888;"
" background-color: #2fbd59;"
"}";
QString QMessageBoxStyle ="QMessageBox {"
" border-radius: 15px;"
" background-color: rgb(172, 172, 172);"
" font: 11pt 'Segoe Print';"
"}"
"QMessageBox {"
" color: rgb(0, 0, 0);"
"}"
"QMessageBox {"
" border-radius: 15px;"
" color: rgb(255, 255, 255);"
" font: bold 13pt 'Segoe Print';"
" background-color: #000000;"
" border: 2px solid #2fbd59;"
" padding: 5px 15px;"
" border: 1px solid #888888;"
" background-color: #2fbd59;"
"}"
"QMessageBox:hover {"
" background-color: #555555;"
"}";
User::User(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::User),
signalMapper(new QSignalMapper(this))
{
ui->setupUi(this);
if (UserType=="Artist"||UserType=="Premium"){
// Connect the signal mapper's mapped signal to the slot
connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(onSongButtonClicked2(int)));
if (UserType=="Premium"){
ui->addmusicpushButton->setVisible(false);
}
ui->walletticketspushButton->setStyleSheet(QPushButtonStyle);
ui->followpushButton->setStyleSheet(QPushButtonStyle);
ui->likedsongspushButton->setStyleSheet(QPushButtonStyle);
ui->songspushButton->setStyleSheet(QPushButtonStyle);
ui->createplaylistpushButton->setStyleSheet(QPushButtonStyle);
ui->showalluserspushButton->setStyleSheet(QPushButtonStyle);
ui->logout->setStyleSheet(QPushButtonStyle);
ui->addmusicpushButton->setStyleSheet(QPushButtonStyle);
ui->commentsall->setStyleSheet(QPushButtonStyle);
ui->allplaylists->setStyleSheet(QPushButtonStyle);
ui->cancle1->setStyleSheet(QPushButtonStyle);
ui->cancle2->setStyleSheet(QPushButtonStyle);
ui->cancle3->setStyleSheet(QPushButtonStyle);
ui->cancle4->setStyleSheet(QPushButtonStyle);
ui->cancle5->setStyleSheet(QPushButtonStyle);
ui->cancle6->setStyleSheet(QPushButtonStyle);
ui->cancle7->setStyleSheet(QPushButtonStyle);
ui->cancle8->setStyleSheet(QPushButtonStyle);
ui->cancle9->setStyleSheet(QPushButtonStyle);
ui->cancle10->setStyleSheet(QPushButtonStyle);
ui->cancle11->setStyleSheet(QPushButtonStyle);
ui->cancle12->setStyleSheet(QPushButtonStyle);
ui->cancle13->setStyleSheet(QPushButtonStyle);
ui->cancle14->setStyleSheet(QPushButtonStyle);
ui->cancle15->setStyleSheet(QPushButtonStyle);
ui->searchcomboBox->addItem("Title");
ui->searchcomboBox->addItem("Artist");
ui->searchcomboBox->addItem("Genre");
ui->searchcomboBox->addItem("Zone");
ui->searchcomboBox->addItem("Age Category");
ui->stackedWidget->setCurrentIndex(0);
int currentYear = QDate::currentDate().year();
// Populate combo box with years from 1900 to current year
for (int year = 1900; year <= currentYear; ++year) {
ui->year->addItem(QString::number(year));
}
// Populate months
QStringList months = {"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};
ui->month->addItems(months);
QStringList days;
for (int i = 1; i <= 31; ++i) {
days << QString::number(i);
}
ui->day->addItems(days);
refreshmainpage();
}
else{//normal user
ui->stackedWidget->setCurrentIndex(10);
ui->searchcomboBox_2->addItem("Title");
ui->searchcomboBox_2->addItem("Artist");
ui->searchcomboBox_2->addItem("Genre");
ui->searchcomboBox_2->addItem("Zone");
ui->searchcomboBox_2->addItem("Age Category");
QSqlQuery query;
query.prepare("EXEC GetWalletBalance @UserID=:userID");
query.bindValue(":userID", UserID);
if (!query.exec()) {
QMessageBox::critical(this, "Error", "Failed to fetch wallet balance: " + query.lastError().text());
return;
}
if (query.next()) {
if (query.record().indexOf("WalletBalance") != -1) {
double walletBalance = query.value("WalletBalance").toDouble();
QString balanceText = QString::number(walletBalance, 'f', 2);
ui->balancelabel_2->setText(" $" + balanceText);
} else {
QString message = query.value("Message").toString();
ui->balancelabel_2->setText(message);
}
}
}
}
User::~User()
{
delete ui;
}
QSqlDatabase createDatabaseConnection(const QString &connectionName) {
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC", connectionName);
QString servername = "LOCALHOST\\SQLEXPRESS";
QString dbname = "Spotify2";
QString dsn = QString("DRIVER={ODBC Driver 17 for SQL Server};SERVER=%1;DATABASE=%2;Trusted_Connection=Yes;").arg(servername).arg(dbname);
db.setDatabaseName(dsn);
if (!db.open()) {
qDebug() << "Database connection failed:" << db.lastError().text();
} else {
qDebug() << "Database opened successfully!";
}
return db;
}
void User::refreshmainpage()
{
QList<QString> playlistNames;
// Initialize QSqlQuery with a valid database connection
QSqlQuery query(QSqlDatabase::database());
// Check if the database connection is open
if (!db.isOpen()) {
qDebug() << "Database is not open!";
return;
}
// Prepare and execute the procedure
query.prepare("EXEC GetUserPlaylistsDetail :user_id");
query.bindValue(":user_id", UserID);
if (!query.exec()) {
qDebug() << "Query execution error:" << query.lastError().text();
return;
}
// Read the results
while (query.next()) {
QString playlistName = query.value("playlist_name").toString();
playlistNames.append(playlistName);
}
// List of QLabel pointers
QList<QLabel*> labels = {ui->namesong1lable_2, ui->namesong2lable_2, ui->namesong3lable_2, ui->namesong4lable_2, ui->namesong5lable_2};
// Clear all labels
for (QLabel* label : labels) {
label->clear();
}
// Set text to labels from playlistNames
int count = qMin(labels.size(), playlistNames.size());
for (int i = 0; i < count; ++i) {
labels[i]->setText(playlistNames[i]);
}
// Show labels
for (QLabel* label : labels) {
label->show();
}
}
//------------------------------------------------------------ Wallet and tickets ------------------------------------------------------------
void User::on_walletticketspushButton_clicked()
{
QSqlQuery query;
query.prepare("EXEC GetWalletBalance @UserID=:userID");
query.bindValue(":userID", UserID);
if (!query.exec()) {
QMessageBox::critical(this, "Error", "Failed to fetch wallet balance: " + query.lastError().text());
return;
}
if (query.next()) {
if (query.record().indexOf("WalletBalance") != -1) {
double walletBalance = query.value("WalletBalance").toDouble();
QString balanceText = QString::number(walletBalance, 'f', 2);
ui->balancelabel->setText(" $" + balanceText);
} else {
QString message = query.value("Message").toString();
ui->balancelabel->setText(message);
}
}
ui->stackedWidget->setCurrentIndex(1);
// Clear the previous content
QWidget *oldContent = ui->scrollArea->widget();
if (oldContent) {
delete oldContent;
}
// Create a new content widget and layout
QWidget *contentWidget = new QWidget(this);
QVBoxLayout *layout = new QVBoxLayout(contentWidget);
// Prepare the query to call the stored procedure
QSqlQuery queryValid(db);
queryValid.prepare("EXEC ShowValidTickets @user_id=:userID");
queryValid.bindValue(":userID", UserID);
// Execute the query and handle errors
if (!queryValid.exec()) {
QMessageBox::critical(this, "Query Error", "Failed to retrieve valid tickets: " + queryValid.lastError().text());
return;
}
// Iterate over the results and populate the UI
while (queryValid.next()) {
QHBoxLayout *hLayout = new QHBoxLayout();
QLabel *ticketIDLabel = new QLabel("Ticket ID: " + queryValid.value("ticket_id").toString(), contentWidget);
ticketIDLabel->setFixedSize(150, 50);
hLayout->addWidget(ticketIDLabel);
QLabel *artistLabel = new QLabel("Artist: " + queryValid.value("Artist_information").toString(), contentWidget);
artistLabel->setFixedSize(150, 50);
hLayout->addWidget(artistLabel);
QLabel *dateLabel = new QLabel("Date: " + queryValid.value("concert_date").toString(), contentWidget);
dateLabel->setFixedSize(220, 50);
hLayout->addWidget(dateLabel);
QLabel *locationLabel = new QLabel("Location: " + queryValid.value("location").toString(), contentWidget);
locationLabel->setFixedSize(200, 50);
hLayout->addWidget(locationLabel);
QLabel *priceLabel = new QLabel("Price: " + queryValid.value("price").toString(), contentWidget);
priceLabel->setFixedSize(150, 50);
hLayout->addWidget(priceLabel);
layout->addLayout(hLayout);
}
// Set the new content widget to the scroll area
ui->scrollArea->setWidget(contentWidget);
// QWidget *contentWidget = new QWidget(this);
// QVBoxLayout *layout = new QVBoxLayout(contentWidget);
// for (int i = 1; i <= 100; ++i)
// {
// QHBoxLayout *hLayout = new QHBoxLayout();
// QLabel *singerLabel = new QLabel("Singer " + QString::number(i), contentWidget);
// singerLabel->setFixedSize(250, 50);
// hLayout->addWidget(singerLabel);
// QLabel *locationLabel = new QLabel("Location " + QString::number(i), contentWidget);
// locationLabel->setFixedSize(250, 50);
// hLayout->addWidget(locationLabel);
// QLabel *dateLabel = new QLabel("Date " + QString::number(i), contentWidget);
// dateLabel->setFixedSize(250, 50);
// hLayout->addWidget(dateLabel);
// layout->addLayout(hLayout);
// }
// ui->scrollArea->setWidget(contentWidget);
}
//**charge account
void User::on_chargepushButton_clicked()
{
QString amountText = ui->lineEdit->text().trimmed();
if (amountText.isEmpty()) {
QMessageBox::warning(this, "Error", "Please enter an amount.");
return;
}
bool ok;
double amount = amountText.toDouble(&ok);
if (!ok) {
QMessageBox::warning(this, "Error", "Invalid amount format.");
return;
}
// Assuming db is your QSqlDatabase object, and you have established a connection
QSqlQuery query(db);
query.prepare("EXEC RechargeWallet @UserID=:userID, @Amount=:amount");
query.bindValue(":userID", UserID); // Replace with actual user ID
query.bindValue(":amount", amount);
if (!query.exec()) {
QMessageBox::critical(this, "Error", "Failed to recharge wallet: " + query.lastError().text());
return;
}
if (query.next()) {
QString message = query.value(0).toString(); // "Recharge successful!" or "No wallet found..."
QMessageBox::information(this, "Info", message);
if (query.record().indexOf("UpdatedBalance") != -1) {
double updatedBalance = query.value("UpdatedBalance").toDouble();
// Assuming updateUserBalance is a method in your User class or related backend logic
updateUserBalance(updatedBalance);
}
}
}
//** buy premium account
int getUserBalance() {
return 100;
}
void User::updateUserBalance(int newBalance) {
// Assuming you have a member variable to store balance
int balance = newBalance;
// Update the UI directly, for example, updating a QLabel to show the new balance
ui->balancelabel->setText(" $" + QString::number(newBalance));
// You can also perform other actions that you would typically handle in the slot
}
void User::on_buypremiumpushButton_clicked()
{
int userBalance = getUserBalance();
int premiumCost = 100;
QString messageBoxStyle = "QMessageBox {"
" border-radius: 15px;"
" background-color: rgb(172, 172, 172);"
" font: 11pt 'Segoe Print';"
"}"
"QLabel {"
" color: rgb(0, 0, 0);"
"}"
"QPushButton {"
" border-radius: 15px;"
" color: rgb(255, 255, 255);"
" font: bold 13pt 'Segoe Print';"
" background-color: #000000;"
" border: 2px solid #2fbd59;"
" padding: 5px 15px;"
" box-shadow: 3px 3px 5px rgba(0, 0, 0, 0.5);"
"}"
"QPushButton:hover {"
" background-color: #555555;"
"}";
if (userBalance >= premiumCost) {
updateUserBalance(userBalance - premiumCost);
QMessageBox successBox;
successBox.setStyleSheet(messageBoxStyle);
successBox.setIcon(QMessageBox::Information);
successBox.setWindowTitle("Success");
successBox.setText("Premium account purchased successfully.");
successBox.exec();
} else {
QMessageBox errorBox;
errorBox.setStyleSheet(messageBoxStyle);
errorBox.setIcon(QMessageBox::Critical);
errorBox.setWindowTitle("Error");
errorBox.setText("Insufficient balance to purchase premium account.");
errorBox.exec();
}
}
//** full ticketd's scrollbox
void User::on_validticketspushButton_clicked()
{
// Clear the previous content
QWidget *oldContent = ui->scrollArea->widget();
if (oldContent) {
delete oldContent;
}
// Create a new content widget and layout
QWidget *contentWidget = new QWidget(this);
QVBoxLayout *layout = new QVBoxLayout(contentWidget);
// Prepare the query to call the stored procedure
QSqlQuery query(db);
query.prepare("EXEC ShowValidTickets @user_id=:userID");
query.bindValue(":userID", UserID);
// Execute the query and handle errors
if (!query.exec()) {
QMessageBox::critical(this, "Query Error", "Failed to retrieve valid tickets: " + query.lastError().text());
return;
}
// Iterate over the results and populate the UI
while (query.next()) {
QHBoxLayout *hLayout = new QHBoxLayout();
QLabel *ticketIDLabel = new QLabel("Ticket ID: " + query.value("ticket_id").toString(), contentWidget);
ticketIDLabel->setFixedSize(150, 50);
hLayout->addWidget(ticketIDLabel);
QLabel *artistLabel = new QLabel("Artist: " + query.value("Artist_information").toString(), contentWidget);
artistLabel->setFixedSize(150, 50);
hLayout->addWidget(artistLabel);
QLabel *dateLabel = new QLabel("Date: " + query.value("concert_date").toString(), contentWidget);
dateLabel->setFixedSize(220, 50);
hLayout->addWidget(dateLabel);
QLabel *locationLabel = new QLabel("Location: " + query.value("location").toString(), contentWidget);
locationLabel->setFixedSize(200, 50);
hLayout->addWidget(locationLabel);
QLabel *priceLabel = new QLabel("Price: " + query.value("price").toString(), contentWidget);
priceLabel->setFixedSize(150, 50);
hLayout->addWidget(priceLabel);
layout->addLayout(hLayout);
}
// Set the new content widget to the scroll area
ui->scrollArea->setWidget(contentWidget);
}
void User::on_expiredticketspushButton_clicked()
{
QWidget *contentWidget = new QWidget(this);
QVBoxLayout *layout = new QVBoxLayout(contentWidget);
QSqlQuery query(db);
query.prepare("EXEC ShowInvalidTickets @user_id = :user_id");
query.bindValue(":user_id", UserID); // Assuming userID is defined and holds the current user's ID
if (!query.exec()) {
QMessageBox::critical(nullptr, "Database Error", query.lastError().text());
return;
}
while (query.next()) {
QHBoxLayout *hLayout = new QHBoxLayout();
// Extract data from the query result
QString artistInfo = query.value("Artist_information").toString();
QString location = query.value("location").toString();
QString concertDate = query.value("concert_date").toString();
QString price = query.value("price").toString();
// Create labels for the UI
QLabel *singerLabel = new QLabel(artistInfo, contentWidget);
singerLabel->setFixedSize(150, 50);
hLayout->addWidget(singerLabel);
QLabel *locationLabel = new QLabel(location, contentWidget);
locationLabel->setFixedSize(200, 50);
hLayout->addWidget(locationLabel);
QLabel *dateLabel = new QLabel(concertDate, contentWidget);
dateLabel->setFixedSize(220, 50);
hLayout->addWidget(dateLabel);
QLabel *priceLabel = new QLabel(price, contentWidget);
priceLabel->setFixedSize(150, 50);
hLayout->addWidget(priceLabel);
layout->addLayout(hLayout);
}
ui->scrollArea->setWidget(contentWidget);
}
//** buy ticket's concert
void User::on_buyticketspushButton_clicked()
{
QSqlQuery query(db);
if (!query.exec("EXEC GetUpcomingConcertDetails")) {
qDebug() << "Stored procedure execution error: " << query.lastError().text();
return;
}
// Process the results
QStringList artists, locations, dates;
QList<int> numRegularTickets, regularTicketPrices;
QList<int> numVIPTickets, vipTicketPrices;
QList<int> numPremiumTickets, premiumTicketPrices;
QList<int> concert_IDs;
while (query.next()) {
if (query.record().count() == 1) { // Check if the result is the message
qDebug() << query.value(0).toString();
return;
}
//qDebug() <<query.value("concert_id").toInt();
concert_IDs << query.value("concert_id").toInt();
artists << query.value("artist").toString();
locations << query.value("location").toString();
dates << query.value("date").toString();
numRegularTickets << query.value("NumRegularTickets").toInt();
regularTicketPrices << query.value("RegularTicketPrice").toInt();
numVIPTickets << query.value("NumVIPTickets").toInt();
vipTicketPrices << query.value("VIPTicketPrice").toInt();
numPremiumTickets << query.value("NumPremiumTickets").toInt();
premiumTicketPrices << query.value("PremiumTicketPrice").toInt();
}
ui->stackedWidget->setCurrentIndex(6);
// Clear previous content
QWidget *contentWidget = new QWidget(this);
QGridLayout *layout = new QGridLayout(contentWidget);
layout->setContentsMargins(20, 20, 20, 20); // Set margins for layout
// Populate the layout with fetched data
for (int i = 0; i < artists.size(); ++i)
{
// Create label for artist name
QLabel *artistLabel = new QLabel(artists[i], contentWidget);
artistLabel->setFixedSize(250, 100);
artistLabel->setStyleSheet(QLableStyle);
layout->addWidget(artistLabel, i, 0); ////////////
// Create label for concert location
QLabel *locationLabel = new QLabel(locations[i], contentWidget);
locationLabel->setFixedSize(250, 100);
locationLabel->setStyleSheet(QLableStyle);
layout->addWidget(locationLabel, i, 1); ////////////
// Create label for concert date
QLabel *dateLabel = new QLabel(dates[i], contentWidget);
dateLabel->setFixedSize(250, 100);
dateLabel->setStyleSheet(QLableStyle);
layout->addWidget(dateLabel, i, 2); ///////////
// Create VIP button
QPushButton *vipButton = new QPushButton("VIP\n" + QString::number(numVIPTickets[i]) + " Tickets\n$" + QString::number(vipTicketPrices[i]), contentWidget);
vipButton->setFixedSize(150, 100);
vipButton->setStyleSheet(QPushButtonStyle);
vipButton->setProperty("concert_id", concert_IDs[i]);
//qDebug()<<concert_IDs[i];
vipButton->setProperty("ticket_type", "VIP");
layout->addWidget(vipButton, i, 3); /////////
connect(vipButton, &QPushButton::clicked, this, &User::handleTicketButtonClicked);
// Create Regular button
QPushButton *regularButton = new QPushButton("Regular\n" + QString::number(numRegularTickets[i]) + " Tickets\n$" + QString::number(regularTicketPrices[i]), contentWidget);
regularButton->setFixedSize(150, 100);
regularButton->setStyleSheet(QPushButtonStyle);
regularButton->setProperty("concert_id", concert_IDs[i]);
regularButton->setProperty("ticket_type", "Regular");
layout->addWidget(regularButton, i, 4); // Add to grid layout at row i, column 4
connect(regularButton, &QPushButton::clicked, this, &User::handleTicketButtonClicked);
// Create Premium button
QPushButton *premiumButton = new QPushButton("Premium\n" + QString::number(numPremiumTickets[i]) + " Tickets\n$" + QString::number(premiumTicketPrices[i]), contentWidget);
premiumButton->setFixedSize(150, 100);
premiumButton->setStyleSheet(QPushButtonStyle);
premiumButton->setProperty("concert_id", concert_IDs[i]);
premiumButton->setProperty("ticket_type", "Premium");
layout->addWidget(premiumButton, i, 5); ////////
connect(premiumButton, &QPushButton::clicked, this, &User::handleTicketButtonClicked);
}
ui->scrollArea_12->setWidget(contentWidget);
}
void User::handleTicketButtonClicked()
{
QPushButton *button = qobject_cast<QPushButton *>(sender());
if (!button) {
return;
}
int concertId = button->property("concert_id").toInt();
QString ticketType = button->property("ticket_type").toString();
qDebug() << "Concert ID:" << concertId << ", Ticket Type:" << ticketType;
// Prepare and execute the SQL query to call the stored procedure
QSqlQuery query;
query.prepare("EXEC BuyTicket :user_id, :concert_id, :ticket_type");
query.bindValue(":user_id", UserID);
query.bindValue(":concert_id", concertId);
query.bindValue(":ticket_type", ticketType);
if (!query.exec()) {
qDebug() << "Stored procedure execution error:" << query.lastError().text();
QMessageBox::warning(this, "Error", "Failed to execute the stored procedure.");
return;
}
if (query.next()) {
QString message = query.value("Message").toString();
qDebug() << "Message:" << message;
QMessageBox::information(this, "Purchase Result", message);
}
}
//----------------------------------------------------------- following page --------------------------------------------------------------
void User::on_followpushButton_clicked()
{
ui->stackedWidget->setCurrentIndex(2);
// Clear previous content
delete ui->scrollArea_11->widget();
QWidget *contentWidget = new QWidget(this);
QVBoxLayout *layout = new QVBoxLayout(contentWidget);
// Execute the stored procedure
QSqlQuery query;
if (!query.exec("EXEC GetAllArtists")) {
QMessageBox::critical(this, "Query Error", "Failed to execute stored procedure: " + query.lastError().text());
return;
}
// Check if there are any artists in the database
bool hasArtists = false;
// Iterate over the results
while (query.next()) {
hasArtists = true;
int artistID = query.value("artist_id").toInt();
QString artistName = query.value("Name").toString();
QHBoxLayout *hLayout = new QHBoxLayout();
QLabel *nameLabel = new QLabel(artistName, contentWidget);
nameLabel->setFixedSize(200, 50);
hLayout->addWidget(nameLabel);
// Create button for follow request
QPushButton *followButton = new QPushButton("Follow", contentWidget);
followButton->setProperty("ID", QString::number(artistID));
followButton->setFixedSize(140, 50);
connect(followButton, &QPushButton::clicked, this, &User::onFollowButtonSingerClicked);
followButton->setStyleSheet(QPushButtonStyle);
hLayout->addWidget(followButton);
layout->addLayout(hLayout);
}
if (!hasArtists) {
qDebug() << "No artists found in the database.";
}
ui->scrollArea_11->setWidget(contentWidget);
///////////////////////////////////////////////////// following singers /////////////////////////////////
// Clear previous content
delete ui->scrollArea_10->widget();
QWidget *contentWidget1 = new QWidget(this);
QVBoxLayout *layout1 = new QVBoxLayout(contentWidget1);
// Prepare the stored procedure call
QSqlQuery queryFollowingSingers;
queryFollowingSingers.prepare("EXEC GetFollowedArtists @UserID = :userID");
queryFollowingSingers.bindValue(":userID", UserID);
// Execute the stored procedure
if (!queryFollowingSingers.exec()) {
QMessageBox::critical(this, "Query Error", "Failed to execute stored procedure: " + queryFollowingSingers.lastError().text());
return;
}
// Check if there are any followed artists in the database
bool hasArtistsFollowing = false;
// Iterate over the results
while (queryFollowingSingers.next()) {
hasArtistsFollowing = true;
QString artistName = queryFollowingSingers.value("Name").toString();
QHBoxLayout *hLayout = new QHBoxLayout();
QLabel *nameLabel = new QLabel(artistName, contentWidget1);
nameLabel->setFixedSize(150, 50);
hLayout->addWidget(nameLabel);
layout1->addLayout(hLayout);
}
if (!hasArtistsFollowing) {
QLabel *noArtistsLabel = new QLabel("No following artist!", contentWidget1);
noArtistsLabel->setFixedSize(150, 50);
layout1->addWidget(noArtistsLabel);
}
ui->scrollArea_10->setWidget(contentWidget1);
///////////////////////////////////////////////////// following others /////////////////////////////////
// Clear previous content
delete ui->scrollArea_13->widget();
QWidget *contentWidget2 = new QWidget(this);
QVBoxLayout *layout2 = new QVBoxLayout(contentWidget2);
// Prepare the stored procedure call
QSqlQuery queryFollowingOthers;
queryFollowingOthers.prepare("EXEC GetFollowedNonArtists @UserID = :userID");
queryFollowingOthers.bindValue(":userID", UserID);
// Execute the stored procedure
if (!queryFollowingOthers.exec()) {
QMessageBox::critical(this, "Query Error", "Failed to execute stored procedure: " + queryFollowingOthers.lastError().text());
return;
}
// Check if there are any followed non-artists in the database
bool hasNonArtists = false;
// Iterate over the results
while (queryFollowingOthers.next()) {
hasNonArtists = true;
QString userName = queryFollowingOthers.value("Name").toString();
QHBoxLayout *hLayout2 = new QHBoxLayout();
QLabel *nameLabel2 = new QLabel(userName, contentWidget2);
nameLabel2->setFixedSize(150, 50);
hLayout2->addWidget(nameLabel2);
layout2->addLayout(hLayout2);
}
if (!hasNonArtists) {
QLabel *noNonArtistsLabel = new QLabel("No following", contentWidget2);
noNonArtistsLabel->setFixedSize(150, 50);
layout2->addWidget(noNonArtistsLabel);
}
ui->scrollArea_13->setWidget(contentWidget2);
///////////////////////////////////////////////////// accepted and rejected request /////////////////////////////////
QWidget *contentWidget3 = new QWidget(this);
QVBoxLayout *layout3 = new QVBoxLayout(contentWidget3);
QSqlQuery queryNotPending;
queryNotPending.prepare("{ CALL GetNonPendingRequestsForReceiver(?) }");
queryNotPending.bindValue(0, UserID);
if (!queryNotPending.exec()) {
qDebug() << "Stored procedure execution error:" << queryNotPending.lastError().text();
return;
}
int i = 1;
while (queryNotPending.next()) {
QString senderName = queryNotPending.value(0).toString();
QString status = queryNotPending.value(1).toString();
QHBoxLayout *hLayout3 = new QHBoxLayout();
QLabel *nameLabel3 = new QLabel(senderName + " (" + status + ")", contentWidget3);
nameLabel3->setFixedSize(200, 50);
hLayout3->addWidget(nameLabel3);
layout3->addLayout(hLayout3);
++i;
}
ui->scrollArea_14->setWidget(contentWidget3);
// QWidget *contentWidget3 = new QWidget(this);
// QVBoxLayout *layout3 = new QVBoxLayout(contentWidget3);
// for (int i = 1; i <= 10; ++i) {
// QHBoxLayout *hLayout3 = new QHBoxLayout();
// QLabel *nameLabel3 = new QLabel("accept " + QString::number(i), contentWidget3);
// nameLabel3->setFixedSize(150, 50);
// hLayout3->addWidget(nameLabel3);
// layout3->addLayout(hLayout3);
// }
// ui->scrollArea_14->setWidget(contentWidget3);
///////////////////////////////////////////////////// pending request /////////////////////////////////
// Clear the previous contents of contentWidget4
delete ui->scrollArea_15->widget();
QWidget *contentWidget4 = new QWidget(this);
QVBoxLayout *layout4 = new QVBoxLayout(contentWidget4);
// Prepare the SQL query to call the stored procedure
QSqlQuery queryPending;
queryPending.prepare("EXEC GetFriendRequestsForUser :UserID");
queryPending.bindValue(":UserID", UserID);
// Execute the query and check for errors
if (queryPending.exec()) {
while (queryPending.next()) {
int requestUserID = queryPending.value("UserID").toInt();
int friendID = queryPending.value("FriendID").toInt();
QString requesterName = queryPending.value("RequesterName").toString();
QHBoxLayout *hLayout4 = new QHBoxLayout();
QLabel *nameLabel4 = new QLabel(requesterName, contentWidget4);
nameLabel4->setFixedSize(150, 50);
hLayout4->addWidget(nameLabel4);
QPushButton *acceptButton = new QPushButton("Accept", contentWidget4);
acceptButton->setProperty("RequestUserID", requestUserID);
acceptButton->setProperty("FriendID", friendID);
acceptButton->setFixedSize(100, 50);
hLayout4->addWidget(acceptButton);
connect(acceptButton, &QPushButton::clicked, this, &User::onAcceptButtonClicked);
QPushButton *rejectButton = new QPushButton("Reject", contentWidget4);
rejectButton->setProperty("RequestUserID", requestUserID);
rejectButton->setProperty("FriendID", friendID);
rejectButton->setFixedSize(100, 50);
hLayout4->addWidget(rejectButton);
connect(rejectButton, &QPushButton::clicked, this, &User::onRejectButtonClicked);
layout4->addLayout(hLayout4);
}
} else {
qDebug() << "Error executing stored procedure:" << queryPending.lastError().text();
QMessageBox::warning(this, "Error", "Failed to retrieve friend requests.");
}
ui->scrollArea_15->setWidget(contentWidget4);
}
void User::onAcceptButtonClicked() {
QPushButton *button = qobject_cast<QPushButton*>(sender());
int friendID = button->property("RequestUserID").toInt();
int userID = UserID; // Replace with the actual user ID of the current user
qDebug() << "Accept friend request button clicked for user:" << friendID;
qDebug() << "Current User ID:" << userID;
// Prepare the SQL query to call the stored procedure
QSqlQuery query;
query.prepare("EXEC AcceptFriendRequest :UserID, :FriendID");
query.bindValue(":UserID", userID);
query.bindValue(":FriendID", friendID);
// Execute the query and check for errors
if (query.exec()) {
if (query.next()) {
QString message = query.value(0).toString();
qDebug() << "Stored Procedure Result:" << message;
QMessageBox::information(this, "Friend Request", message);
// Refresh friend requests list
//loadFriendRequests();
}
} else {
qDebug() << "Error executing stored procedure:" << query.lastError().text();
QMessageBox::warning(this, "Error", "Failed to accept friend request.");
}
}
void User::onRejectButtonClicked() {
QPushButton *button = qobject_cast<QPushButton*>(sender());
int friendID = button->property("RequestUserID").toInt();
int userID = UserID; // Replace with the actual user ID of the current user
qDebug() << "Reject friend request button clicked for user:" << friendID;
// Prepare the SQL query to call the stored procedure
QSqlQuery query;
query.prepare("EXEC RejectFriendRequest :UserID, :FriendID");
query.bindValue(":UserID", userID);
query.bindValue(":FriendID", friendID);
// Execute the query and check for errors
if (query.exec()) {
if (query.next()) {
QString message = query.value(0).toString();
qDebug() << "Stored Procedure Result:" << message;
QMessageBox::information(this, "Friend Request", message);
// Refresh friend requests list
//loadFriendRequests();
}
} else {
qDebug() << "Error executing stored procedure:" << query.lastError().text();
QMessageBox::warning(this, "Error", "Failed to reject friend request.");
}
}
//// Example function to update friend request status
//void User::updateFriendRequestStatus(int userID, int friendID, const QString &status) {
// QSqlQuery query;
// query.prepare("UPDATE Friends SET Status = :Status WHERE UserID = :UserID AND FriendID = :FriendID");
// query.bindValue(":Status", status);