/
login.cpp
95 lines (86 loc) · 2.77 KB
/
login.cpp
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
#include "login.h"
#include "ui_login.h"
#include "mainwindow.h"
#include "QDebug.h"
#include <QSqlQuery>
#include "registerdialog.h"
login::login(QWidget *parent) :
QDialog(parent),
ui(new Ui::login)
{
ui->setupUi(this);
this->setWindowTitle("Login window");
this->setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint));
ui->label_3->setVisible(false);
}
login::~login()
{
delete ui;
}
void login::addMW(MainWindow *MW) {
this->mainW = MW;
}
#include <QSqlResult>
void login::on_pushButton_Ok_clicked()
{
const QString entered_login(ui->lineEdit_login->text());
const QString entered_password(ui->lineEdit_password->text());
QString correct_password;
bool connection_established = mainW->openDB();
if (!connection_established)
{
/* QMessageBox::information(0, "Connection problem", "Cannot connect to server.\n"
"Check your connection settings (settings.ini - located in program folder)");*/
return;
}
QSqlQuery query;
query.prepare("select password_hash, customer_id from CUSTOMER where name = :login");
query.bindValue(":login", entered_login);
qDebug()<<"query exec"<<query.exec();
qDebug()<<query.lastError();
qDebug()<<"query.next - "<<query.next();
if (query.isNull(0))
{
ui->label_3->setVisible(true);
return;
}
correct_password = query.value(0).toString();
qDebug()<<"password: "<<correct_password;
mainW->setCurrent_customer_ID(query.value(1).toInt());
qDebug()<<"customer ID: "<< mainW->getCurrent_customer_ID();
mainW->closeDB();
if (entered_password == correct_password)
{
this->setVisible(false);
mainW->centralWidget()->setVisible(true);
mainW->dbget_Book();
mainW->setUsername(ui->lineEdit_login->text());
mainW->update_wallet();
// mainW->update_tableView_Books();
mainW->update_tableView_Bundles();
mainW->update_tableView_Cart();
mainW->update_tableView_History();
} else ui->label_3->setVisible(true);
}
void login::on_pushButton_Register_clicked()
{
RegisterDialog regDialog(this);
// qDebug()<<"dialog returned: "<<regDialog.exec();
mainW->openDB();
if (regDialog.exec())
{
QSqlQuery query;
query.prepare("call CUSTOMER_INSERT(:login, :email, :phone, :password)");
query.bindValue(":login", regDialog.newLogin);
query.bindValue(":email", regDialog.newEmail);
query.bindValue(":phone", regDialog.newPhone);
query.bindValue(":password", regDialog.newPassword);
qDebug()<<"customer_insert exec: "<<query.exec();
qDebug()<<"last error:"<<query.lastError();
}
mainW->closeDB();
}
void login::on_pushButton_2_clicked()
{
mainW->close();
}