Online account can now be created from the EditItemDialog. The response isn't processed properly yet.
This commit is contained in:
@ -160,6 +160,17 @@ void GenericCore::onBiddingsChanged(int round, QList<bidding> biddings) {
|
||||
// NEXT merge biddings into model
|
||||
}
|
||||
|
||||
void GenericCore::onCreateOnlineAccountTriggered(const QString& mailAddress) {
|
||||
qInfo() << "Creating online account for:" << mailAddress;
|
||||
// QJsonDocument onlineCredentialsDoc = m_mainModel->getOnlineCredentialsAsJsonDoc(mailAddress);
|
||||
// emit createOnlineUser(onlineCredentialsDoc.toJson());
|
||||
emit sendPostRequest(RegisterUser, mailAddress);
|
||||
}
|
||||
|
||||
// void GenericCore::onOnlineUserCreatedReceived(const QJsonDocument& jsonDoc) {}
|
||||
|
||||
// void GenericCore::onOnlineUserExistedReceived(const QJsonDocument jsonDoc) {}
|
||||
|
||||
void GenericCore::setupModels() {
|
||||
m_mainModel = make_shared<TableModel>(m_modelUndoStack);
|
||||
m_sortFilterModel = make_shared<GeneralSortFilterModel>(m_mainModel);
|
||||
|
||||
@ -49,12 +49,17 @@ class GenericCore : public QObject {
|
||||
public slots:
|
||||
void onBiddingsChanged(int round, QList<bidding> biddings);
|
||||
|
||||
void onCreateOnlineAccountTriggered(const QString& mailAddress);
|
||||
// void onOnlineUserCreatedReceived(const QJsonDocument& jsonDoc);
|
||||
// void onOnlineUserExistedReceived(const QJsonDocument jsonDoc);
|
||||
|
||||
signals:
|
||||
void displayStatusMessage(QString message);
|
||||
|
||||
/// *** server communication ***
|
||||
/// request signals
|
||||
void sendGetRequest(GetRequestTypes type, QVariant data = QVariant());
|
||||
void sendPostRequest(PostRequestTypes type, QVariant data = QVariant());
|
||||
/// response signals
|
||||
void currentBiddingRoundChanged(int round, bool isRunning);
|
||||
|
||||
|
||||
@ -91,6 +91,7 @@ enum GetRequestTypes {
|
||||
GetBiddingsOfSpecificRound,
|
||||
GetBiddingsOfHighestRound
|
||||
};
|
||||
enum PostRequestTypes { RegisterUser };
|
||||
|
||||
/// functions
|
||||
static UserRoles GET_ROLE_FOR_COLUMN(const int column) {
|
||||
|
||||
@ -9,6 +9,8 @@ static const QString apiPrefix = "/api/";
|
||||
|
||||
static const QString ROUTE_ITEMS = apiPrefix + "items";
|
||||
|
||||
static const QString ROUTE_REGISTER_USER = apiPrefix + "users";
|
||||
|
||||
static const QString ROUTE_BIDDINGROUNDS = apiPrefix + "bidding_rounds";
|
||||
static const QString ROUTE_CURRENT_BIDDINGROUND = ROUTE_BIDDINGROUNDS + "/get_current";
|
||||
static const QString ROUTE_START_BIDDINGROUND = ROUTE_BIDDINGROUNDS + "/start_new";
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -29,9 +29,9 @@ class ServerCommunicator : public QObject {
|
||||
void onGetReplySuccessful(const GetRequestTypes type, const QJsonDocument doc);
|
||||
void onGetReplyFailure(const GetRequestTypes type, const QString errorString);
|
||||
|
||||
void fetchItems();
|
||||
void postItems(const QByteArray& jsonData);
|
||||
void deleteItem(const QString& id);
|
||||
void onSendPostRequestTriggered(const PostRequestTypes type, QVariant data);
|
||||
void onPostReplySuccessful(const PostRequestTypes type, const QJsonDocument doc);
|
||||
void onPostReplyFailure(const PostRequestTypes type, const QString errorString);
|
||||
|
||||
signals:
|
||||
void urlChanged();
|
||||
|
||||
Reference in New Issue
Block a user