73 lines
1.6 KiB
C++
73 lines
1.6 KiB
C++
#include "actionmanager.h"
|
||
|
||
ActionManager::ActionManager(uDataBase *db, QObject *parent)
|
||
: QObject(parent)
|
||
, m_db(db)
|
||
{
|
||
}
|
||
|
||
bool ActionManager::addAction(const ActionData &action)
|
||
{
|
||
if (!m_db) return false;
|
||
if (!m_db->saveAction(action)) return false;
|
||
|
||
// Перезагрузим все действия, чтобы получить свежий ID
|
||
loadFromDatabase();
|
||
emit dataChanged();
|
||
return true;
|
||
}
|
||
|
||
bool ActionManager::updateAction(int id, const ActionData &action)
|
||
{
|
||
if (!m_db) return false;
|
||
if (!m_db->updateAction(id, action)) return false;
|
||
|
||
for (int i = 0; i < m_actions.size(); ++i) {
|
||
if (m_actions[i].id == id) {
|
||
m_actions[i] = action;
|
||
m_actions[i].id = id;
|
||
break;
|
||
}
|
||
}
|
||
emit dataChanged();
|
||
return true;
|
||
}
|
||
|
||
bool ActionManager::deleteAction(int id)
|
||
{
|
||
if (!m_db) return false;
|
||
if (!m_db->deleteAction(id)) return false;
|
||
// Удаляем все связи с этим действием
|
||
m_db->deleteLinksByActionId(id);
|
||
|
||
for (int i = 0; i < m_actions.size(); ++i) {
|
||
if (m_actions[i].id == id) {
|
||
m_actions.removeAt(i);
|
||
break;
|
||
}
|
||
}
|
||
emit dataChanged();
|
||
return true;
|
||
}
|
||
|
||
QList<ActionData> ActionManager::getAllActions() const
|
||
{
|
||
return m_actions;
|
||
}
|
||
|
||
bool ActionManager::loadFromDatabase()
|
||
{
|
||
if (!m_db) return false;
|
||
m_actions = m_db->loadAllActions();
|
||
emit dataChanged();
|
||
return true;
|
||
}
|
||
|
||
ActionData ActionManager::getAction(int id) const
|
||
{
|
||
for (const ActionData &a : m_actions) {
|
||
if (a.id == id) return a;
|
||
}
|
||
return ActionData(); // с id = -1
|
||
}
|