An item (with hard coded values) can be send to the server. Added signals for fetching and posting items to be triggered from the UI.
This commit is contained in:
@ -142,6 +142,11 @@ bool GenericCore::isSyncServerSetup() const {
|
||||
}
|
||||
}
|
||||
|
||||
void GenericCore::onSendItemTriggered(const int row) {
|
||||
// NEXT gather item values from model to send to server
|
||||
m_serverCommunicator->postItems();
|
||||
}
|
||||
|
||||
void GenericCore::onItemsFetched(const QByteArray jsonDoc) {
|
||||
emit displayStatusMessage("New items fetched.");
|
||||
// TODO ? check compability of JSON structure beforehand?
|
||||
@ -219,7 +224,12 @@ QString GenericCore::getMaintenanceToolFilePath() const {
|
||||
|
||||
void GenericCore::setupServerConfiguration() {
|
||||
m_serverCommunicator = make_unique<ServerCommunicator>(this);
|
||||
/// request connections
|
||||
connect(this, &GenericCore::fetchItemsFromServer, m_serverCommunicator.get(),
|
||||
&ServerCommunicator::fetchItems);
|
||||
connect(this, &GenericCore::sendItemToServer, this, &GenericCore::onSendItemTriggered);
|
||||
|
||||
/// response connections
|
||||
connect(m_serverCommunicator.get(), &ServerCommunicator::itemsFetched, this,
|
||||
&GenericCore::onItemsFetched);
|
||||
connect(m_serverCommunicator.get(), &ServerCommunicator::itemsFetchFailure, this,
|
||||
@ -229,6 +239,7 @@ void GenericCore::setupServerConfiguration() {
|
||||
connect(m_serverCommunicator.get(), &ServerCommunicator::postRequestFailure, this,
|
||||
&GenericCore::onPostRequestFailure);
|
||||
|
||||
/// fetching items on startup to test the communication with the server
|
||||
m_serverCommunicator->fetchItems();
|
||||
/// fetching/posting items on startup to test the communication with the server
|
||||
// m_serverCommunicator->fetchItems();
|
||||
// m_serverCommunicator->postItems();
|
||||
}
|
||||
|
||||
@ -36,6 +36,7 @@ class GenericCore : public QObject {
|
||||
bool isSyncServerSetup() const;
|
||||
|
||||
public slots:
|
||||
void onSendItemTriggered(const int row);
|
||||
void onItemsFetched(const QByteArray jsonDoc);
|
||||
void onItemsFetchFailure(const QString errorString);
|
||||
void onPostRequestSuccessful(const QString message);
|
||||
@ -43,6 +44,8 @@ class GenericCore : public QObject {
|
||||
|
||||
signals:
|
||||
void displayStatusMessage(QString message);
|
||||
void fetchItemsFromServer();
|
||||
void sendItemToServer(int row);
|
||||
|
||||
private:
|
||||
QUndoStack* m_modelUndoStack;
|
||||
|
||||
@ -32,6 +32,7 @@ void ServerCommunicator::setUrl(const QUrl& url) {
|
||||
}
|
||||
m_serviceApi->setBaseUrl(url);
|
||||
QHttpHeaders authenticationHeaders;
|
||||
authenticationHeaders.append(QHttpHeaders::WellKnownHeader::ContentType, "application/json");
|
||||
m_serviceApi->setCommonHeaders(authenticationHeaders);
|
||||
emit urlChanged();
|
||||
}
|
||||
@ -58,3 +59,34 @@ void ServerCommunicator::fetchItems() {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void ServerCommunicator::postItems() {
|
||||
QJsonObject itemObject;
|
||||
itemObject["name"] = "Post test 1";
|
||||
itemObject["description"] = "Post description 1";
|
||||
itemObject["info"] = "Post info 1";
|
||||
itemObject["amount"] = 1;
|
||||
itemObject["factor"] = 5.3;
|
||||
|
||||
QJsonObject rootObject;
|
||||
rootObject.insert("item", itemObject);
|
||||
|
||||
QJsonDocument jsonDoc(rootObject);
|
||||
QByteArray jsonData = jsonDoc.toJson();
|
||||
|
||||
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(message);
|
||||
} else {
|
||||
const QString message = QString("Error: %1").arg(reply->errorString());
|
||||
qDebug() << message;
|
||||
emit postRequestFailure(message);
|
||||
}
|
||||
reply->deleteLater();
|
||||
});
|
||||
}
|
||||
|
||||
@ -16,13 +16,17 @@ class ServerCommunicator : public QObject {
|
||||
QUrl url() const;
|
||||
void setUrl(const QUrl& url);
|
||||
|
||||
public slots:
|
||||
void fetchItems();
|
||||
void postItems(); /// NEXT add item(s) as argument;
|
||||
|
||||
signals:
|
||||
void urlChanged();
|
||||
|
||||
void itemsFetched(const QByteArray jsonDoc);
|
||||
void itemsFetchFailure(const QString errorString);
|
||||
void postRequestSuccessful(const QString message);
|
||||
void postRequestFailure(const QString errorString);
|
||||
|
||||
private:
|
||||
QNetworkAccessManager m_netManager;
|
||||
|
||||
Reference in New Issue
Block a user