-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddlease.cpp
More file actions
143 lines (124 loc) · 3.63 KB
/
addlease.cpp
File metadata and controls
143 lines (124 loc) · 3.63 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
#include "addlease.h"
#include "calender.h"
#include "ui_addlease.h"
AddLease::AddLease(QSqlRelationalTableModel *m, QWidget *parent)
: QDialog(parent), ui(new Ui::AddLease) {
ui->setupUi(this);
this->m = m;
r = ui->rooms;
k = ui->keys;
p = ui->people;
u = ui->to;
f = ui->from;
update_rooms();
reload_date_time();
update_keys();
populate_people();
}
AddLease::~AddLease() { delete ui; }
bool AddLease::update_rooms() {
// todo error validation
return update_rooms(new QString());
}
bool AddLease::populate_people() {
QSqlQuery q("SELECT users.id, users.name FROM users");
while (q.next()) {
p->addItem(q.value(q.record().indexOf("users.name")).toString());
}
return true;
}
bool AddLease::update_keys() {
key_ids.clear();
k->clear();
QSqlQuery q;
if (r->currentIndex() > 0) {
q.prepare("SELECT key_data.id, key_data.key_type AS key_type FROM key_data "
"JOIN rooms "
"ON key_data.key_room=rooms.id WHERE rooms.name = :r ");
q.bindValue(":r", r->currentText());
} else {
q.prepare("SELECT id, key_type FROM key_data");
}
if (!q.exec()) {
}
std::cout << q.lastQuery().toStdString() << std::endl;
std::cout << q.lastError().text().toStdString() << std::endl;
QStringList l;
QSqlQuery v("SELECT * FROM leases");
v.exec();
std::vector<int> _leases;
{
int lease_offset = v.record().indexOf("key_id");
while (v.next()) {
_leases.push_back(v.value(lease_offset).toInt());
}
}
while (q.next()) {
int name_offset = q.record().indexOf("key_type");
int id_offset = q.record().indexOf("id");
int id = q.value(id_offset).toInt();
if (std::find(_leases.begin(), _leases.end(), id) == _leases.end()) {
key_ids.push_back(id);
QString s = q.value(name_offset).toString();
if (!l.contains(s)) {
l.push_back(s);
k->addItem(s);
}
}
}
return true;
}
bool AddLease::update_rooms(QString *s) {
// todo error validation
QSqlQuery q("SELECT name FROM rooms");
int r_name_offset = q.record().indexOf("name");
r->addItem("None");
while (q.next()) {
r->addItem(QString(q.value(r_name_offset).toString()));
}
return true;
}
void AddLease::reload_date_time() {
u->setDateTime(QDateTime(QDate::currentDate(), QTime::currentTime()));
f->setDateTime(QDateTime(QDate::currentDate(), QTime::currentTime()));
}
void AddLease::on_rooms_currentIndexChanged(int index) { update_keys(); }
int AddLease::get_id_of_selected_user() {
QSqlQuery q;
q.prepare("SELECT id FROM users WHERE name LIKE :name ");
q.bindValue(":name", p->currentText());
if (!q.exec()) {
qDebug() << q.lastError();
}
int index = q.record().indexOf("id");
q.first();
int answer = q.value(index).toInt();
if (answer == -1) {
std::cout << "Error" << std::endl;
throw;
}
return answer;
}
void AddLease::on_buttonBox_accepted() {
int _k = get_id_of_selected_user();
QSqlQuery q;
qDebug() << _k;
q.prepare("INSERT INTO leases (key_id, start_ts, end_ts, user_id) VALUES "
"(:id, :sts, :ets, :u_id);");
q.bindValue(":id", this->key_ids[k->currentIndex()]);
q.bindValue(":sts", f->dateTime());
q.bindValue(":ets", u->dateTime());
q.bindValue(":u_id", _k);
q.exec();
std::cout << q.lastQuery().toStdString() << std::endl;
std::cout << q.lastError().text().toStdString() << std::endl;
qDebug() << q.lastError();
m->select();
// key_data.key_room=rooms.id ");
}
void AddLease::on_cal_1_clicked() { on_cal_clicked(f); }
void AddLease::on_cal_2_clicked() { on_cal_clicked(u); }
void AddLease::on_cal_clicked(QDateTimeEdit *dt) {
Calender *cal = new Calender(dt);
cal->show();
}