Online account can now be created from the EditItemDialog. The response isn't processed properly yet.
This commit is contained in:
@ -20,6 +20,8 @@ ServerCommunicator::ServerCommunicator(GenericCore* core)
|
||||
|
||||
connect(m_core, &GenericCore::sendGetRequest, this,
|
||||
&ServerCommunicator::onSendGetRequestTriggered);
|
||||
connect(m_core, &GenericCore::sendPostRequest, this,
|
||||
&ServerCommunicator::onSendPostRequestTriggered);
|
||||
connect(this, &ServerCommunicator::biddingsChanged, m_core, &GenericCore::onBiddingsChanged);
|
||||
}
|
||||
|
||||
@ -44,66 +46,6 @@ void ServerCommunicator::setUrl(const QUrl& url) {
|
||||
emit urlChanged();
|
||||
}
|
||||
|
||||
void ServerCommunicator::fetchItems() {
|
||||
/// Set up a GET request
|
||||
m_restManager->get(m_serviceApi->createRequest(ROUTE_ITEMS), this, [this](QRestReply& reply) {
|
||||
if (reply.isSuccess()) {
|
||||
qInfo() << "Fetching items successful.";
|
||||
const QJsonDocument doc = reply.readJson().value();
|
||||
emit itemsFetched(doc.toJson());
|
||||
|
||||
} else {
|
||||
if (reply.hasError()) {
|
||||
const QString errorString = reply.errorString();
|
||||
qCritical() << "ERROR:" << errorString;
|
||||
emit itemsFetchFailure(errorString);
|
||||
} else {
|
||||
int statusCode = reply.httpStatus();
|
||||
qCritical() << "ERROR:" << statusCode;
|
||||
emit itemsFetchFailure(QString::number(statusCode));
|
||||
emit itemsFetchFailure(QString("HTTP status code: %1").arg(statusCode));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void ServerCommunicator::postItems(const QByteArray& jsonData) {
|
||||
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(responseData);
|
||||
} else {
|
||||
const QString message = QString("Error: %1").arg(reply->errorString());
|
||||
qDebug() << message;
|
||||
emit postRequestFailure(message);
|
||||
}
|
||||
reply->deleteLater();
|
||||
});
|
||||
}
|
||||
|
||||
void ServerCommunicator::deleteItem(const QString& id) {
|
||||
const QString deleteRoute = QString("%1/%2").arg(ROUTE_ITEMS, id);
|
||||
QNetworkReply* reply = m_restManager->deleteResource(m_serviceApi->createRequest(deleteRoute));
|
||||
|
||||
QObject::connect(reply, &QNetworkReply::finished, [=]() {
|
||||
if (reply->error() == QNetworkReply::NoError) {
|
||||
QByteArray responseData = reply->readAll();
|
||||
const QString message = QString("DELETE successful! Response: %1").arg(responseData);
|
||||
qInfo() << message;
|
||||
emit deleteRequestSuccessful(responseData);
|
||||
} else {
|
||||
const QString message = QString("Error: %1").arg(reply->errorString());
|
||||
qDebug() << message;
|
||||
emit deleteRequestFailure(message);
|
||||
}
|
||||
reply->deleteLater();
|
||||
});
|
||||
}
|
||||
|
||||
void ServerCommunicator::setServerConfiguration(const QString url,
|
||||
const QString email,
|
||||
const QString password) {
|
||||
@ -191,6 +133,75 @@ void ServerCommunicator::onGetReplyFailure(const GetRequestTypes type, const QSt
|
||||
m_core->displayStatusMessage(message);
|
||||
}
|
||||
|
||||
void ServerCommunicator::onSendPostRequestTriggered(const PostRequestTypes type, QVariant data) {
|
||||
QString path;
|
||||
switch (type) {
|
||||
case RegisterUser:
|
||||
path = ROUTE_REGISTER_USER;
|
||||
break;
|
||||
default:
|
||||
qWarning() << "No route found for PostRequestType:" << type;
|
||||
break;
|
||||
}
|
||||
|
||||
// TODO move into default case of switch statement?
|
||||
if (path.isEmpty()) {
|
||||
qDebug() << "Empty path -> Not sending a request.";
|
||||
return;
|
||||
}
|
||||
|
||||
const QNetworkRequest request = m_serviceApi->createRequest(path);
|
||||
|
||||
QJsonDocument doc = QJsonDocument();
|
||||
QJsonObject rootObject;
|
||||
|
||||
QJsonObject itemObject;
|
||||
itemObject.insert("email", data.toString());
|
||||
rootObject.insert("user", itemObject);
|
||||
|
||||
doc.setObject(rootObject);
|
||||
|
||||
m_restManager->post(request, doc, this, [this, type](QRestReply& reply) {
|
||||
if (reply.isSuccess()) {
|
||||
int statusCode = reply.httpStatus();
|
||||
qInfo() << "Request successful. Status code:" << statusCode;
|
||||
const QJsonDocument doc = reply.readJson().value();
|
||||
onPostReplySuccessful(type, doc);
|
||||
} else {
|
||||
if (reply.hasError()) {
|
||||
const QString errorString = reply.errorString();
|
||||
qWarning() << "Network error:" << errorString;
|
||||
onPostReplyFailure(type, errorString);
|
||||
} else {
|
||||
int statusCode = reply.httpStatus();
|
||||
qWarning() << "Request not successful:" << statusCode;
|
||||
qCritical() << "Content:" << reply.readJson();
|
||||
onPostReplyFailure(type, QString("HTTP status code: %1").arg(statusCode));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void ServerCommunicator::onPostReplySuccessful(const PostRequestTypes type,
|
||||
const QJsonDocument doc) {
|
||||
switch (type) {
|
||||
case RegisterUser:
|
||||
qInfo() << "Register user successful:" << type;
|
||||
m_core->displayStatusMessage(doc.toJson());
|
||||
break;
|
||||
default:
|
||||
qWarning() << "Can't match request type:" << type;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void ServerCommunicator::onPostReplyFailure(const PostRequestTypes type,
|
||||
const QString errorString) {
|
||||
const QString message =
|
||||
QString("Request of type %1 returned: %2").arg(QString::number(type), errorString);
|
||||
m_core->displayStatusMessage(message);
|
||||
}
|
||||
|
||||
void ServerCommunicator::currentBiddingRoundChangedReply(const QJsonDocument jsonDoc) {
|
||||
qInfo() << "Current bidding round received.";
|
||||
const QJsonObject rootObject = jsonDoc["data"].toObject();
|
||||
|
||||
Reference in New Issue
Block a user