PSQL id 06.11.2024

This commit is contained in:
krivoshein
2024-11-06 16:06:32 +03:00
parent 0f1fa71c33
commit f4ca58ce91
89 changed files with 1131 additions and 763 deletions

View File

@@ -36,9 +36,9 @@ bool DataBaseInstructors::AuthorizationInstructor(QString login, QString passwor
Instructor instructor = listOfInstructors[i];
instructor.setLoggedIn(true);
bool result = dbLMS->updateInstructor(instructor);
int id = dbLMS->updateInstructor(instructor);
if(result)
if(id)
{
LoadInstructorsPSQL();
return true;
@@ -63,9 +63,9 @@ bool DataBaseInstructors::deAuthorizationInstructor(QString login)
Instructor instructor = listOfInstructors[i];
instructor.setLoggedIn(false);
bool result = dbLMS->updateInstructor(instructor);
int id = dbLMS->updateInstructor(instructor);
if(result)
if(id)
{
LoadInstructorsPSQL();
return true;
@@ -77,12 +77,12 @@ bool DataBaseInstructors::deAuthorizationInstructor(QString login)
return false;
}
Instructor DataBaseInstructors::getInstructor(QString name)
Instructor DataBaseInstructors::getInstructor(int id)
{
//Инструкторы
for(int i = 0; i < listOfInstructors.count(); i++)
{
if(listOfInstructors[i].getName() == name)
if(listOfInstructors[i].getID() == id)
return listOfInstructors[i];
}
return Instructor();
@@ -97,7 +97,7 @@ QString DataBaseInstructors::getNameInstructorByLogin(QString login)
}
return QString(QStringLiteral(""));
}
/*
QString DataBaseInstructors::getAuthorizedInstructorName()
{
//Инструкторы
@@ -107,9 +107,9 @@ QString DataBaseInstructors::getAuthorizedInstructorName()
return listOfInstructors[i].getName();
}
return QStringLiteral("");
}
}*/
QString DataBaseInstructors::newInstructor()
int DataBaseInstructors::newInstructor()
{
Instructor instructor;
instructor.setName(generateDefaultNameInstructor());
@@ -119,83 +119,80 @@ QString DataBaseInstructors::newInstructor()
instructor.setArchived(false);
instructor.setLoggedIn(false);
bool result = dbLMS->insertInstructor(instructor);
int id = dbLMS->insertInstructor(instructor);
if(result)
if(id)
{
//listOfInstructors.append(instructor);
LoadInstructorsPSQL();
return instructor.getName();
return id;
}
else
return QStringLiteral("");
return 0;
}
void DataBaseInstructors::deleteInstructor(QString name)
int DataBaseInstructors::deleteInstructor(int id)
{
//Инструкторы
for(int i = 0; i < listOfInstructors.count(); i++)
{
if(listOfInstructors[i].getName() == name)
if(listOfInstructors[i].getID() == id)
{
int id = listOfInstructors[i].getID();
int id_del = dbLMS->deleteInstructor(id);
bool result = dbLMS->deleteInstructor(id);
if(result)
if(id_del)
{
LoadInstructorsPSQL();
//listOfInstructors.removeAt(i);
return;
return id_del;
}
else
return 0;
}
}
return 0;
}
bool DataBaseInstructors::editInstructor(QString name, Instructor instructor)
int DataBaseInstructors::editInstructor(Instructor instructor)
{
//Инструкторы
for(int i = 0; i < listOfInstructors.count(); i++)
{
if(listOfInstructors[i].getName() == name)
if(listOfInstructors[i].getID() == instructor.getID())
{
if( (!checkExistNameInstructor(instructor.getName()) || instructor.getName() == name) &&
if( (!checkExistNameInstructor(instructor.getName()) || instructor.getName() == listOfInstructors[i].getName()) &&
(!checkExistLoginInstructor(instructor.getLogin()) || instructor.getLogin() == listOfInstructors[i].getLogin()) )
{
//instructor.setID(listOfInstructors[i].getID());
int id = dbLMS->updateInstructor(instructor);
bool result = dbLMS->updateInstructor(instructor);
if(result)
if(id)
{
//listOfInstructors.replace(i, instructor);
LoadInstructorsPSQL();
return true;
return id;
}
else
return false;
return 0;
}
}
}
return false;
return 0;
}
bool DataBaseInstructors::isAdmin(QString name)
bool DataBaseInstructors::isAdmin(int id)
{
//Инструкторы
for(int i = 0; i < listOfInstructors.count(); i++)
{
if(listOfInstructors[i].getName() == name)
if(listOfInstructors[i].getID() == id)
return listOfInstructors[i].getIsAdmin();
}
return false;
}
bool DataBaseInstructors::isArchived(QString name)
bool DataBaseInstructors::isArchived(int id)
{
//Инструкторы
for(int i = 0; i < listOfInstructors.count(); i++)
{
if(listOfInstructors[i].getName() == name)
if(listOfInstructors[i].getID() == id)
return listOfInstructors[i].getArchived();
}
return false;

View File

@@ -9,7 +9,7 @@
#include "databaselms.h"
class INSTRUCTORSANDTRAINEES_EXPORT DataBaseInstructors : QObject
class DataBaseInstructors : QObject
{
Q_OBJECT
@@ -24,18 +24,18 @@ public:
QList<Instructor> getListInstructors(){ return listOfInstructors; } //Для загрузки
Instructor getInstructor(QString name);
Instructor getInstructor(int id);
QString getNameInstructorByLogin(QString login);
QString getAuthorizedInstructorName();
//QString getAuthorizedInstructorName();
QString newInstructor();
void deleteInstructor(QString name);
bool editInstructor(QString name, Instructor instructor);
int newInstructor();
int deleteInstructor(int id);
int editInstructor(Instructor instructor);
bool isAdmin(QString name);
bool isArchived(QString name);
bool isAdmin(int id);
bool isArchived(int id);
private:

View File

@@ -4,8 +4,7 @@
#include "ui_editorinstructors.h"
EditorInstructors::EditorInstructors(DataBaseInstructors* db, bool adminMode, QWidget *parent) :
//QDialog(parent),
InstructorsView(db, CommonView::TypeView::control, parent),
InstructorsView(db, CommonView::TypeView::control, adminMode, parent),
ui(new Ui::EditorInstructors)
{
ui->setupUi((QDialog*)this);
@@ -22,9 +21,9 @@ EditorInstructors::~EditorInstructors()
void EditorInstructors::on_btnNewInstructor_clicked()
{
QString name = dbInstructors->newInstructor();
int id = dbInstructors->newInstructor();
loadInstructorsFromDB();
setCurrentInstructor(name);
setCurrentInstructor(id);
}
void EditorInstructors::on_btnDeleteInstructor_clicked()
@@ -37,9 +36,9 @@ void EditorInstructors::on_btnDeleteInstructor_clicked()
if(treeItemParent == nullptr)
{//Выбран Инструктор
QString name = treeItemCurrent->text(0);
int id = treeItemCurrent->text(0).toInt();
if(dbInstructors->isAdmin(name))
if(dbInstructors->isAdmin(id))
{//Это Админ!
QMessageBox::critical(this, tr("Error!"), tr("You cannot delete the Administrator."));
return;
@@ -47,7 +46,7 @@ void EditorInstructors::on_btnDeleteInstructor_clicked()
if(QMessageBox::warning(this, tr("Attention!"), tr("The deletion will be irrevocable.\nDelete it anyway?"), QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Ok)
{
dbInstructors->deleteInstructor(name);
dbInstructors->deleteInstructor(id);
loadInstructorsFromDB();
}
}
@@ -64,29 +63,29 @@ void EditorInstructors::on_btnToOrFromArchive_clicked()
if(treeItemParent == nullptr)
{//Выбран Инструктор
QString name = treeItemCurrent->text(0);
int id = treeItemCurrent->text(0).toInt();
Instructor instructor = dbInstructors->getInstructor(name);
Instructor instructor = dbInstructors->getInstructor(id);
if(instructor.getArchived())
{//Архивный
instructor.setArchived(false);
if(dbInstructors->editInstructor(name, instructor))
if(int id_edit = dbInstructors->editInstructor(instructor))
{
loadInstructorsFromDB();
setCurrentInstructor(instructor.getName());
setCurrentInstructor(id_edit);
}
}
else
{//Не Архивный
instructor.setArchived(true);
if(dbInstructors->editInstructor(name, instructor))
if(int id_edit = dbInstructors->editInstructor(instructor))
{
if(!archiveVisible)
ui->btnArchive->click();
loadInstructorsFromDB();
setCurrentInstructor(instructor.getName());
setCurrentInstructor(id_edit);
}
}
}
@@ -104,19 +103,19 @@ void EditorInstructors::on_btnEdit_clicked()
if(treeItemParent == nullptr)
{//Выбран Инструктор
QString name = treeItemCurrent->text(0);
int id = treeItemCurrent->text(0).toInt();
DialogEditInstructor dlg(this);
dlg.setInstructor(dbInstructors->getInstructor(name));
dlg.setInstructor(dbInstructors->getInstructor(id));
switch( dlg.exec() )
{
case QDialog::Accepted:
{
if(dbInstructors->editInstructor(name, dlg.getInstructor()))
if(int id_edit = dbInstructors->editInstructor(dlg.getInstructor()))
{
loadInstructorsFromDB();
setCurrentInstructor(dlg.getInstructor().getName());
setCurrentInstructor(id_edit);
}
else
QMessageBox::critical(this, tr("Editing error!"),
@@ -149,9 +148,9 @@ void EditorInstructors::on_treeWidget_currentItemChanged(QTreeWidgetItem *curren
if(treeItemParent == nullptr)
{//Выбран инструктор
QString name = current->text(0);
int id = current->text(0).toInt();
if(dbInstructors->isArchived(name))
if(dbInstructors->isArchived(id))
{//Архивный
ui->btnToOrFromArchive->setText(tr("From archive"));
ui->btnToOrFromArchive->setIcon(QIcon(QStringLiteral(":/icons/instructorFromArchive.png")));
@@ -164,7 +163,7 @@ void EditorInstructors::on_treeWidget_currentItemChanged(QTreeWidgetItem *curren
ui->btnNewInstructor->setEnabled(true);
if(dbInstructors->isAdmin(name))
if(dbInstructors->isAdmin(id))
{//Это Админ! Удалять/Архивировать нельзя!
ui->btnDeleteInstructor->setEnabled(false);
ui->btnToOrFromArchive->setEnabled(false);
@@ -173,7 +172,7 @@ void EditorInstructors::on_treeWidget_currentItemChanged(QTreeWidgetItem *curren
{
ui->btnToOrFromArchive->setEnabled(true);
if(dbInstructors->isArchived(name))
if(dbInstructors->isArchived(id))
ui->btnDeleteInstructor->setEnabled(true);
else
ui->btnDeleteInstructor->setEnabled(false);
@@ -184,13 +183,13 @@ void EditorInstructors::on_treeWidget_currentItemChanged(QTreeWidgetItem *curren
}
}
void EditorInstructors::setCurrentInstructor(QString name)
void EditorInstructors::setCurrentInstructor(int id)
{
for(int i = 0; i < treeWidget->topLevelItemCount(); i++)
{
QTreeWidgetItem * item = treeWidget->topLevelItem(i);
if(item != nullptr)
if(item->text(0) == name)
if(item->text(0).toInt() == id)
{
treeWidget->setCurrentItem(item);
break;

View File

@@ -17,7 +17,7 @@ class EditorInstructors : public InstructorsView
Q_OBJECT
public:
explicit EditorInstructors(DataBaseInstructors* db, bool adminMode = false, QWidget *parent = nullptr);
explicit EditorInstructors(DataBaseInstructors* db, bool adminMode, QWidget *parent = nullptr);
~EditorInstructors();
private Q_SLOTS:
@@ -29,7 +29,7 @@ private Q_SLOTS:
void on_treeWidget_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous);
private:
void setCurrentInstructor(QString name);
void setCurrentInstructor(int id);
private:
Ui::EditorInstructors *ui;

View File

@@ -2,8 +2,8 @@
#include <QTranslator>
#include "instructorsview.h"
InstructorsView::InstructorsView(DataBaseInstructors* db, TypeView type, QWidget *parent):
CommonView(type, parent),
InstructorsView::InstructorsView(DataBaseInstructors* db, TypeView type, bool adminMode, QWidget *parent):
CommonView(type, adminMode, parent),
dbInstructors(db)
{
@@ -16,34 +16,35 @@ void InstructorsView::preparationTreeWidget(QTreeWidget* tree)
if(treeWidget == nullptr)
return;
treeWidget->setColumnCount(6);
treeWidget->setColumnCount(7);
reSetHeadTreeWidget();
treeWidget->header()->setStyleSheet(QStringLiteral("font-size: 10pt;"));
treeWidget->setColumnWidth(0, 250);
treeWidget->setColumnWidth(1, 100);
treeWidget->setColumnWidth(0, 50);
treeWidget->setColumnWidth(1, 250);
treeWidget->setColumnWidth(2, 100);
treeWidget->setColumnWidth(3, 100);
treeWidget->setColumnWidth(4, 80);
treeWidget->setColumnWidth(4, 100);
treeWidget->setColumnWidth(5, 80);
treeWidget->setColumnWidth(6, 80);
if(typeView == TypeView::onlyView)
{//onlyView
treeWidget->setColumnHidden(1, true);
treeWidget->setColumnHidden(2, true);
treeWidget->setColumnHidden(4, true);
treeWidget->setColumnHidden(3, true);
treeWidget->setColumnHidden(5, true);
}
else
{//control
treeWidget->setColumnHidden(4, true);
treeWidget->setColumnHidden(5, true);
}
}
void InstructorsView::loadInstructorsFromDB()
{
//dbInstructors->LoadInstructorsPSQL();
dbInstructors->LoadInstructorsPSQL();
if(treeWidget == nullptr)
return;
@@ -58,47 +59,48 @@ void InstructorsView::loadInstructorsFromDB()
{
QTreeWidgetItem *ItemInstructor = new QTreeWidgetItem(treeWidget);
ItemInstructor->setText(0, instructor.getName());
ItemInstructor->setText(1, instructor.getLogin());
ItemInstructor->setText(2, instructor.getPassword());
ItemInstructor->setText(0, QString::number(instructor.getID()));
ItemInstructor->setText(1, instructor.getName());
ItemInstructor->setText(2, instructor.getLogin());
ItemInstructor->setText(3, instructor.getPassword());
if(instructor.getArchived())
{//Архивный
ItemInstructor->setText(4, tr("yes"));
ItemInstructor->setIcon(0, QIcon(QStringLiteral(":/icons/instructorArchive.png")));
ItemInstructor->setText(5, tr("yes"));
ItemInstructor->setIcon(1, QIcon(QStringLiteral(":/icons/instructorArchive.png")));
setItemColorArchive(ItemInstructor);
}
else
{//Не Архивный
ItemInstructor->setText(4, tr("no"));
ItemInstructor->setIcon(0, QIcon(QStringLiteral(":/icons/instructor.png")));
ItemInstructor->setText(5, tr("no"));
ItemInstructor->setIcon(1, QIcon(QStringLiteral(":/icons/instructor.png")));
setItemColorNoArchive(ItemInstructor);
}
if(instructor.getIsAdmin())
{//Админ
ItemInstructor->setText(3, tr("yes"));
ItemInstructor->setIcon(0, QIcon(QStringLiteral(":/icons/admin.png")));
ItemInstructor->setText(4, tr("yes"));
ItemInstructor->setIcon(1, QIcon(QStringLiteral(":/icons/admin.png")));
}
else
{//Не Админ
ItemInstructor->setText(3, tr("no"));
ItemInstructor->setText(4, tr("no"));
}
if(instructor.getLoggedIn())
{//Залогинен
ItemInstructor->setText(5, tr("yes"));
ItemInstructor->setIcon(5, QIcon(QStringLiteral(":/icons/circleGreen.png")));
ItemInstructor->setText(6, tr("yes"));
ItemInstructor->setIcon(6, QIcon(QStringLiteral(":/icons/circleGreen.png")));
}
else
{//Не Залогинен
ItemInstructor->setText(5, tr("no"));
ItemInstructor->setIcon(5, QIcon(QStringLiteral(":/icons/circleGray.png")));
ItemInstructor->setText(6, tr("no"));
ItemInstructor->setIcon(6, QIcon(QStringLiteral(":/icons/circleGray.png")));
}
//Скрываем архивных (при необходимости)
if(instructor.getArchived())
if(!archiveVisible)
if(! archiveVisible)
ItemInstructor->setHidden(true);
//Скрываем незалогиненых (при необходимости)
@@ -108,7 +110,7 @@ void InstructorsView::loadInstructorsFromDB()
}
treeWidget->setSortingEnabled(true);
treeWidget->sortItems(0, Qt::SortOrder::AscendingOrder);
treeWidget->sortItems(1, Qt::SortOrder::AscendingOrder);
treeWidget->expandAll();
if(typeView == TypeView::control)
@@ -121,7 +123,7 @@ void InstructorsView::loadInstructorsFromDB()
void InstructorsView::reSetHeadTreeWidget()
{
QStringList listHeaders = {tr("Instructor"), tr("Login"), tr("Password"), tr("Administrator"), tr("Archived"), tr("Logged")};
QStringList listHeaders = {tr("ID"), tr("Instructor"), tr("Login"), tr("Password"), tr("Administrator"), tr("Archived"), tr("Logged")};
treeWidget->setHeaderLabels(listHeaders);
}

View File

@@ -7,12 +7,12 @@
//Родительский класс представления БД Инструкторов (для просмотра и управления)
class INSTRUCTORSANDTRAINEES_EXPORT InstructorsView: public CommonView
class InstructorsView: public CommonView
{
Q_OBJECT
public:
InstructorsView(DataBaseInstructors* db, TypeView type, QWidget *parent = nullptr);
InstructorsView(DataBaseInstructors* db, TypeView type, bool adminMode, QWidget *parent = nullptr);
protected:
void preparationTreeWidget(QTreeWidget* tree);

View File

@@ -4,8 +4,7 @@
#include "ui_viewerinstructors.h"
ViewerInstructors::ViewerInstructors(DataBaseInstructors* db, bool adminMode, QWidget *parent) :
//QWidget(parent),
InstructorsView(db, CommonView::TypeView::onlyView, parent),
InstructorsView(db, CommonView::TypeView::onlyView, adminMode, parent),
ui(new Ui::ViewerInstructors)
{
ui->setupUi(this);
@@ -63,16 +62,12 @@ void ViewerInstructors::on_btnEditorInstructors_clicked()
}
EditorInstructors editorInstructors(dbInstructors, adminMode);
//dlg.setWindowTitle(tr("List instructors"));
//dlg.exec();
//dlg.show();
QDialog* dialog = new QDialog(this);
QHBoxLayout *layout = new QHBoxLayout(dialog);
layout->addWidget(&editorInstructors);
dialog->setWindowTitle(tr("List instructors"));
dialog->setWindowTitle(tr("Editor of instructors"));
dialog->setMinimumSize(1400, 800);
dialog->exec();
dbInstructors->LoadInstructorsPSQL();
loadInstructorsFromDB();
}

View File

@@ -12,7 +12,7 @@ class ViewerInstructors;
//Виджет только для просмотра БД Инструкторов
class INSTRUCTORSANDTRAINEES_EXPORT ViewerInstructors : public InstructorsView
class ViewerInstructors : public InstructorsView
{
Q_OBJECT
@@ -29,7 +29,7 @@ public Q_SLOTS:
void slot_LanguageChanged(QString language);
public:
void setFilterInstructorLoggedIn( bool enabled );
void setFilterInstructorLoggedIn(bool enabled);
private slots:
void on_btnEditorInstructors_clicked();