refactor
This commit is contained in:
+222
@@ -1179,3 +1179,225 @@ bool uDataBase::updateChat(const QString &name, HttpServerChat *server,
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool uDataBase::createNotificationsTable()
|
||||
{
|
||||
QSqlQuery query(m_db);
|
||||
QString sql =
|
||||
"CREATE TABLE IF NOT EXISTS notifications ("
|
||||
" id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
||||
" name TEXT NOT NULL,"
|
||||
" type TEXT NOT NULL," // "notification"
|
||||
" port INTEGER NOT NULL,"
|
||||
" block_color TEXT,"
|
||||
" border_color TEXT,"
|
||||
" border_size INTEGER,"
|
||||
" transparency INTEGER,"
|
||||
" page_background_color TEXT,"
|
||||
" title_family TEXT,"
|
||||
" title_size INTEGER,"
|
||||
" title_color TEXT,"
|
||||
" content_family TEXT,"
|
||||
" content_size INTEGER,"
|
||||
" content_color TEXT,"
|
||||
" duration INTEGER,"
|
||||
" created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP"
|
||||
")";
|
||||
|
||||
if (!query.exec(sql)) {
|
||||
m_lastError = query.lastError().text();
|
||||
qWarning() << "Failed to create notifications table:" << m_lastError;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Создаем индекс для быстрого поиска по порту
|
||||
sql = "CREATE INDEX IF NOT EXISTS idx_notifications_port ON notifications (port)";
|
||||
query.exec(sql);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool uDataBase::saveNotification(const QString &name, HttpServer *server)
|
||||
{
|
||||
if (!server) {
|
||||
m_lastError = "Server is null";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!m_db.isOpen()) {
|
||||
m_lastError = "Database is not open";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Создаем таблицу если не существует
|
||||
if (!tableExists("notifications")) {
|
||||
if (!createNotificationsTable()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
QSqlQuery query(m_db);
|
||||
query.prepare(
|
||||
"INSERT INTO notifications ("
|
||||
" name, type, port, block_color, border_color, border_size,"
|
||||
" transparency, page_background_color, title_family, title_size,"
|
||||
" title_color, content_family, content_size, content_color, duration"
|
||||
") VALUES ("
|
||||
" :name, :type, :port, :block_color, :border_color, :border_size,"
|
||||
" :transparency, :page_background_color, :title_family, :title_size,"
|
||||
" :title_color, :content_family, :content_size, :content_color, :duration"
|
||||
")"
|
||||
);
|
||||
|
||||
// Используем реальные значения из сервера
|
||||
query.bindValue(":name", name);
|
||||
query.bindValue(":type", "notification");
|
||||
query.bindValue(":port", server->port());
|
||||
query.bindValue(":block_color", server->getBlockColor());
|
||||
query.bindValue(":border_color", server->getBorderColor());
|
||||
query.bindValue(":border_size", server->getBorderSize());
|
||||
query.bindValue(":transparency", server->getTransparency());
|
||||
query.bindValue(":page_background_color", server->getPageBackgroundColor());
|
||||
query.bindValue(":title_family", server->getTitleFamily());
|
||||
query.bindValue(":title_size", server->getTitleSize());
|
||||
query.bindValue(":title_color", server->getTitleColor());
|
||||
query.bindValue(":content_family", server->getContentFamily());
|
||||
query.bindValue(":content_size", server->getContentSize());
|
||||
query.bindValue(":content_color", server->getContentColor());
|
||||
query.bindValue(":duration", server->getDuration());
|
||||
|
||||
if (!query.exec()) {
|
||||
m_lastError = query.lastError().text();
|
||||
qWarning() << "Failed to save notification:" << m_lastError;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool uDataBase::updateNotification(const QString &name, HttpServer *server, int oldPort)
|
||||
{
|
||||
if (!server) {
|
||||
m_lastError = "Server is null";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!m_db.isOpen()) {
|
||||
m_lastError = "Database is not open";
|
||||
return false;
|
||||
}
|
||||
|
||||
QSqlQuery query(m_db);
|
||||
query.prepare(
|
||||
"UPDATE notifications SET "
|
||||
" name = :name, "
|
||||
" port = :port, "
|
||||
" block_color = :block_color, "
|
||||
" border_color = :border_color, "
|
||||
" border_size = :border_size, "
|
||||
" transparency = :transparency, "
|
||||
" page_background_color = :page_background_color, "
|
||||
" title_family = :title_family, "
|
||||
" title_size = :title_size, "
|
||||
" title_color = :title_color, "
|
||||
" content_family = :content_family, "
|
||||
" content_size = :content_size, "
|
||||
" content_color = :content_color, "
|
||||
" duration = :duration "
|
||||
"WHERE port = :old_port"
|
||||
);
|
||||
|
||||
query.bindValue(":name", name);
|
||||
query.bindValue(":port", server->port());
|
||||
query.bindValue(":block_color", server->getBlockColor());
|
||||
query.bindValue(":border_color", server->getBorderColor());
|
||||
query.bindValue(":border_size", server->getBorderSize());
|
||||
query.bindValue(":transparency", server->getTransparency());
|
||||
query.bindValue(":page_background_color", server->getPageBackgroundColor());
|
||||
query.bindValue(":title_family", server->getTitleFamily());
|
||||
query.bindValue(":title_size", server->getTitleSize());
|
||||
query.bindValue(":title_color", server->getTitleColor());
|
||||
query.bindValue(":content_family", server->getContentFamily());
|
||||
query.bindValue(":content_size", server->getContentSize());
|
||||
query.bindValue(":content_color", server->getContentColor());
|
||||
query.bindValue(":duration", server->getDuration());
|
||||
query.bindValue(":old_port", oldPort);
|
||||
|
||||
if (!query.exec()) {
|
||||
m_lastError = query.lastError().text();
|
||||
qWarning() << "Failed to update notification:" << m_lastError;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
QList<NotificationSettings> uDataBase::loadAllNotifications()
|
||||
{
|
||||
QList<NotificationSettings> notifications;
|
||||
|
||||
if (!m_db.isOpen()) {
|
||||
m_lastError = "Database is not open";
|
||||
return notifications;
|
||||
}
|
||||
|
||||
if (!tableExists("notifications")) {
|
||||
// Если таблицы нет, ничего не загружаем
|
||||
return notifications;
|
||||
}
|
||||
|
||||
QSqlQuery query(m_db);
|
||||
QString sql = "SELECT * FROM notifications ORDER BY created_at";
|
||||
|
||||
if (!query.exec(sql)) {
|
||||
m_lastError = query.lastError().text();
|
||||
qWarning() << "Failed to load notifications:" << m_lastError;
|
||||
return notifications;
|
||||
}
|
||||
|
||||
while (query.next()) {
|
||||
NotificationSettings settings;
|
||||
settings.id = query.value("id").toInt();
|
||||
settings.name = query.value("name").toString();
|
||||
settings.type = query.value("type").toString();
|
||||
settings.port = query.value("port").toInt();
|
||||
settings.blockColor = query.value("block_color").toString();
|
||||
settings.borderColor = query.value("border_color").toString();
|
||||
settings.borderSize = query.value("border_size").toInt();
|
||||
settings.transparency = query.value("transparency").toInt();
|
||||
settings.pageBackgroundColor = query.value("page_background_color").toString();
|
||||
settings.titleFamily = query.value("title_family").toString();
|
||||
settings.titleSize = query.value("title_size").toInt();
|
||||
settings.titleColor = query.value("title_color").toString();
|
||||
settings.contentFamily = query.value("content_family").toString();
|
||||
settings.contentSize = query.value("content_size").toInt();
|
||||
settings.contentColor = query.value("content_color").toString();
|
||||
settings.duration = query.value("duration").toInt();
|
||||
settings.createdAt = query.value("created_at").toDateTime();
|
||||
|
||||
notifications.append(settings);
|
||||
}
|
||||
|
||||
return notifications;
|
||||
}
|
||||
|
||||
bool uDataBase::deleteNotification(int port)
|
||||
{
|
||||
if (!m_db.isOpen()) {
|
||||
m_lastError = "Database is not open";
|
||||
return false;
|
||||
}
|
||||
|
||||
QSqlQuery query(m_db);
|
||||
query.prepare("DELETE FROM notifications WHERE port = :port");
|
||||
query.bindValue(":port", port);
|
||||
|
||||
if (!query.exec()) {
|
||||
m_lastError = query.lastError().text();
|
||||
qWarning() << "Failed to delete notification:" << m_lastError;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user