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:
@ -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