sendItem(...) & deleteItem(...) use the more generic send...Request(...) functions. (No merging of UUID of sent item into model and no deletion of local item yet.)
This commit is contained in:
@ -159,7 +159,7 @@ void GenericCore::saveItems() {
|
||||
}
|
||||
|
||||
void GenericCore::onSendItemTriggered(const QByteArray& jsonData) {
|
||||
m_serverCommunicator->postItems(jsonData);
|
||||
m_serverCommunicator->sendItem(jsonData);
|
||||
}
|
||||
|
||||
void GenericCore::onItemsFetched(const QByteArray jsonData) {
|
||||
@ -173,20 +173,20 @@ void GenericCore::onItemsFetchFailure(const QString 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);
|
||||
emit displayStatusMessage(message);
|
||||
}
|
||||
|
||||
void GenericCore::onPostRequestFailure(const QString errorString) {
|
||||
void GenericCore::onSendItemFailure(const QString 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!!!";
|
||||
}
|
||||
|
||||
void GenericCore::onDeleteRequestFailure(const QString errorString) {
|
||||
void GenericCore::onDeleteItemFailure(const QString errorString) {
|
||||
qWarning() << "TODO: Process error response!!!";
|
||||
}
|
||||
|
||||
@ -252,7 +252,8 @@ void GenericCore::setupServerCommunication() {
|
||||
/// request connections
|
||||
connect(this, &GenericCore::fetchItemsFromServer, m_serverCommunicator.get(),
|
||||
&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(),
|
||||
&ServerCommunicator::deleteItem);
|
||||
|
||||
@ -261,14 +262,14 @@ void GenericCore::setupServerCommunication() {
|
||||
&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);
|
||||
connect(m_serverCommunicator.get(), &ServerCommunicator::deleteRequestSuccessful, this,
|
||||
&GenericCore::onDeleteRequestSuccessful);
|
||||
connect(m_serverCommunicator.get(), &ServerCommunicator::deleteRequestFailure, this,
|
||||
&GenericCore::onDeleteRequestFailure);
|
||||
connect(m_serverCommunicator.get(), &ServerCommunicator::sendItemSuccessful, this,
|
||||
&GenericCore::onSendItemSuccessful);
|
||||
connect(m_serverCommunicator.get(), &ServerCommunicator::sendItemFailure, this,
|
||||
&GenericCore::onSendItemFailure);
|
||||
connect(m_serverCommunicator.get(), &ServerCommunicator::deleteItemSuccessful, this,
|
||||
&GenericCore::onDeleteItemSuccessful);
|
||||
connect(m_serverCommunicator.get(), &ServerCommunicator::deleteItemFailure, this,
|
||||
&GenericCore::onDeleteItemFailure);
|
||||
|
||||
applyServerConfiguration();
|
||||
}
|
||||
|
||||
@ -42,15 +42,15 @@ class GenericCore : public QObject {
|
||||
void onSendItemTriggered(const QByteArray& jsonData);
|
||||
void onItemsFetched(const QByteArray jsonData);
|
||||
void onItemsFetchFailure(const QString errorString);
|
||||
void onPostRequestSuccessful(const QByteArray responseData);
|
||||
void onPostRequestFailure(const QString errorString);
|
||||
void onDeleteRequestSuccessful(const QByteArray responseData);
|
||||
void onDeleteRequestFailure(const QString errorString);
|
||||
void onSendItemSuccessful(const QByteArray responseData);
|
||||
void onSendItemFailure(const QString errorString);
|
||||
void onDeleteItemSuccessful(const QByteArray responseData);
|
||||
void onDeleteItemFailure(const QString errorString);
|
||||
|
||||
signals:
|
||||
void displayStatusMessage(QString message);
|
||||
void fetchItemsFromServer();
|
||||
void postItemToServer(const QByteArray& jsonData);
|
||||
void sendItemToServer(const QByteArray& jsonData);
|
||||
void deleteItemFromServer(const QString& id);
|
||||
|
||||
private:
|
||||
|
||||
@ -53,41 +53,13 @@ void ServerCommunicator::setServerConfiguration(const QString url,
|
||||
|
||||
void ServerCommunicator::fetchItems() { sendGetRequest(ROUTE_ITEMS); }
|
||||
|
||||
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::sendItem(const QByteArray& jsonData) {
|
||||
sendPostRequest(ROUTE_ITEMS, jsonData);
|
||||
}
|
||||
|
||||
void ServerCommunicator::deleteItem(const QString& id) {
|
||||
const QString deleteRoute = QString("%1/%2").arg(ROUTE_ITEMS, id);
|
||||
QNetworkReply* reply = m_restManager->deleteResource(m_serviceApi->createRequest(deleteRoute));
|
||||
|
||||
QObject::connect(reply, &QNetworkReply::finished, [=]() {
|
||||
if (reply->error() == QNetworkReply::NoError) {
|
||||
QByteArray responseData = reply->readAll();
|
||||
const QString message = QString("DELETE successful! Response: %1").arg(responseData);
|
||||
qInfo() << message;
|
||||
emit deleteRequestSuccessful(responseData);
|
||||
} else {
|
||||
const QString message = QString("Error: %1").arg(reply->errorString());
|
||||
qDebug() << message;
|
||||
emit deleteRequestFailure(message);
|
||||
}
|
||||
reply->deleteLater();
|
||||
});
|
||||
const QString path = QString("%1/%2").arg(ROUTE_ITEMS, id);
|
||||
sendDeleteRequest(path);
|
||||
}
|
||||
|
||||
void ServerCommunicator::sendGetRequest(const QString& path) {
|
||||
@ -133,8 +105,79 @@ void ServerCommunicator::onGetReplyFailure(const QString& path, const QString er
|
||||
}
|
||||
}
|
||||
|
||||
void ServerCommunicator::onSendPostRequestTriggered(const QString& path, const QByteArray data) {}
|
||||
void ServerCommunicator::sendPostRequest(const QString& path, const QByteArray data) {
|
||||
const QNetworkRequest request = m_serviceApi->createRequest(path);
|
||||
|
||||
void ServerCommunicator::onPostReplySuccessful(const QString& path, const QJsonDocument doc) {}
|
||||
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::onPostReplyFailure(const QString& path, const QString errorString) {}
|
||||
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:
|
||||
void fetchItems();
|
||||
void postItems(const QByteArray& jsonData);
|
||||
void sendItem(const QByteArray& jsonData);
|
||||
void deleteItem(const QString& id);
|
||||
// NEXT editItem(const QByteArray& jsonData)
|
||||
|
||||
signals:
|
||||
void urlChanged();
|
||||
|
||||
void itemsFetched(const QByteArray jsonDoc);
|
||||
void itemsFetchFailure(const QString errorString);
|
||||
void postRequestSuccessful(const QByteArray responseData);
|
||||
void postRequestFailure(const QString errorString);
|
||||
void deleteRequestSuccessful(const QByteArray responseData);
|
||||
void deleteRequestFailure(const QString errorString);
|
||||
|
||||
void sendItemSuccessful(const QByteArray responseData);
|
||||
void sendItemFailure(const QString errorString);
|
||||
|
||||
void deleteItemSuccessful(const QByteArray responseData);
|
||||
void deleteItemFailure(const QString errorString);
|
||||
|
||||
private:
|
||||
QNetworkAccessManager m_netManager;
|
||||
@ -48,6 +51,14 @@ class ServerCommunicator : public QObject {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user