Initial commit
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
#include "application_controller.h"
|
||||
#include "config.h"
|
||||
#include "database_manager.h"
|
||||
|
||||
// Translations
|
||||
#include <QCoreApplication>
|
||||
#include <QLocale>
|
||||
#include <QTranslator>
|
||||
|
||||
// Generation of records
|
||||
#include "refund_generator.h"
|
||||
#include "session_generator.h"
|
||||
#include "ticket_generator.h"
|
||||
|
||||
// Auntification
|
||||
#include "auth_dialog.h"
|
||||
|
||||
qint32 ApplicationController::userId_ = -1;
|
||||
qint32 ApplicationController::accessLevel_ = 0;
|
||||
|
||||
bool ApplicationController::setupApplication() {
|
||||
|
||||
if (!ensureDatabaseExists(DATABASE_NAME)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!connectToDatabase(DATABASE_NAME)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!loadTranslations("ru", "app", "_", ":/translations")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
AuthDialog authDialog;
|
||||
|
||||
QObject::connect(&authDialog, &AuthDialog::loginSuccessful,
|
||||
&ApplicationController::onLoginSuccessful);
|
||||
|
||||
QObject::connect(&authDialog, &AuthDialog::loginFailed,
|
||||
&ApplicationController::onLoginFailed);
|
||||
|
||||
if (authDialog.exec() != QDialog::Accepted) {
|
||||
return false; // User has not logged in
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ApplicationController::ensureDatabaseExists(
|
||||
const QString &databaseFileName) {
|
||||
DatabaseManager::CreateDatabaseResult result =
|
||||
DatabaseManager::createDefaultDatabaseIfMissing(databaseFileName);
|
||||
|
||||
switch (result) {
|
||||
case DatabaseManager::CreateDatabaseResult::Success: {
|
||||
|
||||
#ifndef NDEBUG
|
||||
qInfo() << "Generating test data...";
|
||||
|
||||
DayManager dayManager(QTime(8, 0), QTime(17, 0));
|
||||
dayManager.setMode(DayManager::SlotSelectionMode::Quick);
|
||||
dayManager.setMode(DayManager::StartTimeMode::Random);
|
||||
|
||||
const QDate begin = QDate(2025, 1, 1); // Since 01-01-2025
|
||||
const QDate end = begin.addDays(5);
|
||||
ReservationManager reservationManager(14, begin.daysTo(end),
|
||||
std::move(dayManager));
|
||||
|
||||
SessionGenerator sessionGenerator(reservationManager);
|
||||
|
||||
sessionGenerator.generateAll(
|
||||
sessionGenerator.defaultShouldLog(1 /* percent */, 60 /* seconds */));
|
||||
|
||||
QPair<QTime, QTime> timeRange(QTime(9, 30), QTime(15, 30));
|
||||
|
||||
TicketGenerator ticketsGenerator(
|
||||
{0, 30} /* day range */, timeRange,
|
||||
{0.0, 1.0} /* fill ratio per session range*/);
|
||||
GeneratorInterface::ShouldLogSuccess performOtherwise =
|
||||
ticketsGenerator.shouldLogFirstAndLastPercent();
|
||||
ticketsGenerator.generateByFillRatio(
|
||||
0.35,
|
||||
ticketsGenerator.defaultShouldLog(1 /* percent */, 600 /* seconds */));
|
||||
|
||||
RefundGenerator refundGenerator({0.1, 0.5} /* refund ratio range */,
|
||||
{0, 7} /* day range */, timeRange);
|
||||
refundGenerator.generateByFillRatio(
|
||||
0.1,
|
||||
refundGenerator.defaultShouldLog(1 /* percent */, 600 /* seconds
|
||||
*/));
|
||||
|
||||
qInfo() << "Test data generated successfully";
|
||||
#endif
|
||||
|
||||
qInfo() << "Database created and initialized successfully";
|
||||
} break;
|
||||
case DatabaseManager::CreateDatabaseResult::AlreadyExists:
|
||||
break;
|
||||
case DatabaseManager::CreateDatabaseResult::Error:
|
||||
qCritical() << "Failed to create and connect to the database";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ApplicationController::onLoginFailed() {
|
||||
userId_ = -1;
|
||||
accessLevel_ = 0;
|
||||
}
|
||||
|
||||
void ApplicationController::onLoginSuccessful(qint32 userId,
|
||||
qint32 accessLevel) {
|
||||
userId_ = userId;
|
||||
accessLevel_ = accessLevel;
|
||||
}
|
||||
|
||||
bool ApplicationController::connectToDatabase(const QString &databaseName) {
|
||||
if (!DatabaseManager::connectToDefaultDatabase(databaseName)) {
|
||||
qCritical() << "Failed to connect to the database";
|
||||
return false;
|
||||
}
|
||||
|
||||
qInfo() << "Connected to database" << databaseName << "successfully";
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ApplicationController::loadTranslations(const QString &locale,
|
||||
const QString &filename,
|
||||
const QString &prefix,
|
||||
const QString &directory) {
|
||||
QCoreApplication *app = QCoreApplication::instance();
|
||||
QTranslator *translator = new QTranslator(app);
|
||||
|
||||
bool loaded = translator->load(QLocale(locale), filename, prefix, directory);
|
||||
if (!loaded) {
|
||||
qWarning() << "Failed to load translations";
|
||||
return false;
|
||||
}
|
||||
|
||||
bool installed = app->installTranslator(translator);
|
||||
if (!installed) {
|
||||
qWarning() << "Failed to install translations";
|
||||
return false;
|
||||
}
|
||||
|
||||
qInfo() << "Translations are loaded successfully";
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user