Basic JSON RESTful client fetching items from a local server at application start and adding them to the model.

This commit is contained in:
2026-01-25 10:47:19 +01:00
parent e1bc779791
commit e29cd0aebf
8 changed files with 178 additions and 3 deletions

View File

@ -16,6 +16,7 @@
#include "model/generalsortfiltermodel.h"
#include "model/metadata.h"
#include "model/tablemodel.h"
#include "network/servercommunicator.h"
#include <QtGui/QUndoStack>
@ -37,6 +38,7 @@ GenericCore::GenericCore() {
m_modelUndoStack = new QUndoStack(this);
setupModels();
setupServerConfiguration();
}
GenericCore::~GenericCore() { qDebug() << "Destroying core..."; }
@ -132,6 +134,32 @@ bool GenericCore::exportCSVFile(const QString& filePath) {
return FileHandler::exportToCSVFile(itemsAsStringLists, filePath);
}
bool GenericCore::isSyncServerSetup() const {
if (m_serverCommunicator) {
return true;
} else {
return false;
}
}
void GenericCore::onItemsFetched(const QByteArray jsonDoc) {
emit displayStatusMessage("New items fetched.");
// TODO ? check compability of JSON structure beforehand?
m_mainModel->appendItems(jsonDoc);
}
void GenericCore::onItemsFetchFailure(const QString errorString) {
emit displayStatusMessage(QString("Error: %1").arg(errorString));
}
void GenericCore::onPostRequestSuccessful(const QString message) {
emit displayStatusMessage(message);
}
void GenericCore::onPostRequestFailure(const QString errorString) {
emit displayStatusMessage(QString("Error: %1").arg(errorString));
}
void GenericCore::setupModels() {
m_mainModel = make_shared<TableModel>(m_modelUndoStack, this);
m_sortFilterModel = make_shared<GeneralSortFilterModel>(m_mainModel);
@ -188,3 +216,19 @@ QString GenericCore::getMaintenanceToolFilePath() const {
const QString filePath = applicationDirPath + "/" + UPDATER_EXE;
return filePath;
}
void GenericCore::setupServerConfiguration() {
m_serverCommunicator = make_unique<ServerCommunicator>(this);
connect(m_serverCommunicator.get(), &ServerCommunicator::itemsFetched, this,
&GenericCore::onItemsFetched);
connect(m_serverCommunicator.get(), &ServerCommunicator::itemsFetchFailure, this,
&GenericCore::onItemsFetchFailure);
connect(m_serverCommunicator.get(), &ServerCommunicator::postRequestSuccessful, this,
&GenericCore::onPostRequestSuccessful);
connect(m_serverCommunicator.get(), &ServerCommunicator::postRequestFailure, this,
&GenericCore::onPostRequestFailure);
/// fetching items on startup to test the communication with the server
m_serverCommunicator->fetchItems();
}