Рефакт

This commit is contained in:
2025-08-12 17:52:32 +03:00
parent bcd58e9631
commit 7466e33599
7 changed files with 150 additions and 245 deletions

View File

@@ -7,6 +7,7 @@ add_library(DataBaseLMS SHARED
databaselms.cpp
databaselms_tasks.cpp
databaselms_groups.cpp
databaselms_users.cpp
databaselms_instructors.cpp
databaselms_trainees.cpp
databaselms.h

View File

@@ -6,6 +6,9 @@
#include <QMessageBox>
#include <QDomDocument>
const QString DataBaseLMS::TypeUserDBInstructor = "instructor";
const QString DataBaseLMS::TypeUserDBTrainee = "trainee";
DataBaseLMS::DataBaseLMS(QWidget *ownerWidget):
db(nullptr),
transactionBegined(false),
@@ -98,7 +101,7 @@ DataBaseSettings DataBaseLMS::getDataBaseSettings()
QFile file("config/settings.xml");
if(!file.open(QIODevice::ReadOnly))
{
QMessageBox::critical(nullptr, "Attention!", "The file could not be opened: config/settings.xml");
QMessageBox::critical(ownerWidget, "Attention!", "The file could not be opened: config/settings.xml");
return settings;
}
QXmlStreamReader xmlReader(&file);

View File

@@ -30,8 +30,8 @@ public:
~DataBaseLMS();
public:
const QString TypeUserDBInstructor = "instructor";
const QString TypeUserDBTrainee = "trainee";
static const QString TypeUserDBInstructor;
static const QString TypeUserDBTrainee;
protected:
//Подключение
@@ -43,17 +43,19 @@ protected:
bool transactionBegin();
bool transactionEnd();
//Юзер
int selectUserID(QString type, QString login, QString password = QStringLiteral(""));
QString selectUserNameByLogin(QString type, QString login);
bool selectUserLoggedIn(QString type, int id_user);
bool selectUserArchived(QString type, int id_user);
int updateUserLoggedIn(QString type, int id_user, bool loggedIn);
bool updateAllUsersLoggedIn(QString type, bool loggedIn);
int updateUserArchived(QString type, int id_user, bool archived);
//Инструктор
QList<Instructor> selectAllInstructors();
Instructor selectInstructor(int id_instructor);
int selectInstructorID(QString login, QString password = QStringLiteral(""));
QString selectInstructorNameByLogin(QString login);
bool selectInstructorIsAdmin(int id_instructor);
bool selectInstructorLoggedIn(int id_instructor);
bool selectInstructorArchived(int id_instructor);
int updateInstructorLoggedIn(int id_instructor, bool loggedIn);
bool updateAllInstructorsLoggedIn(bool loggedIn);
int updateInstructorArchived(int id_instructor, bool archived);
int insertInstructor();
int insertInstructor(Instructor instructor);
int deleteInstructor(int id_instructor);
@@ -71,15 +73,8 @@ protected:
QList<Trainee> selectAllTrainees();
Trainee selectTrainee(int id_trainee);
QList<Trainee> selectAllTraineesInGroup(int id_group);
int selectTraineeID(QString login, QString password = QStringLiteral(""));
QString selectTraineeNameByLogin(QString login);
QString selectTraineeNameOnComputer(QString computer_name);
Trainee selectTraineeOnComputer(QString computer_name);
bool selectTraineeArchived(int id_trainee);
bool selectTraineeLoggedIn(int id_trainee);
int updateTraineeLoggedIn(int id_trainee, bool loggedIn);
bool updateAllTraineesLoggedIn(bool loggedIn);
int updateTraineeArchived(int id_trainee, bool archived);
int insertTrainee(int id_group);
int insertTrainee(Trainee trainee);
int deleteTrainee(int id_trainee);

View File

@@ -67,42 +67,6 @@ Instructor DataBaseLMS::selectInstructor(int id_instructor)
return instructor;
}
int DataBaseLMS::selectInstructorID(QString login, QString password)
{
QString queryStr;
if(password != QStringLiteral(""))
{
queryStr = QString("SELECT users.user_id "
"FROM public.users "
"WHERE login = '%1' AND password = '%2' AND users.type = '%3' ").arg(
login,
password,
TypeUserDBInstructor);
}
else
{
queryStr = QString("SELECT users.user_id "
"FROM public.users "
"WHERE login = '%1' AND users.type = '%2' ").arg(
login,
TypeUserDBInstructor);
}
return queryExecInt(queryStr);
}
QString DataBaseLMS::selectInstructorNameByLogin(QString login)
{
QString queryStr = QString("SELECT users.name "
"FROM public.users "
"WHERE users.login = '%1' AND users.type = '%2' ").arg(
login,
TypeUserDBInstructor );
return queryExecString(queryStr);
}
bool DataBaseLMS::selectInstructorIsAdmin(int id_instructor)
{
QString queryStr = QString("SELECT users.is_admin "
@@ -114,67 +78,6 @@ bool DataBaseLMS::selectInstructorIsAdmin(int id_instructor)
return queryExecBool(queryStr);
}
bool DataBaseLMS::selectInstructorLoggedIn(int id_instructor)
{
QString queryStr = QString("SELECT users.logged_in "
"FROM public.users "
"WHERE user_id = %1 AND users.type = '%2' ").arg(
QString::number(id_instructor),
TypeUserDBInstructor );
return queryExecBool(queryStr);
}
bool DataBaseLMS::selectInstructorArchived(int id_instructor)
{
QString queryStr = QString("SELECT users.archived "
"FROM public.users "
"WHERE user_id = %1 AND users.type = '%2' ").arg(
QString::number(id_instructor),
TypeUserDBInstructor );
return queryExecBool(queryStr);
}
int DataBaseLMS::updateInstructorLoggedIn(int id_instructor, bool loggedIn)
{
QString queryStr = QString("UPDATE public.users "
"SET logged_in = %1 "
"WHERE user_id = %2 AND users.type = '%3' "
"RETURNING users.user_id").arg(
loggedIn ? "true" : "false",
QString::number(id_instructor),
TypeUserDBInstructor);
return queryExecInt(queryStr);
}
bool DataBaseLMS::updateAllInstructorsLoggedIn(bool loggedIn)
{
QString queryStr = QString("UPDATE public.users "
"SET logged_in = %1 "
"WHERE users.type = '%2' ").arg(
loggedIn ? "true" : "false",
TypeUserDBInstructor);
QSqlQuery query = QSqlQuery(*db);
return queryExec(queryStr, &query);
}
int DataBaseLMS::updateInstructorArchived(int id_instructor, bool archived)
{
QString queryStr = QString("UPDATE public.users "
"SET archived = %1 "
"WHERE user_id = %2 AND users.type = '%3' "
"RETURNING users.user_id").arg(
archived ? "true" : "false",
QString::number(id_instructor),
TypeUserDBInstructor);
return queryExecInt(queryStr);
}
int DataBaseLMS::insertInstructor()
{
QString queryStr = QString("INSERT INTO public.users (type, name, login, password, is_admin, archived, logged_in)"

View File

@@ -136,42 +136,6 @@ QList<Trainee> DataBaseLMS::selectAllTraineesInGroup(int id_group)
return listTrainees;
}
int DataBaseLMS::selectTraineeID(QString login, QString password)
{
QString queryStr;
if(password != QStringLiteral(""))
{
queryStr = QString("SELECT users.user_id "
"FROM public.users "
"WHERE login = '%1' AND password = '%2' AND users.type = '%3' ").arg(
login,
password,
TypeUserDBTrainee);
}
else
{
queryStr = QString("SELECT users.user_id "
"FROM public.users "
"WHERE login = '%1' AND users.type = '%2' ").arg(
login,
TypeUserDBTrainee);
}
return queryExecInt(queryStr);
}
QString DataBaseLMS::selectTraineeNameByLogin(QString login)
{
QString queryStr = QString("SELECT users.name "
"FROM public.users "
"WHERE users.login = '%1' AND users.type = '%2' ").arg(
login,
TypeUserDBTrainee);
return queryExecString(queryStr);
}
QString DataBaseLMS::selectTraineeNameOnComputer(QString computer_name)
{
QString queryStr = QString("SELECT users.name "
@@ -223,67 +187,6 @@ Trainee DataBaseLMS::selectTraineeOnComputer(QString computer_name)
return trainee;
}
bool DataBaseLMS::selectTraineeArchived(int id_trainee)
{
QString queryStr = QString("SELECT users.archived "
"FROM public.users "
"WHERE user_id = %1 AND users.type = '%2' ").arg(
QString::number(id_trainee),
TypeUserDBTrainee);
return queryExecBool(queryStr);
}
bool DataBaseLMS::selectTraineeLoggedIn(int id_trainee)
{
QString queryStr = QString("SELECT users.logged_in "
"FROM public.users "
"WHERE user_id = %1 AND users.type = '%2' ").arg(
QString::number(id_trainee),
TypeUserDBTrainee );
return queryExecBool(queryStr);
}
int DataBaseLMS::updateTraineeLoggedIn(int id_trainee, bool loggedIn)
{
QString queryStr = QString("UPDATE public.users "
"SET logged_in = %1 "
"WHERE user_id = %2 AND users.type = '%3' "
"RETURNING users.user_id").arg(
loggedIn ? "true" : "false",
QString::number(id_trainee),
TypeUserDBTrainee);
return queryExecInt(queryStr);
}
bool DataBaseLMS::updateAllTraineesLoggedIn(bool loggedIn)
{
QString queryStr = QString("UPDATE public.users "
"SET logged_in = %1 "
"WHERE users.type = '%2' ").arg(
loggedIn ? "true" : "false",
TypeUserDBTrainee);
QSqlQuery query = QSqlQuery(*db);
return queryExec(queryStr, &query);
}
int DataBaseLMS::updateTraineeArchived(int id_trainee, bool archived)
{
QString queryStr = QString("UPDATE public.users "
"SET archived = %1 "
"WHERE user_id = %2 AND users.type = '%3' "
"RETURNING users.user_id").arg(
archived ? "true" : "false",
QString::number(id_trainee),
TypeUserDBTrainee);
return queryExecInt(queryStr);
}
int DataBaseLMS::insertTrainee(int id_group)
{
QString queryStr = QString("INSERT INTO public.users (type, name, login, password, archived, logged_in, fk_group_id) "

View File

@@ -0,0 +1,101 @@
#include "databaselms.h"
#include <QtSql>
#include <QSqlDatabase>
#include <QSqlDriver>
#include <QMessageBox>
int DataBaseLMS::selectUserID(QString type, QString login, QString password)
{
QString queryStr;
if(password != QStringLiteral(""))
{
queryStr = QString("SELECT users.user_id "
"FROM public.users "
"WHERE login = '%1' AND password = '%2' AND users.type = '%3' ").arg(
login,
password,
type);
}
else
{
queryStr = QString("SELECT users.user_id "
"FROM public.users "
"WHERE login = '%1' AND users.type = '%2' ").arg(
login,
type);
}
return queryExecInt(queryStr);
}
QString DataBaseLMS::selectUserNameByLogin(QString type, QString login)
{
QString queryStr = QString("SELECT users.name "
"FROM public.users "
"WHERE users.login = '%1' AND users.type = '%2' ").arg(
login,
type );
return queryExecString(queryStr);
}
bool DataBaseLMS::selectUserLoggedIn(QString type, int id_user)
{
QString queryStr = QString("SELECT users.logged_in "
"FROM public.users "
"WHERE user_id = %1 AND users.type = '%2' ").arg(
QString::number(id_user),
type );
return queryExecBool(queryStr);
}
bool DataBaseLMS::selectUserArchived(QString type, int id_user)
{
QString queryStr = QString("SELECT users.archived "
"FROM public.users "
"WHERE user_id = %1 AND users.type = '%2' ").arg(
QString::number(id_user),
type );
return queryExecBool(queryStr);
}
int DataBaseLMS::updateUserLoggedIn(QString type, int id_user, bool loggedIn)
{
QString queryStr = QString("UPDATE public.users "
"SET logged_in = %1 "
"WHERE user_id = %2 AND users.type = '%3' "
"RETURNING users.user_id").arg(
loggedIn ? "true" : "false",
QString::number(id_user),
type);
return queryExecInt(queryStr);
}
bool DataBaseLMS::updateAllUsersLoggedIn(QString type, bool loggedIn)
{
QString queryStr = QString("UPDATE public.users "
"SET logged_in = %1 "
"WHERE users.type = '%2' ").arg(
loggedIn ? "true" : "false",
type);
return queryExecBool(queryStr);
}
int DataBaseLMS::updateUserArchived(QString type, int id_user, bool archived)
{
QString queryStr = QString("UPDATE public.users "
"SET archived = %1 "
"WHERE user_id = %2 AND users.type = '%3' "
"RETURNING users.user_id").arg(
archived ? "true" : "false",
QString::number(id_user),
type);
return queryExecInt(queryStr);
}

View File

@@ -6,7 +6,6 @@
#include "interfacedatabaselms.h"
InterfaceDataBaseLMS::InterfaceDataBaseLMS(QWidget *ownerWidget, QObject *parent):
//QWidget(parent),
QObject(parent),
DataBaseLMS(ownerWidget),
ownerWidget(ownerWidget)
@@ -24,12 +23,12 @@ bool InterfaceDataBaseLMS::ConnectionToDB()
{
if(!createConnection())
{
QMessageBox::critical(nullptr, dbSettings.dbName, tr("Connection error: ") + db->lastError().text());
QMessageBox::critical(ownerWidget, dbSettings.dbName, tr("Connection error: ") + db->lastError().text());
return false;
}
else
{
//QMessageBox::information(nullptr, dbName, tr("Connection is successful!"));
//QMessageBox::information(ownerWidget, dbName, tr("Connection is successful!"));
return true;
}
}
@@ -37,7 +36,7 @@ bool InterfaceDataBaseLMS::ConnectionToDB()
bool InterfaceDataBaseLMS::DisConnectionFromDB()
{
deleteConnection();
//QMessageBox::information(nullptr, dbName, tr("Disconnection is successful!"));
//QMessageBox::information(ownerWidget, dbName, tr("Disconnection is successful!"));
return true;
}
@@ -54,14 +53,14 @@ bool InterfaceDataBaseLMS::AuthorizationInstructor(QString login, QString passwo
if(! transactionBegin())
return false;
if(int id = selectInstructorID(login, password))
if(int id = selectUserID(DataBaseLMS::TypeUserDBInstructor, login, password))
{
if(isArchivedInstructor(id) || isLoggedInInstructor(id))
{
transactionEnd();
return false;
}
if(updateInstructorLoggedIn(id, true))
if(updateUserLoggedIn(DataBaseLMS::TypeUserDBInstructor, id, true))
return transactionEnd();
}
@@ -74,9 +73,9 @@ bool InterfaceDataBaseLMS::deAuthorizationInstructor(QString login)
if(! transactionBegin())
return false;
if(int id = selectInstructorID(login))
if(int id = selectUserID(DataBaseLMS::TypeUserDBInstructor, login))
{
if(updateInstructorLoggedIn(id, false))
if(updateUserLoggedIn(DataBaseLMS::TypeUserDBInstructor, id, false))
return transactionEnd();
}
@@ -86,17 +85,17 @@ bool InterfaceDataBaseLMS::deAuthorizationInstructor(QString login)
bool InterfaceDataBaseLMS::deAuthorizationAllInstructors()
{
return updateAllInstructorsLoggedIn(false);
return updateAllUsersLoggedIn(DataBaseLMS::TypeUserDBInstructor,false);
}
QString InterfaceDataBaseLMS::getNameInstructorByLogin(QString login)
{
return selectInstructorNameByLogin(login);
return selectUserNameByLogin(DataBaseLMS::TypeUserDBInstructor, login);
}
int InterfaceDataBaseLMS::getIdInstructorByLogin(QString login)
{
return selectInstructorID(login);
return selectUserID(DataBaseLMS::TypeUserDBInstructor, login);
}
QList<Instructor> InterfaceDataBaseLMS::getListInstructors()
@@ -127,35 +126,35 @@ int InterfaceDataBaseLMS::editInstructor(Instructor instructor)
{
if(instructor.getName() == QStringLiteral("<instructor>"))
{//Имя не корректно!
QMessageBox::critical(nullptr, tr("Editing error!"),
QMessageBox::critical(ownerWidget, tr("Editing error!"),
tr("Unacceptable instructor name has been entered.\nThe changes will not be accepted."));
return 0;
}
if(instructor.getLogin() == QStringLiteral("<login>"))
{//Логин не корректен!
QMessageBox::critical(nullptr, tr("Editing error!"),
QMessageBox::critical(ownerWidget, tr("Editing error!"),
tr("Unacceptable instructor login has been entered.\nThe changes will not be accepted."));
return 0;
}
if(instructor.getPassword() == QStringLiteral("<password>"))
{//Пароль не корректный!
QMessageBox::critical(nullptr, tr("Editing error!"),
QMessageBox::critical(ownerWidget, tr("Editing error!"),
tr("Unacceptable instructor password has been entered.\nThe changes will not be accepted."));
return 0;
}
if(instructor.getName() == exist_instructor.getName() && instructor.getID() != exist_instructor.getID())
{//Имя уже существует
QMessageBox::critical(nullptr, tr("Editing error!"),
QMessageBox::critical(ownerWidget, tr("Editing error!"),
tr("An existing instructor name has been entered."));
return 0;
}
if(instructor.getLogin() == exist_instructor.getLogin() && instructor.getID() != exist_instructor.getID())
{//Логин уже существует!
QMessageBox::critical(nullptr, tr("Editing error!"),
QMessageBox::critical(ownerWidget, tr("Editing error!"),
tr("An existing instructor login has been entered.\nThe changes will not be accepted."));
return 0;
}
@@ -171,12 +170,12 @@ bool InterfaceDataBaseLMS::isAdminInstructor(int id)
bool InterfaceDataBaseLMS::isArchivedInstructor(int id)
{
return selectInstructorArchived(id);
return selectUserArchived(DataBaseLMS::TypeUserDBInstructor, id);
}
bool InterfaceDataBaseLMS::isLoggedInInstructor(int id)
{
return selectInstructorLoggedIn(id);
return selectUserLoggedIn(DataBaseLMS::TypeUserDBInstructor, id);
}
@@ -187,14 +186,14 @@ bool InterfaceDataBaseLMS::AuthorizationTrainee(QString login, QString password,
if(! transactionBegin())
return false;
if(int id = selectTraineeID(login, password))
if(int id = selectUserID(DataBaseLMS::TypeUserDBTrainee, login, password))
{
if(isArchivedTrainee(id) || isLoggedInTrainee(id))
{
transactionEnd();
return false;
}
if(updateTraineeLoggedIn(id, true))
if(updateUserLoggedIn(DataBaseLMS::TypeUserDBTrainee, id, true))
return transactionEnd();
}
@@ -207,9 +206,9 @@ bool InterfaceDataBaseLMS::deAuthorizationTrainee(QString login)
if(! transactionBegin())
return false;
if(int id = selectTraineeID(login))
if(int id = selectUserID(DataBaseLMS::TypeUserDBTrainee, login))
{
if(updateTraineeLoggedIn(id, false))
if(updateUserLoggedIn(DataBaseLMS::TypeUserDBTrainee, id, false))
return transactionEnd();
}
@@ -219,7 +218,7 @@ bool InterfaceDataBaseLMS::deAuthorizationTrainee(QString login)
bool InterfaceDataBaseLMS::deAuthorizationAllTrainees()
{
return updateAllTraineesLoggedIn(false);
return updateAllUsersLoggedIn(DataBaseLMS::TypeUserDBTrainee, false);
}
int InterfaceDataBaseLMS::entryTraineeOnSimulator(int id_trainee)
@@ -274,12 +273,12 @@ Trainee InterfaceDataBaseLMS::getTraineeOnComputer(QString computer_name)
QString InterfaceDataBaseLMS::getNameTraineeByLogin(QString login)
{
return selectTraineeNameByLogin(login);
return selectUserNameByLogin(DataBaseLMS::TypeUserDBTrainee, login);
}
int InterfaceDataBaseLMS::getIdTraineeByLogin(QString login)
{
return selectTraineeID(login);
return selectUserID(DataBaseLMS::TypeUserDBTrainee, login);
}
QList<Trainee> InterfaceDataBaseLMS::getListTraineesInGroup(int id)
@@ -325,14 +324,14 @@ int InterfaceDataBaseLMS::editGroup(Group group)
{
if(group.getName() == QStringLiteral("<group>"))
{//Имя не корректно!
QMessageBox::critical(nullptr, tr("Editing error!"),
QMessageBox::critical(ownerWidget, tr("Editing error!"),
tr("Unacceptable group name has been entered.\nThe changes will not be accepted."));
return 0;
}
if(group.getName() == exist_group.getName() && group.getID() != exist_group.getID())
{//Имя уже существует
QMessageBox::critical(nullptr, tr("Editing error!"),
QMessageBox::critical(ownerWidget, tr("Editing error!"),
tr("An existing group name has been entered.\nThe changes will not be accepted."));
return 0;
}
@@ -433,35 +432,35 @@ int InterfaceDataBaseLMS::editTrainee(Trainee trainee)
{
if(trainee.getName() == QStringLiteral("<trainee>"))
{//Имя не корректно!
QMessageBox::critical(nullptr, tr("Editing error!"),
QMessageBox::critical(ownerWidget, tr("Editing error!"),
tr("Unacceptable trainee name has been entered.\nThe changes will not be accepted."));
return 0;
}
if(trainee.getLogin() == QStringLiteral("<login>"))
{//Логин не корректен!
QMessageBox::critical(nullptr, tr("Editing error!"),
QMessageBox::critical(ownerWidget, tr("Editing error!"),
tr("Unacceptable trainee login has been entered.\nThe changes will not be accepted."));
return 0;
}
if(trainee.getPassword() == QStringLiteral("<password>"))
{//Пароль не корректный!
QMessageBox::critical(nullptr, tr("Editing error!"),
QMessageBox::critical(ownerWidget, tr("Editing error!"),
tr("Unacceptable trainee password has been entered.\nThe changes will not be accepted."));
return 0;
}
if(trainee.getName() == exist_trainee.getName() && trainee.getID() != exist_trainee.getID())
{//Имя уже существует
QMessageBox::critical(nullptr, tr("Editing error!"),
QMessageBox::critical(ownerWidget, tr("Editing error!"),
tr("An existing trainee name has been entered."));
return 0;
}
if(trainee.getLogin() == exist_trainee.getLogin() && trainee.getID() != exist_trainee.getID())
{//Логин уже существует!
QMessageBox::critical(nullptr, tr("Editing error!"),
QMessageBox::critical(ownerWidget, tr("Editing error!"),
tr("An existing trainee login has been entered.\nThe changes will not be accepted."));
return 0;
}
@@ -472,10 +471,10 @@ int InterfaceDataBaseLMS::editTrainee(Trainee trainee)
bool InterfaceDataBaseLMS::isArchivedTrainee(int id)
{
return selectTraineeArchived(id);
return selectUserArchived(DataBaseLMS::TypeUserDBTrainee, id);
}
bool InterfaceDataBaseLMS::isLoggedInTrainee(int id)
{
return selectTraineeLoggedIn(id);
return selectUserLoggedIn(DataBaseLMS::TypeUserDBTrainee, id);
}