-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtenantdialog.cpp
61 lines (56 loc) · 2.1 KB
/
tenantdialog.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
#include "tenantdialog.h"
#include "database.h"
#include "shared.h"
#include "ui_tenantdialog.h"
#include <QLineEdit>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlRecord>
TenantDialog::TenantDialog(QSqlTableModel* tenants, QWidget* parent)
: QDialog(parent)
, ui(new Ui::TenantDialog)
, m_tenants_model(tenants)
{
setAttribute(Qt::WA_DeleteOnClose);
ui->setupUi(this);
connect(ui->button_box, &QDialogButtonBox::accepted, this, [this]() {
QString first, middle, last;
first = { ui->line_edit_first->text().trimmed() };
middle = { ui->line_edit_middle->text().trimmed() };
last = { ui->line_edit_last->text().trimmed() };
if (first.isEmpty() or last.isEmpty()) {
QMessageBox::warning(this, "Tenant error", "First and Last name are required");
} else {
auto record = m_tenants_model->record();
record.setGenerated(Db::TENANT_ID, false);
record.setValue(Db::TENANT_FIRST, first);
if (middle.isEmpty()) {
record.setGenerated(Db::TENANT_MIDDLE, false);
} else {
record.setValue(Db::TENANT_MIDDLE, middle);
}
record.setValue(Db::TENANT_LAST, last);
record.setNull(Db::TENANT_EMAIL);
record.setNull(Db::TENANT_PHONE);
m_tenants_model->insertRecord(-1, record);
const bool submitted = m_tenants_model->submitAll();
auto new_id = m_tenants_model->query().lastInsertId().toInt();
if (submitted && new_id > 0) {
qDebug() << "tenant created, id: " << new_id;
accept();
} else {
m_tenants_model->revertAll();
ui->line_edit_first->clear();
ui->line_edit_middle->clear();
ui->line_edit_last->clear();
auto err = m_tenants_model->lastError();
qDebug() << "tenants submit error: " << err;
Shared::handle_error(this, err.nativeErrorCode());
}
}
});
}
TenantDialog::~TenantDialog()
{
delete ui;
}