-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcheck_admin.cpp
More file actions
110 lines (91 loc) · 4.34 KB
/
check_admin.cpp
File metadata and controls
110 lines (91 loc) · 4.34 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
#include "check_admin.h"
#include "ui_check_admin.h"
#include <QApplication>
#include <QMessageBox>
#include <QVBoxLayout>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlDatabase>
#include <QDate>
#include <QDebug>
check_Admin::check_Admin(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::check_Admin)
, tableView(new QTableView(this)),
model(new QSqlTableModel(this))
{
ui->setupUi(this);
// Set up the SQL Table Model
model->setTable("users"); // Specify the table to display data from
model->select(); // Load data lazily from the database
// Set up the QTableView
tableView->setModel(model);
tableView->setSelectionMode(QAbstractItemView::SingleSelection); // Allow single row selection
tableView->setSelectionBehavior(QAbstractItemView::SelectRows); // Select entire rows
tableView->setEditTriggers(QAbstractItemView::NoEditTriggers); // Disable cell editing
// Add the table view to the layout
ui->table_horizontalLayout->addWidget(tableView);
// Adjust column sizes based on the content
tableView->resizeColumnsToContents();
// Set a minimum width for each column to ensure the table looks good
for (int column = 0; column < model->columnCount(); ++column) {
// Set a minimum width for the column, you can adjust this value as needed
tableView->setColumnWidth(column, qMax(tableView->columnWidth(column), 100)); // Minimum width is 100px
}
}
check_Admin::~check_Admin()
{
delete ui;
}
void check_Admin::on_Delete_btn_clicked()
{
// Get the selected index from the table view
QModelIndexList selectedIndexes = tableView->selectionModel()->selectedRows();
// Check if at least one row is selected
if (selectedIndexes.isEmpty()) {
QMessageBox::warning(this, "Warning", "Please select an admin to delete.");
return;
}
// Loop through selected rows (in case of multiple row selection)
for (const QModelIndex &index : selectedIndexes) {
int row = index.row(); // Get the row index of the selected item
// Get the employee ID (assuming employee_id is the first column)
int employeeId = model->data(model->index(row, 0)).toInt(); // Adjust column index as necessary
// Start a transaction to ensure both delete actions (employee and permissions) succeed or fail together
QSqlDatabase::database().transaction();
// Remove the selected employee record from the model
if (!model->removeRow(row)) {
QMessageBox::critical(this, "Error", "Failed to delete the employee.");
return;
}
// Remove the associated permissions from the permissions table using employee_id
QSqlQuery deletePermissionsQuery;
deletePermissionsQuery.prepare("DELETE FROM User_permissions WHERE user_id = :user_id");
deletePermissionsQuery.bindValue(":user_id", employeeId); // Use employeeId as the user_id in the permissions table
if (!deletePermissionsQuery.exec()) {
QMessageBox::critical(this, "Error", "Failed to delete associated permissions.");
QSqlDatabase::database().rollback(); // Rollback the transaction if deleting permissions fails
return;
}
// Commit the transaction after both delete operations are successful
if (model->submitAll()) {
QSqlDatabase::database().commit();
QMessageBox::information(this, "Success", "Employee and associated permissions deleted successfully.");
refreshTable(); // Refresh the table view after deletion
} else {
QMessageBox::critical(this, "Error", "Failed to delete employee(s).");
QSqlDatabase::database().rollback(); // Rollback if model submit fails
}
}
}
void check_Admin::refreshTable(){
model->select(); // Reload data from the database into the model
tableView->resizeColumnsToContents(); // Adjust column sizes based on content
// Adjust column sizes based on the content
tableView->resizeColumnsToContents();
// Set a minimum width for each column to ensure the table looks good
for (int column = 0; column < model->columnCount(); ++column) {
// Set a minimum width for the column, you can adjust this value as needed
tableView->setColumnWidth(column, qMax(tableView->columnWidth(column), 100)); // Minimum width is 100px
}
}