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:
2026-01-29 08:54:47 +01:00
parent e29cd0aebf
commit bc96a805f8
4 changed files with 52 additions and 2 deletions

View File

@ -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) { void GenericCore::onItemsFetched(const QByteArray jsonDoc) {
emit displayStatusMessage("New items fetched."); emit displayStatusMessage("New items fetched.");
// TODO ? check compability of JSON structure beforehand? // TODO ? check compability of JSON structure beforehand?
@ -219,7 +224,12 @@ QString GenericCore::getMaintenanceToolFilePath() const {
void GenericCore::setupServerConfiguration() { void GenericCore::setupServerConfiguration() {
m_serverCommunicator = make_unique<ServerCommunicator>(this); 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, connect(m_serverCommunicator.get(), &ServerCommunicator::itemsFetched, this,
&GenericCore::onItemsFetched); &GenericCore::onItemsFetched);
connect(m_serverCommunicator.get(), &ServerCommunicator::itemsFetchFailure, this, connect(m_serverCommunicator.get(), &ServerCommunicator::itemsFetchFailure, this,
@ -229,6 +239,7 @@ void GenericCore::setupServerConfiguration() {
connect(m_serverCommunicator.get(), &ServerCommunicator::postRequestFailure, this, connect(m_serverCommunicator.get(), &ServerCommunicator::postRequestFailure, this,
&GenericCore::onPostRequestFailure); &GenericCore::onPostRequestFailure);
/// fetching items on startup to test the communication with the server /// fetching/posting items on startup to test the communication with the server
m_serverCommunicator->fetchItems(); // m_serverCommunicator->fetchItems();
// m_serverCommunicator->postItems();
} }

View File

@ -36,6 +36,7 @@ class GenericCore : public QObject {
bool isSyncServerSetup() const; bool isSyncServerSetup() const;
public slots: public slots:
void onSendItemTriggered(const int row);
void onItemsFetched(const QByteArray jsonDoc); void onItemsFetched(const QByteArray jsonDoc);
void onItemsFetchFailure(const QString errorString); void onItemsFetchFailure(const QString errorString);
void onPostRequestSuccessful(const QString message); void onPostRequestSuccessful(const QString message);
@ -43,6 +44,8 @@ class GenericCore : public QObject {
signals: signals:
void displayStatusMessage(QString message); void displayStatusMessage(QString message);
void fetchItemsFromServer();
void sendItemToServer(int row);
private: private:
QUndoStack* m_modelUndoStack; QUndoStack* m_modelUndoStack;

View File

@ -32,6 +32,7 @@ void ServerCommunicator::setUrl(const QUrl& url) {
} }
m_serviceApi->setBaseUrl(url); m_serviceApi->setBaseUrl(url);
QHttpHeaders authenticationHeaders; QHttpHeaders authenticationHeaders;
authenticationHeaders.append(QHttpHeaders::WellKnownHeader::ContentType, "application/json");
m_serviceApi->setCommonHeaders(authenticationHeaders); m_serviceApi->setCommonHeaders(authenticationHeaders);
emit urlChanged(); 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();
});
}

View File

@ -16,13 +16,17 @@ class ServerCommunicator : public QObject {
QUrl url() const; QUrl url() const;
void setUrl(const QUrl& url); void setUrl(const QUrl& url);
public slots:
void fetchItems(); void fetchItems();
void postItems(); /// NEXT add item(s) as argument;
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 QString message);
void postRequestFailure(const QString errorString);
private: private:
QNetworkAccessManager m_netManager; QNetworkAccessManager m_netManager;