Compare commits
3 Commits
b76523ec75
...
ceaf6b5fbd
| Author | SHA1 | Date | |
|---|---|---|---|
| ceaf6b5fbd | |||
| 760c778c3b | |||
| b1f7fbff8b |
@ -39,7 +39,7 @@ GenericCore::GenericCore() {
|
|||||||
m_modelUndoStack = new QUndoStack(this);
|
m_modelUndoStack = new QUndoStack(this);
|
||||||
|
|
||||||
setupModels();
|
setupModels();
|
||||||
setupServerConfiguration();
|
setupServerCommunication();
|
||||||
}
|
}
|
||||||
|
|
||||||
GenericCore::~GenericCore() { qDebug() << "Destroying core..."; }
|
GenericCore::~GenericCore() { qDebug() << "Destroying core..."; }
|
||||||
@ -129,7 +129,7 @@ void GenericCore::applySettings(QVariantMap settingMap, QString group) {
|
|||||||
SettingsHandler::saveSettings(settingMap, group);
|
SettingsHandler::saveSettings(settingMap, group);
|
||||||
|
|
||||||
if (group == "Server") {
|
if (group == "Server") {
|
||||||
setupServerConfiguration();
|
applyServerConfiguration();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,8 +157,9 @@ void GenericCore::saveItems() {
|
|||||||
emit displayStatusMessage(QString("Error: Items couldn't be saved."));
|
emit displayStatusMessage(QString("Error: Items couldn't be saved."));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GenericCore::onSendItemTriggered(const QByteArray& jsonData) {
|
void GenericCore::onSendItemTriggered(const QByteArray& jsonData) {
|
||||||
m_serverCommunicator->postItems(jsonData);
|
m_serverCommunicator->sendItem(jsonData);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GenericCore::onItemsFetched(const QByteArray jsonData) {
|
void GenericCore::onItemsFetched(const QByteArray jsonData) {
|
||||||
@ -172,20 +173,20 @@ void GenericCore::onItemsFetchFailure(const QString errorString) {
|
|||||||
emit displayStatusMessage(QString("Error: %1").arg(errorString));
|
emit displayStatusMessage(QString("Error: %1").arg(errorString));
|
||||||
}
|
}
|
||||||
|
|
||||||
void GenericCore::onPostRequestSuccessful(const QByteArray responseData) {
|
void GenericCore::onSendItemSuccessful(const QByteArray responseData) {
|
||||||
const QString message = m_mainModel->updateItemsFromJson(responseData);
|
const QString message = m_mainModel->updateItemsFromJson(responseData);
|
||||||
emit displayStatusMessage(message);
|
emit displayStatusMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GenericCore::onPostRequestFailure(const QString errorString) {
|
void GenericCore::onSendItemFailure(const QString errorString) {
|
||||||
emit displayStatusMessage(QString("Error: %1").arg(errorString));
|
emit displayStatusMessage(QString("Error: %1").arg(errorString));
|
||||||
}
|
}
|
||||||
|
|
||||||
void GenericCore::onDeleteRequestSuccessful(const QByteArray responseData) {
|
void GenericCore::onDeleteItemSuccessful(const QByteArray responseData) {
|
||||||
qWarning() << "TODO: Process success response!!!";
|
qWarning() << "TODO: Process success response!!!";
|
||||||
}
|
}
|
||||||
|
|
||||||
void GenericCore::onDeleteRequestFailure(const QString errorString) {
|
void GenericCore::onDeleteItemFailure(const QString errorString) {
|
||||||
qWarning() << "TODO: Process error response!!!";
|
qWarning() << "TODO: Process error response!!!";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -246,12 +247,13 @@ QString GenericCore::getMaintenanceToolFilePath() const {
|
|||||||
return filePath;
|
return filePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GenericCore::setupServerConfiguration() {
|
void GenericCore::setupServerCommunication() {
|
||||||
m_serverCommunicator = make_unique<ServerCommunicator>(this);
|
m_serverCommunicator = make_unique<ServerCommunicator>(this);
|
||||||
/// request connections
|
/// request connections
|
||||||
connect(this, &GenericCore::fetchItemsFromServer, m_serverCommunicator.get(),
|
connect(this, &GenericCore::fetchItemsFromServer, m_serverCommunicator.get(),
|
||||||
&ServerCommunicator::fetchItems);
|
&ServerCommunicator::fetchItems);
|
||||||
connect(this, &GenericCore::postItemToServer, this, &GenericCore::onSendItemTriggered);
|
connect(this, &GenericCore::sendItemToServer, m_serverCommunicator.get(),
|
||||||
|
&ServerCommunicator::sendItem);
|
||||||
connect(this, &GenericCore::deleteItemFromServer, m_serverCommunicator.get(),
|
connect(this, &GenericCore::deleteItemFromServer, m_serverCommunicator.get(),
|
||||||
&ServerCommunicator::deleteItem);
|
&ServerCommunicator::deleteItem);
|
||||||
|
|
||||||
@ -260,14 +262,14 @@ void GenericCore::setupServerConfiguration() {
|
|||||||
&GenericCore::onItemsFetched);
|
&GenericCore::onItemsFetched);
|
||||||
connect(m_serverCommunicator.get(), &ServerCommunicator::itemsFetchFailure, this,
|
connect(m_serverCommunicator.get(), &ServerCommunicator::itemsFetchFailure, this,
|
||||||
&GenericCore::onItemsFetchFailure);
|
&GenericCore::onItemsFetchFailure);
|
||||||
connect(m_serverCommunicator.get(), &ServerCommunicator::postRequestSuccessful, this,
|
connect(m_serverCommunicator.get(), &ServerCommunicator::sendItemSuccessful, this,
|
||||||
&GenericCore::onPostRequestSuccessful);
|
&GenericCore::onSendItemSuccessful);
|
||||||
connect(m_serverCommunicator.get(), &ServerCommunicator::postRequestFailure, this,
|
connect(m_serverCommunicator.get(), &ServerCommunicator::sendItemFailure, this,
|
||||||
&GenericCore::onPostRequestFailure);
|
&GenericCore::onSendItemFailure);
|
||||||
connect(m_serverCommunicator.get(), &ServerCommunicator::deleteRequestSuccessful, this,
|
connect(m_serverCommunicator.get(), &ServerCommunicator::deleteItemSuccessful, this,
|
||||||
&GenericCore::onDeleteRequestSuccessful);
|
&GenericCore::onDeleteItemSuccessful);
|
||||||
connect(m_serverCommunicator.get(), &ServerCommunicator::deleteRequestFailure, this,
|
connect(m_serverCommunicator.get(), &ServerCommunicator::deleteItemFailure, this,
|
||||||
&GenericCore::onDeleteRequestFailure);
|
&GenericCore::onDeleteItemFailure);
|
||||||
|
|
||||||
applyServerConfiguration();
|
applyServerConfiguration();
|
||||||
}
|
}
|
||||||
@ -275,6 +277,7 @@ void GenericCore::setupServerConfiguration() {
|
|||||||
void GenericCore::applyServerConfiguration() {
|
void GenericCore::applyServerConfiguration() {
|
||||||
const QVariantMap serverSettings = SettingsHandler::getSettings("Server");
|
const QVariantMap serverSettings = SettingsHandler::getSettings("Server");
|
||||||
const QString urlValue = serverSettings.value("url").toString();
|
const QString urlValue = serverSettings.value("url").toString();
|
||||||
|
// NEXT if urlValue is empty -> remove authToken from settings?
|
||||||
if (!urlValue.isEmpty()) {
|
if (!urlValue.isEmpty()) {
|
||||||
const QString emailValue = serverSettings.value("email").toString();
|
const QString emailValue = serverSettings.value("email").toString();
|
||||||
const QString passwordValue = serverSettings.value("password").toString();
|
const QString passwordValue = serverSettings.value("password").toString();
|
||||||
|
|||||||
@ -42,15 +42,15 @@ class GenericCore : public QObject {
|
|||||||
void onSendItemTriggered(const QByteArray& jsonData);
|
void onSendItemTriggered(const QByteArray& jsonData);
|
||||||
void onItemsFetched(const QByteArray jsonData);
|
void onItemsFetched(const QByteArray jsonData);
|
||||||
void onItemsFetchFailure(const QString errorString);
|
void onItemsFetchFailure(const QString errorString);
|
||||||
void onPostRequestSuccessful(const QByteArray responseData);
|
void onSendItemSuccessful(const QByteArray responseData);
|
||||||
void onPostRequestFailure(const QString errorString);
|
void onSendItemFailure(const QString errorString);
|
||||||
void onDeleteRequestSuccessful(const QByteArray responseData);
|
void onDeleteItemSuccessful(const QByteArray responseData);
|
||||||
void onDeleteRequestFailure(const QString errorString);
|
void onDeleteItemFailure(const QString errorString);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void displayStatusMessage(QString message);
|
void displayStatusMessage(QString message);
|
||||||
void fetchItemsFromServer();
|
void fetchItemsFromServer();
|
||||||
void postItemToServer(const QByteArray& jsonData);
|
void sendItemToServer(const QByteArray& jsonData);
|
||||||
void deleteItemFromServer(const QString& id);
|
void deleteItemFromServer(const QString& id);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -67,7 +67,7 @@ class GenericCore : public QObject {
|
|||||||
|
|
||||||
/// Network communication
|
/// Network communication
|
||||||
std::unique_ptr<ServerCommunicator> m_serverCommunicator;
|
std::unique_ptr<ServerCommunicator> m_serverCommunicator;
|
||||||
void setupServerConfiguration();
|
void setupServerCommunication();
|
||||||
void applyServerConfiguration();
|
void applyServerConfiguration();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -27,6 +27,7 @@ QString GeneralSortFilterModel::getUuid(const QModelIndex& itemIndex) const {
|
|||||||
return data(itemIndex, IdRole).toString();
|
return data(itemIndex, IdRole).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NEXT remove when deprecated
|
||||||
QByteArray GeneralSortFilterModel::jsonDataForServer(const QModelIndex& proxyIndex) {
|
QByteArray GeneralSortFilterModel::jsonDataForServer(const QModelIndex& proxyIndex) {
|
||||||
const QModelIndex sourceIndex = mapToSource(proxyIndex);
|
const QModelIndex sourceIndex = mapToSource(proxyIndex);
|
||||||
return m_tableModel->jsonDataForServer(sourceIndex);
|
return m_tableModel->jsonDataForServer(sourceIndex);
|
||||||
|
|||||||
@ -51,62 +51,133 @@ void ServerCommunicator::setServerConfiguration(const QString url,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerCommunicator::fetchItems() {
|
void ServerCommunicator::fetchItems() { sendGetRequest(ROUTE_ITEMS); }
|
||||||
/// Set up a GET request
|
|
||||||
m_restManager->get(m_serviceApi->createRequest(ROUTE_ITEMS), this, [this](QRestReply& reply) {
|
|
||||||
if (reply.isSuccess()) {
|
|
||||||
qInfo() << "Fetching items successful.";
|
|
||||||
const QJsonDocument doc = reply.readJson().value();
|
|
||||||
emit itemsFetched(doc.toJson());
|
|
||||||
|
|
||||||
} else {
|
void ServerCommunicator::sendItem(const QByteArray& jsonData) {
|
||||||
if (reply.hasError()) {
|
sendPostRequest(ROUTE_ITEMS, jsonData);
|
||||||
const QString errorString = reply.errorString();
|
|
||||||
qCritical() << "ERROR:" << errorString;
|
|
||||||
emit itemsFetchFailure(errorString);
|
|
||||||
} else {
|
|
||||||
int statusCode = reply.httpStatus();
|
|
||||||
qCritical() << "ERROR:" << statusCode;
|
|
||||||
emit itemsFetchFailure(QString::number(statusCode));
|
|
||||||
emit itemsFetchFailure(QString("HTTP status code: %1").arg(statusCode));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
void ServerCommunicator::postItems(const QByteArray& jsonData) {
|
|
||||||
QNetworkReply* reply = m_restManager->post(m_serviceApi->createRequest(ROUTE_ITEMS), jsonData);
|
|
||||||
|
|
||||||
QObject::connect(reply, &QNetworkReply::finished, [=]() {
|
|
||||||
if (reply->error() == QNetworkReply::NoError) {
|
|
||||||
QByteArray responseData = reply->readAll();
|
|
||||||
const QString message = QString("POST successful! Response: %1").arg(responseData);
|
|
||||||
qInfo() << message;
|
|
||||||
emit postRequestSuccessful(responseData);
|
|
||||||
} else {
|
|
||||||
const QString message = QString("Error: %1").arg(reply->errorString());
|
|
||||||
qDebug() << message;
|
|
||||||
emit postRequestFailure(message);
|
|
||||||
}
|
|
||||||
reply->deleteLater();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerCommunicator::deleteItem(const QString& id) {
|
void ServerCommunicator::deleteItem(const QString& id) {
|
||||||
const QString deleteRoute = QString("%1/%2").arg(ROUTE_ITEMS, id);
|
const QString path = QString("%1/%2").arg(ROUTE_ITEMS, id);
|
||||||
QNetworkReply* reply = m_restManager->deleteResource(m_serviceApi->createRequest(deleteRoute));
|
sendDeleteRequest(path);
|
||||||
|
}
|
||||||
QObject::connect(reply, &QNetworkReply::finished, [=]() {
|
|
||||||
if (reply->error() == QNetworkReply::NoError) {
|
void ServerCommunicator::sendGetRequest(const QString& path) {
|
||||||
QByteArray responseData = reply->readAll();
|
// TODO check for valid path, instead of emptiness
|
||||||
const QString message = QString("DELETE successful! Response: %1").arg(responseData);
|
if (path.isEmpty()) {
|
||||||
qInfo() << message;
|
qDebug() << "Empty path -> Not sending a request.";
|
||||||
emit deleteRequestSuccessful(responseData);
|
return;
|
||||||
} else {
|
}
|
||||||
const QString message = QString("Error: %1").arg(reply->errorString());
|
|
||||||
qDebug() << message;
|
const QNetworkRequest request = m_serviceApi->createRequest(path);
|
||||||
emit deleteRequestFailure(message);
|
m_restManager->get(request, this, [this, path](QRestReply& reply) {
|
||||||
|
if (reply.isSuccess()) {
|
||||||
|
qInfo() << "Request successful.";
|
||||||
|
const QJsonDocument doc = reply.readJson().value();
|
||||||
|
onGetReplySuccessful(path, doc);
|
||||||
|
} else {
|
||||||
|
if (reply.hasError()) {
|
||||||
|
const QString errorString = reply.errorString();
|
||||||
|
qWarning() << "Network error:" << errorString;
|
||||||
|
onGetReplyFailure(path, errorString);
|
||||||
|
} else {
|
||||||
|
int statusCode = reply.httpStatus();
|
||||||
|
qWarning() << "Request not successful:" << statusCode;
|
||||||
|
onGetReplyFailure(path, QString("HTTP status code: %1").arg(statusCode));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
reply->deleteLater();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ServerCommunicator::onGetReplySuccessful(const QString& path, const QJsonDocument doc) {
|
||||||
|
if (path == ROUTE_ITEMS) {
|
||||||
|
emit itemsFetched(doc.toJson());
|
||||||
|
} else {
|
||||||
|
qWarning() << "Can't match request path:" << path;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerCommunicator::onGetReplyFailure(const QString& path, const QString errorString) {
|
||||||
|
if (path == ROUTE_ITEMS) {
|
||||||
|
emit itemsFetchFailure(errorString);
|
||||||
|
} else {
|
||||||
|
qWarning() << "Can't match request path:" << path;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerCommunicator::sendPostRequest(const QString& path, const QByteArray data) {
|
||||||
|
const QNetworkRequest request = m_serviceApi->createRequest(path);
|
||||||
|
|
||||||
|
m_restManager->post(request, data, this, [this, path](QRestReply& reply) {
|
||||||
|
if (reply.isSuccess()) {
|
||||||
|
int statusCode = reply.httpStatus();
|
||||||
|
qInfo() << "Request successful. Status code:" << statusCode;
|
||||||
|
const QJsonDocument doc = reply.readJson().value();
|
||||||
|
onPostReplySuccessful(path, doc);
|
||||||
|
} else {
|
||||||
|
if (reply.hasError()) {
|
||||||
|
const QString errorString = reply.errorString();
|
||||||
|
qWarning() << "Network error:" << errorString;
|
||||||
|
onPostReplyFailure(path, errorString);
|
||||||
|
} else {
|
||||||
|
int statusCode = reply.httpStatus();
|
||||||
|
qWarning() << "Request not successful:" << statusCode;
|
||||||
|
qInfo() << "Content:" << reply.readJson();
|
||||||
|
onPostReplyFailure(path, QString("HTTP status code: %1").arg(statusCode));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerCommunicator::onPostReplySuccessful(const QString& path, const QJsonDocument doc) {
|
||||||
|
if (path == ROUTE_ITEMS) {
|
||||||
|
emit sendItemSuccessful(doc.toJson());
|
||||||
|
} else {
|
||||||
|
qWarning() << "Can't match request path:" << path;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerCommunicator::onPostReplyFailure(const QString& path, const QString errorString) {
|
||||||
|
if (path == ROUTE_ITEMS) {
|
||||||
|
emit sendItemFailure(errorString);
|
||||||
|
} else {
|
||||||
|
qWarning() << "Can't match request path:" << path;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerCommunicator::sendDeleteRequest(const QString& path) {
|
||||||
|
const QNetworkRequest request = m_serviceApi->createRequest(path);
|
||||||
|
m_restManager->deleteResource(request, this, [this, path](QRestReply& reply) {
|
||||||
|
if (reply.isSuccess()) {
|
||||||
|
qInfo() << "Request successful.";
|
||||||
|
const QJsonDocument doc = reply.readJson().value();
|
||||||
|
onDeleteReplySuccessful(path, doc);
|
||||||
|
} else {
|
||||||
|
if (reply.hasError()) {
|
||||||
|
const QString errorString = reply.errorString();
|
||||||
|
qWarning() << "Network error:" << errorString;
|
||||||
|
onDeleteReplyFailure(path, errorString);
|
||||||
|
} else {
|
||||||
|
int statusCode = reply.httpStatus();
|
||||||
|
qWarning() << "Request not successful:" << statusCode;
|
||||||
|
onDeleteReplyFailure(path, QString("HTTP status code: %1").arg(statusCode));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerCommunicator::onDeleteReplySuccessful(const QString& path, const QJsonDocument doc) {
|
||||||
|
if (path.startsWith(ROUTE_ITEMS)) {
|
||||||
|
emit deleteItemSuccessful(doc.toJson());
|
||||||
|
} else {
|
||||||
|
qWarning() << "Can't match request path:" << path;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerCommunicator::onDeleteReplyFailure(const QString& path, const QString errorString) {
|
||||||
|
if (path.startsWith(ROUTE_ITEMS)) {
|
||||||
|
emit deleteItemFailure(errorString);
|
||||||
|
} else {
|
||||||
|
qWarning() << "Can't match request path:" << path;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -23,18 +23,21 @@ class ServerCommunicator : public QObject {
|
|||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void fetchItems();
|
void fetchItems();
|
||||||
void postItems(const QByteArray& jsonData);
|
void sendItem(const QByteArray& jsonData);
|
||||||
void deleteItem(const QString& id);
|
void deleteItem(const QString& id);
|
||||||
|
// NEXT editItem(const QByteArray& jsonData)
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void urlChanged();
|
void urlChanged();
|
||||||
|
|
||||||
void itemsFetched(const QByteArray jsonDoc);
|
void itemsFetched(const QByteArray jsonDoc);
|
||||||
void itemsFetchFailure(const QString errorString);
|
void itemsFetchFailure(const QString errorString);
|
||||||
void postRequestSuccessful(const QByteArray responseData);
|
|
||||||
void postRequestFailure(const QString errorString);
|
void sendItemSuccessful(const QByteArray responseData);
|
||||||
void deleteRequestSuccessful(const QByteArray responseData);
|
void sendItemFailure(const QString errorString);
|
||||||
void deleteRequestFailure(const QString errorString);
|
|
||||||
|
void deleteItemSuccessful(const QByteArray responseData);
|
||||||
|
void deleteItemFailure(const QString errorString);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QNetworkAccessManager m_netManager;
|
QNetworkAccessManager m_netManager;
|
||||||
@ -44,6 +47,18 @@ class ServerCommunicator : public QObject {
|
|||||||
QString m_email;
|
QString m_email;
|
||||||
QString m_password;
|
QString m_password;
|
||||||
QString m_authToken;
|
QString m_authToken;
|
||||||
|
|
||||||
|
void sendGetRequest(const QString& path);
|
||||||
|
void onGetReplySuccessful(const QString& path, const QJsonDocument doc);
|
||||||
|
void onGetReplyFailure(const QString& path, const QString errorString);
|
||||||
|
|
||||||
|
void sendPostRequest(const QString& path, const QByteArray data);
|
||||||
|
void onPostReplySuccessful(const QString& path, const QJsonDocument doc);
|
||||||
|
void onPostReplyFailure(const QString& path, const QString errorString);
|
||||||
|
|
||||||
|
void sendDeleteRequest(const QString& path);
|
||||||
|
void onDeleteReplySuccessful(const QString& path, const QJsonDocument doc);
|
||||||
|
void onDeleteReplyFailure(const QString& path, const QString errorString);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SERVERCOMMUNICATOR_H
|
#endif // SERVERCOMMUNICATOR_H
|
||||||
|
|||||||
Reference in New Issue
Block a user