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();
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user