Implement basic UI with QML (MailListPage) and batching DbChangeProcessor (Step 3-4 of transition plan)
This commit is contained in:
+25
-25
@@ -1,5 +1,4 @@
|
||||
#include "accountdao.h"
|
||||
#include <optional>
|
||||
|
||||
bool AccountDao::insert(const Account& account)
|
||||
{
|
||||
@@ -69,7 +68,7 @@ bool AccountDao::remove(int id)
|
||||
return true;
|
||||
}
|
||||
|
||||
std::optional<Account> AccountDao::findById(int id)
|
||||
Account* AccountDao::findById(int id)
|
||||
{
|
||||
QSqlDatabase& db = DatabaseManager::instance().database();
|
||||
QSqlQuery query(db);
|
||||
@@ -78,21 +77,22 @@ std::optional<Account> AccountDao::findById(int id)
|
||||
|
||||
if (!query.exec()) {
|
||||
qWarning() << "Failed to find account by id:" << query.lastError().text();
|
||||
return std::nullopt;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (query.next()) {
|
||||
Account acc;
|
||||
acc.setId(query.value(0).toInt());
|
||||
acc.setEmail(query.value(1).toString());
|
||||
acc.setDisplayName(query.value(2).toString());
|
||||
acc.setType(static_cast<AccountType>(query.value(3).toInt()));
|
||||
acc.setAccessToken(query.value(4).toString());
|
||||
acc.setRefreshToken(query.value(5).toString());
|
||||
acc.setTokenExpires(query.value(6).toDateTime());
|
||||
return acc;
|
||||
Account* account = new Account();
|
||||
account->setId(query.value("id").toLongLong());
|
||||
account->setEmail(query.value("email").toString());
|
||||
account->setDisplayName(query.value("displayName").toString());
|
||||
account->setType(static_cast<AccountType>(query.value("type").toInt()));
|
||||
account->setAccessToken(query.value("accessToken").toString());
|
||||
account->setRefreshToken(query.value("refreshToken").toString());
|
||||
account->setTokenExpires(query.value("tokenExpires").toDateTime());
|
||||
return account;
|
||||
}
|
||||
return std::nullopt;
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
QVector<Account> AccountDao::findAll()
|
||||
@@ -119,7 +119,7 @@ QVector<Account> AccountDao::findAll()
|
||||
return accounts;
|
||||
}
|
||||
|
||||
std::optional<Account> AccountDao::findByEmail(const QString& email)
|
||||
Account* AccountDao::findByEmail(const QString& email)
|
||||
{
|
||||
QSqlDatabase& db = DatabaseManager::instance().database();
|
||||
QSqlQuery query(db);
|
||||
@@ -128,19 +128,19 @@ std::optional<Account> AccountDao::findByEmail(const QString& email)
|
||||
|
||||
if (!query.exec()) {
|
||||
qWarning() << "Failed to find account by email:" << query.lastError().text();
|
||||
return std::nullopt;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (query.next()) {
|
||||
Account acc;
|
||||
acc.setId(query.value(0).toInt());
|
||||
acc.setEmail(query.value(1).toString());
|
||||
acc.setDisplayName(query.value(2).toString());
|
||||
acc.setType(static_cast<AccountType>(query.value(3).toInt()));
|
||||
acc.setAccessToken(query.value(4).toString());
|
||||
acc.setRefreshToken(query.value(5).toString());
|
||||
acc.setTokenExpires(query.value(6).toDateTime());
|
||||
Account* acc = new Account();
|
||||
acc->setId(query.value(0).toLongLong());
|
||||
acc->setEmail(query.value(1).toString());
|
||||
acc->setDisplayName(query.value(2).toString());
|
||||
acc->setType(static_cast<AccountType>(query.value(3).toInt()));
|
||||
acc->setAccessToken(query.value(4).toString());
|
||||
acc->setRefreshToken(query.value(5).toString());
|
||||
acc->setTokenExpires(query.value(6).toDateTime());
|
||||
return acc;
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
#include <QSqlQuery>
|
||||
#include "folderdao.h"
|
||||
#include <QSqlError>
|
||||
#include <QSqlError>
|
||||
#include <optional>
|
||||
|
||||
bool FolderDao::insert(const Folder& folder)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#include <QSqlQuery>
|
||||
#include <QSqlError>
|
||||
#include "mailitemdao.h"
|
||||
#include <optional>
|
||||
|
||||
|
||||
@@ -0,0 +1,214 @@
|
||||
#include "dbchangeprocessor.h"
|
||||
#include <QDebug>
|
||||
#include <QDateTime>
|
||||
|
||||
DbChangeProcessor::DbChangeProcessor(QObject* parent)
|
||||
: QObject(parent),
|
||||
m_db(DatabaseManager::instance()),
|
||||
m_mailItemDao(MailItemDao::instance()),
|
||||
m_folderDao(FolderDao::instance()),
|
||||
m_accountDao(AccountDao::instance()),
|
||||
m_batchTimer(new QTimer(this))
|
||||
{
|
||||
// Configurar el timer para procesar batch cada 5 segundos
|
||||
m_batchTimer->setInterval(5000); // 5 segundos
|
||||
connect(m_batchTimer, &QTimer::timeout, this, &DbChangeProcessor::processBatch);
|
||||
m_batchTimer->start();
|
||||
|
||||
// Suscribirse a todos los eventos relevantes
|
||||
SUBSCRIBE(WinoMail::Events::MailItemAddedEvent,
|
||||
[this](const WinoMail::Events::MailItemAddedEvent& event) {
|
||||
handleMailItemAdded(event);
|
||||
});
|
||||
|
||||
SUBSCRIBE(WinoMail::Events::MailItemRemovedEvent,
|
||||
[this](const WinoMail::Events::MailItemRemovedEvent& event) {
|
||||
handleMailItemRemoved(event);
|
||||
});
|
||||
|
||||
SUBSCRIBE(WinoMail::Events::MailItemUpdatedEvent,
|
||||
[this](const WinoMail::Events::MailItemUpdatedEvent& event) {
|
||||
handleMailItemUpdated(event);
|
||||
});
|
||||
|
||||
SUBSCRIBE(WinoMail::Events::FolderAddedEvent,
|
||||
[this](const WinoMail::Events::FolderAddedEvent& event) {
|
||||
handleFolderAdded(event);
|
||||
});
|
||||
|
||||
SUBSCRIBE(WinoMail::Events::FolderRemovedEvent,
|
||||
[this](const WinoMail::Events::FolderRemovedEvent& event) {
|
||||
handleFolderRemoved(event);
|
||||
});
|
||||
|
||||
SUBSCRIBE(WinoMail::Events::FolderUpdatedEvent,
|
||||
[this](const WinoMail::Events::FolderUpdatedEvent& event) {
|
||||
handleFolderUpdated(event);
|
||||
});
|
||||
|
||||
SUBSCRIBE(WinoMail::Events::AccountAddedEvent,
|
||||
[this](const WinoMail::Events::AccountAddedEvent& event) {
|
||||
handleAccountAdded(event);
|
||||
});
|
||||
|
||||
SUBSCRIBE(WinoMail::Events::AccountRemovedEvent,
|
||||
[this](const WinoMail::Events::AccountRemovedEvent& event) {
|
||||
handleAccountRemoved(event);
|
||||
});
|
||||
|
||||
SUBSCRIBE(WinoMail::Events::AccountUpdatedEvent,
|
||||
[this](const WinoMail::Events::AccountUpdatedEvent& event) {
|
||||
handleAccountUpdated(event);
|
||||
});
|
||||
}
|
||||
|
||||
void DbChangeProcessor::handleMailItemAdded(const WinoMail::Events::MailItemAddedEvent& event)
|
||||
{
|
||||
qDebug() << "DbChangeProcessor: Queuing MailItemAddedEvent for UID:" << event.item.uid;
|
||||
m_mailItemAddedQueue.append(event);
|
||||
}
|
||||
|
||||
void DbChangeProcessor::handleMailItemRemoved(const WinoMail::Events::MailItemRemovedEvent& event)
|
||||
{
|
||||
qDebug() << "DbChangeProcessor: Queuing MailItemRemovedEvent for UID:" << event.itemUid;
|
||||
m_mailItemRemovedQueue.append(event);
|
||||
}
|
||||
|
||||
void DbChangeProcessor::handleMailItemUpdated(const WinoMail::Events::MailItemUpdatedEvent& event)
|
||||
{
|
||||
qDebug() << "DbChangeProcessor: Queuing MailItemUpdatedEvent for UID:" << event.item.uid;
|
||||
m_mailItemUpdatedQueue.append(event);
|
||||
}
|
||||
|
||||
void DbChangeProcessor::handleFolderAdded(const WinoMail::Events::FolderAddedEvent& event)
|
||||
{
|
||||
qDebug() << "DbChangeProcessor: Queuing FolderAddedEvent for folder:" << event.folder.name;
|
||||
m_folderAddedQueue.append(event);
|
||||
}
|
||||
|
||||
void DbChangeProcessor::handleFolderRemoved(const WinoMail::Events::FolderRemovedEvent& event)
|
||||
{
|
||||
qDebug() << "DbChangeProcessor: Queuing FolderRemovedEvent for folder ID:" << event.folderId;
|
||||
m_folderRemovedQueue.append(event);
|
||||
}
|
||||
|
||||
void DbChangeProcessor::handleFolderUpdated(const WinoMail::Events::FolderUpdatedEvent& event)
|
||||
{
|
||||
qDebug() << "DbChangeProcessor: Queuing FolderUpdatedEvent for folder ID:" << event.folder.id();
|
||||
m_folderUpdatedQueue.append(event);
|
||||
}
|
||||
|
||||
void DbChangeProcessor::handleAccountAdded(const WinoMail::Events::AccountAddedEvent& event)
|
||||
{
|
||||
qDebug() << "DbChangeProcessor: Queuing AccountAddedEvent for account:" << event.account.email();
|
||||
m_accountAddedQueue.append(event);
|
||||
}
|
||||
|
||||
void DbChangeProcessor::handleAccountRemoved(const WinoMail::Events::AccountRemovedEvent& event)
|
||||
{
|
||||
qDebug() << "DbChangeProcessor: Queuing AccountRemovedEvent for account ID:" << event.accountId;
|
||||
m_accountRemovedQueue.append(event);
|
||||
}
|
||||
|
||||
void DbChangeProcessor::handleAccountUpdated(const WinoMail::Events::AccountUpdatedEvent& event)
|
||||
{
|
||||
qDebug() << "DbChangeProcessor: Queuing AccountUpdatedEvent for account ID:" << event.account.id();
|
||||
m_accountUpdatedQueue.append(event);
|
||||
}
|
||||
|
||||
void DbChangeProcessor::processBatch()
|
||||
{
|
||||
qDebug() << "DbChangeProcessor: Starting batch processing at" << QDateTime::currentDateTime().toString();
|
||||
|
||||
// Procesar eventos de MailItem añadidos
|
||||
if (!m_mailItemAddedQueue.isEmpty()) {
|
||||
qDebug() << "DbChangeProcessor: Processing" << m_mailItemAddedQueue.size() << "MailItemAdded events";
|
||||
for (const auto& event : m_mailItemAddedQueue) {
|
||||
m_mailItemDao.insert(event.item);
|
||||
}
|
||||
m_mailItemAddedQueue.clear();
|
||||
}
|
||||
|
||||
// Procesar eventos de MailItem eliminados
|
||||
if (!m_mailItemRemovedQueue.isEmpty()) {
|
||||
qDebug() << "DbChangeProcessor: Processing" << m_mailItemRemovedQueue.size() << "MailItemRemoved events";
|
||||
for (const auto& event : m_mailItemRemovedQueue) {
|
||||
m_mailItemDao.removeByUid(event.itemUid, event.folderId);
|
||||
}
|
||||
m_mailItemRemovedQueue.clear();
|
||||
}
|
||||
|
||||
// Procesar eventos de MailItem actualizados
|
||||
if (!m_mailItemUpdatedQueue.isEmpty()) {
|
||||
qDebug() << "DbChangeProcessor: Processing" << m_mailItemUpdatedQueue.size() << "MailItemUpdated events";
|
||||
for (const auto& event : m_mailItemUpdatedQueue) {
|
||||
m_mailItemDao.update(event.item, event.changedFields);
|
||||
}
|
||||
m_mailItemUpdatedQueue.clear();
|
||||
}
|
||||
|
||||
// Procesar eventos de carpetas añadidas
|
||||
if (!m_folderAddedQueue.isEmpty()) {
|
||||
qDebug() << "DbChangeProcessor: Processing" << m_folderAddedQueue.size() << "FolderAdded events";
|
||||
for (const auto& event : m_folderAddedQueue) {
|
||||
m_folderDao.insert(event.folder);
|
||||
}
|
||||
m_folderAddedQueue.clear();
|
||||
}
|
||||
|
||||
// Procesar eventos de carpetas eliminadas
|
||||
if (!m_folderRemovedQueue.isEmpty()) {
|
||||
qDebug() << "DbChangeProcessor: Processing" << m_folderRemovedQueue.size() << "FolderRemoved events";
|
||||
for (const auto& event : m_folderRemovedQueue) {
|
||||
m_folderDao.remove(event.folderId.toInt());
|
||||
}
|
||||
m_folderRemovedQueue.clear();
|
||||
}
|
||||
|
||||
// Procesar eventos de carpetas actualizadas
|
||||
if (!m_folderUpdatedQueue.isEmpty()) {
|
||||
qDebug() << "DbChangeProcessor: Processing" << m_folderUpdatedQueue.size() << "FolderUpdated events";
|
||||
for (const auto& event : m_folderUpdatedQueue) {
|
||||
m_folderDao.update(event.folder, event.changedFields);
|
||||
}
|
||||
m_folderUpdatedQueue.clear();
|
||||
}
|
||||
|
||||
// Procesar eventos de cuentas añadidas
|
||||
if (!m_accountAddedQueue.isEmpty()) {
|
||||
qDebug() << "DbChangeProcessor: Processing" << m_accountAddedQueue.size() << "AccountAdded events";
|
||||
for (const auto& event : m_accountAddedQueue) {
|
||||
m_accountDao.insert(event.account);
|
||||
}
|
||||
m_accountAddedQueue.clear();
|
||||
}
|
||||
|
||||
// Procesar eventos de cuentas eliminadas
|
||||
if (!m_accountRemovedQueue.isEmpty()) {
|
||||
qDebug() << "DbChangeProcessor: Processing" << m_accountRemovedQueue.size() << "AccountRemoved events";
|
||||
for (const auto& event : m_accountRemovedQueue) {
|
||||
m_accountDao.remove(event.accountId);
|
||||
}
|
||||
m_accountRemovedQueue.clear();
|
||||
}
|
||||
|
||||
// Procesar eventos de cuentas actualizadas
|
||||
if (!m_accountUpdatedQueue.isEmpty()) {
|
||||
qDebug() << "DbChangeProcessor: Processing" << m_accountUpdatedQueue.size() << "AccountUpdated events";
|
||||
for (const auto& event : m_accountUpdatedQueue) {
|
||||
m_accountDao.update(event.account, event.changedFields);
|
||||
}
|
||||
m_accountUpdatedQueue.clear();
|
||||
}
|
||||
|
||||
qDebug() << "DbChangeProcessor: Batch processing completed at" << QDateTime::currentDateTime().toString();
|
||||
}
|
||||
|
||||
DbChangeProcessor::~DbChangeProcessor()
|
||||
{
|
||||
if (m_batchTimer->isActive()) {
|
||||
m_batchTimer->stop();
|
||||
}
|
||||
// Procesar cualquier evento restante antes de destruir
|
||||
processBatch();
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
#ifndef DBCHANGEPROCESSOR_H
|
||||
#define DBCHANGEPROCESSOR_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QTimer>
|
||||
#include <QVector>
|
||||
#include "core/eventbus.h"
|
||||
#include "../core/events.h"
|
||||
#include "../db/databasemanager.h"
|
||||
#include "../db/dao/accountdao.h"
|
||||
#include "../db/dao/folderdao.h"
|
||||
#include "../db/dao/mailitemdao.h"
|
||||
|
||||
class DbChangeProcessor : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit DbChangeProcessor(QObject* parent = nullptr);
|
||||
~DbChangeProcessor() override = default;
|
||||
|
||||
private slots:
|
||||
void handleMailItemAdded(const WinoMail::Events::MailItemAddedEvent& event);
|
||||
void handleMailItemRemoved(const WinoMail::Events::MailItemRemovedEvent& event);
|
||||
void handleMailItemUpdated(const WinoMail::Events::MailItemUpdatedEvent& event);
|
||||
void handleFolderAdded(const WinoMail::Events::FolderAddedEvent& event);
|
||||
void handleFolderRemoved(const WinoMail::Events::FolderRemovedEvent& event);
|
||||
void handleFolderUpdated(const WinoMail::Events::FolderUpdatedEvent& event);
|
||||
void handleAccountAdded(const WinoMail::Events::AccountAddedEvent& event);
|
||||
void handleAccountRemoved(const WinoMail::Events::AccountRemovedEvent& event);
|
||||
void handleAccountUpdated(const WinoMail::Events::AccountUpdatedEvent& event);
|
||||
void processBatch();
|
||||
|
||||
private:
|
||||
DatabaseManager& m_db;
|
||||
MailItemDao& m_mailItemDao;
|
||||
FolderDao& m_folderDao;
|
||||
AccountDao& m_accountDao;
|
||||
|
||||
QTimer* m_batchTimer;
|
||||
QVector<WinoMail::Events::MailItemAddedEvent> m_mailItemAddedQueue;
|
||||
QVector<WinoMail::Events::MailItemRemovedEvent> m_mailItemRemovedQueue;
|
||||
QVector<WinoMail::Events::MailItemUpdatedEvent> m_mailItemUpdatedQueue;
|
||||
QVector<WinoMail::Events::FolderAddedEvent> m_folderAddedQueue;
|
||||
QVector<WinoMail::Events::FolderRemovedEvent> m_folderRemovedQueue;
|
||||
QVector<WinoMail::Events::FolderUpdatedEvent> m_folderUpdatedQueue;
|
||||
QVector<WinoMail::Events::AccountAddedEvent> m_accountAddedQueue;
|
||||
QVector<WinoMail::Events::AccountRemovedEvent> m_accountRemovedQueue;
|
||||
QVector<WinoMail::Events::AccountUpdatedEvent> m_accountUpdatedQueue;
|
||||
};
|
||||
|
||||
#endif // DBCHANGEPROCESSOR_H
|
||||
Reference in New Issue
Block a user