From bc96a805f8321c620fc1d0a80d461e69a3fb4aaa Mon Sep 17 00:00:00 2001 From: Bent Witthold Date: Thu, 29 Jan 2026 08:54:47 +0100 Subject: [PATCH] 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. --- genericcore.cpp | 15 +++++++++++++-- genericcore.h | 3 +++ network/servercommunicator.cpp | 32 ++++++++++++++++++++++++++++++++ network/servercommunicator.h | 4 ++++ 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/genericcore.cpp b/genericcore.cpp index c26c2cc..4394f3b 100644 --- a/genericcore.cpp +++ b/genericcore.cpp @@ -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(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(); } diff --git a/genericcore.h b/genericcore.h index a916647..8e4e8b3 100644 --- a/genericcore.h +++ b/genericcore.h @@ -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; diff --git a/network/servercommunicator.cpp b/network/servercommunicator.cpp index 89e07d8..c4fce47 100644 --- a/network/servercommunicator.cpp +++ b/network/servercommunicator.cpp @@ -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(); + }); +} diff --git a/network/servercommunicator.h b/network/servercommunicator.h index 5c23469..73ac5f4 100644 --- a/network/servercommunicator.h +++ b/network/servercommunicator.h @@ -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;