When receiving online user credentials insert them into the model.
This commit is contained in:
@ -70,7 +70,25 @@ QByteArray JsonParser::itemValuesListToJson(const QList<ModelItemValues>& itemVa
|
||||
return jsonDoc.toJson(QJsonDocument::Compact);
|
||||
}
|
||||
|
||||
JsonParser::JsonParser() {}
|
||||
QByteArray JsonParser::ToJsonObject(const QHash<QString, QVariant>& values,
|
||||
const QString& objectName) {
|
||||
QJsonDocument jsonDoc;
|
||||
QJsonObject rootObject;
|
||||
QJsonObject itemObject;
|
||||
|
||||
QHashIterator<QString, QVariant> i(values);
|
||||
while (i.hasNext()) {
|
||||
i.next();
|
||||
const QString key = i.key();
|
||||
const QVariant value = i.value();
|
||||
itemObject.insert(key, value.toString());
|
||||
}
|
||||
|
||||
rootObject.insert(objectName, itemObject);
|
||||
jsonDoc.setObject(rootObject);
|
||||
|
||||
return jsonDoc.toJson(QJsonDocument::Compact);
|
||||
}
|
||||
|
||||
QJsonArray JsonParser::extractItemArray(const QJsonDocument& doc, const QString& objectName) {
|
||||
QJsonArray itemArray;
|
||||
@ -83,6 +101,8 @@ QJsonArray JsonParser::extractItemArray(const QJsonDocument& doc, const QString&
|
||||
return itemArray;
|
||||
}
|
||||
|
||||
JsonParser::JsonParser() {}
|
||||
|
||||
ModelItemValues JsonParser::jsonObjectToItemValues(const QJsonObject& itemJsonObject) {
|
||||
ModelItemValues values;
|
||||
|
||||
|
||||
@ -18,6 +18,8 @@ class JsonParser {
|
||||
const QString& rootValueName = "");
|
||||
static QByteArray itemValuesListToJson(const QList<ModelItemValues>& itemValuesList,
|
||||
const QString& objectName = "");
|
||||
static QByteArray ToJsonObject(const QHash<QString, QVariant>& Values,
|
||||
const QString& objectName = "");
|
||||
|
||||
private:
|
||||
explicit JsonParser();
|
||||
|
||||
@ -162,14 +162,21 @@ void GenericCore::onBiddingsChanged(int round, QList<bidding> biddings) {
|
||||
|
||||
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);
|
||||
QHash<QString, QVariant> hash;
|
||||
hash.insert("email", mailAddress);
|
||||
const QByteArray jsonDoc = JsonParser::ToJsonObject(hash, "user");
|
||||
emit sendPostRequest(RegisterUser, jsonDoc);
|
||||
}
|
||||
|
||||
// void GenericCore::onOnlineUserCreatedReceived(const QJsonDocument& jsonDoc) {}
|
||||
void GenericCore::onOnlineUserAccountReceived(const QString mailAddress,
|
||||
const QString uuid,
|
||||
const QString accessToken) {
|
||||
m_mainModel->setOnlineCredentials(mailAddress, uuid, accessToken);
|
||||
|
||||
// void GenericCore::onOnlineUserExistedReceived(const QJsonDocument jsonDoc) {}
|
||||
const QString message = QString("Online credentials received for: %1").arg(mailAddress);
|
||||
emit displayStatusMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
void GenericCore::setupModels() {
|
||||
m_mainModel = make_shared<TableModel>(m_modelUndoStack);
|
||||
|
||||
@ -50,8 +50,9 @@ class GenericCore : public QObject {
|
||||
void onBiddingsChanged(int round, QList<bidding> biddings);
|
||||
|
||||
void onCreateOnlineAccountTriggered(const QString& mailAddress);
|
||||
// void onOnlineUserCreatedReceived(const QJsonDocument& jsonDoc);
|
||||
// void onOnlineUserExistedReceived(const QJsonDocument jsonDoc);
|
||||
void onOnlineUserAccountReceived(const QString mailAddress,
|
||||
const QString uuid,
|
||||
const QString accessToken);
|
||||
|
||||
signals:
|
||||
void displayStatusMessage(QString message);
|
||||
@ -59,7 +60,7 @@ class GenericCore : public QObject {
|
||||
/// *** server communication ***
|
||||
/// request signals
|
||||
void sendGetRequest(GetRequestTypes type, QVariant data = QVariant());
|
||||
void sendPostRequest(PostRequestTypes type, QVariant data = QVariant());
|
||||
void sendPostRequest(PostRequestTypes type, QByteArray data);
|
||||
/// response signals
|
||||
void currentBiddingRoundChanged(int round, bool isRunning);
|
||||
|
||||
|
||||
@ -231,6 +231,14 @@ bool TableModel::updateItem(const ModelItemValues& itemValues) {
|
||||
}
|
||||
}
|
||||
|
||||
void TableModel::setOnlineCredentials(const QString& mail,
|
||||
const QString& uuid,
|
||||
const QString& token) {
|
||||
QModelIndex itemIndex = getIndexByRoleValue(mail, MailRole);
|
||||
setItemData(itemIndex, {{OnlineIdRole, uuid}, {AccessCodeRole, token}});
|
||||
}
|
||||
|
||||
|
||||
bool TableModel::removeRows(int firstRow, int nRows, const QModelIndex& parentIndex) {
|
||||
if (parentIndex != QModelIndex()) {
|
||||
qWarning() << "Removing of child rows is not supported yet!";
|
||||
@ -282,29 +290,6 @@ void TableModel::insertItems(int startPosition,
|
||||
m_undoStack->push(insertCommand);
|
||||
}
|
||||
|
||||
void TableModel::onRowCountChanged(const QModelIndex& parent, int first, int last) {
|
||||
Q_UNUSED(first);
|
||||
Q_UNUSED(last);
|
||||
|
||||
if (parent != QModelIndex()) {
|
||||
return;
|
||||
}
|
||||
emit rowCountChanged();
|
||||
emit nExpectedBiddingsChanged();
|
||||
|
||||
emit nPlacedBiddingsChanged(1);
|
||||
emit nPlacedBiddingsChanged(2);
|
||||
emit nPlacedBiddingsChanged(3);
|
||||
|
||||
emit biddingSumChanged(1);
|
||||
emit biddingSumChanged(2);
|
||||
emit biddingSumChanged(3);
|
||||
|
||||
emit biddingAverageChanged(1);
|
||||
emit biddingAverageChanged(2);
|
||||
emit biddingAverageChanged(3);
|
||||
}
|
||||
|
||||
int TableModel::nExpectedBiddings() const {
|
||||
int result = 0;
|
||||
for (auto i = m_items.begin(), end = m_items.end(); i != end; ++i) {
|
||||
@ -352,6 +337,29 @@ qreal TableModel::biddingAverage3() const {
|
||||
return averageBidding;
|
||||
}
|
||||
|
||||
void TableModel::onRowCountChanged(const QModelIndex& parent, int first, int last) {
|
||||
Q_UNUSED(first);
|
||||
Q_UNUSED(last);
|
||||
|
||||
if (parent != QModelIndex()) {
|
||||
return;
|
||||
}
|
||||
emit rowCountChanged();
|
||||
emit nExpectedBiddingsChanged();
|
||||
|
||||
emit nPlacedBiddingsChanged(1);
|
||||
emit nPlacedBiddingsChanged(2);
|
||||
emit nPlacedBiddingsChanged(3);
|
||||
|
||||
emit biddingSumChanged(1);
|
||||
emit biddingSumChanged(2);
|
||||
emit biddingSumChanged(3);
|
||||
|
||||
emit biddingAverageChanged(1);
|
||||
emit biddingAverageChanged(2);
|
||||
emit biddingAverageChanged(3);
|
||||
}
|
||||
|
||||
void TableModel::execInsertItems(const int firstRow, const QList<ModelItemValues> valueList) {
|
||||
const int nRows = valueList.size();
|
||||
qDebug() << "Inserting" << nRows << "items...";
|
||||
@ -387,7 +395,6 @@ void TableModel::execEditItemData(const int row, const QMap<int, QVariant>& chan
|
||||
roles.insert(0, Qt::DisplayRole);
|
||||
emit dataChanged(firstIndex, lastIndex, roles.toVector());
|
||||
|
||||
// NEXT check which roles are changed & trigger only changed properties
|
||||
if (roles.contains(BiddingTypeRole) || roles.contains(ShareAmountRole) ||
|
||||
roles.contains(ShareTypeRole)) {
|
||||
emit nExpectedBiddingsChanged();
|
||||
@ -576,3 +583,29 @@ qreal TableModel::totalSharesWithBiddings(const UserRoles biddingRole) const {
|
||||
qInfo() << "Biddings exist for " << result << "shares!";
|
||||
return result;
|
||||
}
|
||||
|
||||
QModelIndex TableModel::getIndexByRoleValue(const QString& valueString, const int role) const {
|
||||
const QString text =
|
||||
QString("Searching list item with value '%1' for role '%2'").arg(valueString, role);
|
||||
qInfo() << text;
|
||||
int itemRow = -1;
|
||||
|
||||
for (auto i = m_items.begin(), end = m_items.end(); i != end; ++i) {
|
||||
const QString mailValue = (*i)->data(role).toString();
|
||||
qDebug() << "found value:" << mailValue;
|
||||
if (valueString == mailValue) {
|
||||
itemRow = i - m_items.constBegin();
|
||||
qInfo() << "Found item at row:" << itemRow;
|
||||
break;
|
||||
} else {
|
||||
qDebug() << "Not the right item. Continuing search...";
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (itemRow < 0 || itemRow >= m_items.length()) {
|
||||
qWarning() << "Couldn't find item with mail address:" << valueString << "- Returning...";
|
||||
return QModelIndex();
|
||||
}
|
||||
const QModelIndex itemIndex = index(itemRow, 0);
|
||||
return itemIndex;
|
||||
}
|
||||
|
||||
@ -44,6 +44,7 @@ class TableModel : public QAbstractTableModel {
|
||||
QString updateItemsFromJson(const QByteArray& jsonData);
|
||||
bool updateItem(const ModelItemValues& itemValues);
|
||||
|
||||
void setOnlineCredentials(const QString& mail, const QString& uuid, const QString& token);
|
||||
public slots:
|
||||
// bool insertRows(int position, int rows, const QModelIndex& parentIndex = QModelIndex())
|
||||
// override;
|
||||
@ -105,6 +106,8 @@ class TableModel : public QAbstractTableModel {
|
||||
int biddingSum(const UserRoles biddingRole) const;
|
||||
qreal averageBiddingAmount(const UserRoles biddingRole) const;
|
||||
qreal totalSharesWithBiddings(const UserRoles biddingRole) const;
|
||||
|
||||
QModelIndex getIndexByRoleValue(const QString& valueString, const int role) const;
|
||||
};
|
||||
|
||||
#endif // TABLEMODEL_H
|
||||
|
||||
@ -23,6 +23,8 @@ ServerCommunicator::ServerCommunicator(GenericCore* core)
|
||||
connect(m_core, &GenericCore::sendPostRequest, this,
|
||||
&ServerCommunicator::onSendPostRequestTriggered);
|
||||
connect(this, &ServerCommunicator::biddingsChanged, m_core, &GenericCore::onBiddingsChanged);
|
||||
connect(this, &ServerCommunicator::onlineUserAccountReceived, m_core,
|
||||
&GenericCore::onOnlineUserAccountReceived);
|
||||
}
|
||||
|
||||
bool ServerCommunicator::sslSupported() const {
|
||||
@ -133,7 +135,8 @@ void ServerCommunicator::onGetReplyFailure(const GetRequestTypes type, const QSt
|
||||
m_core->displayStatusMessage(message);
|
||||
}
|
||||
|
||||
void ServerCommunicator::onSendPostRequestTriggered(const PostRequestTypes type, QVariant data) {
|
||||
void ServerCommunicator::onSendPostRequestTriggered(const PostRequestTypes type,
|
||||
const QByteArray data) {
|
||||
QString path;
|
||||
switch (type) {
|
||||
case RegisterUser:
|
||||
@ -152,16 +155,7 @@ void ServerCommunicator::onSendPostRequestTriggered(const PostRequestTypes type,
|
||||
|
||||
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) {
|
||||
m_restManager->post(request, data, this, [this, type](QRestReply& reply) {
|
||||
if (reply.isSuccess()) {
|
||||
int statusCode = reply.httpStatus();
|
||||
qInfo() << "Request successful. Status code:" << statusCode;
|
||||
@ -187,7 +181,7 @@ void ServerCommunicator::onPostReplySuccessful(const PostRequestTypes type,
|
||||
switch (type) {
|
||||
case RegisterUser:
|
||||
qInfo() << "Register user successful:" << type;
|
||||
m_core->displayStatusMessage(doc.toJson());
|
||||
onlineUserAccountReply(doc);
|
||||
break;
|
||||
default:
|
||||
qWarning() << "Can't match request type:" << type;
|
||||
@ -225,6 +219,16 @@ void ServerCommunicator::currentBiddingsReply(const QJsonDocument jsonDoc) {
|
||||
.depotWishOne = "one",
|
||||
.depotWishTwo = "two"};
|
||||
const QList<bidding> biddings{dummyBidding};
|
||||
|
||||
emit biddingsChanged(roundNumber, biddings);
|
||||
}
|
||||
|
||||
void ServerCommunicator::onlineUserAccountReply(const QJsonDocument jsonDoc) {
|
||||
qInfo() << "Online user account received.";
|
||||
// TODO move data extraction of jsonDoc into JsonParser
|
||||
const QJsonObject rootObject = jsonDoc["data"].toObject();
|
||||
const QString emailAddress = rootObject["email"].toString();
|
||||
const QString uuid = rootObject["id"].toString();
|
||||
const QString token = rootObject["token"].toString();
|
||||
|
||||
emit onlineUserAccountReceived(emailAddress, uuid, token);
|
||||
}
|
||||
|
||||
@ -29,7 +29,7 @@ class ServerCommunicator : public QObject {
|
||||
void onGetReplySuccessful(const GetRequestTypes type, const QJsonDocument doc);
|
||||
void onGetReplyFailure(const GetRequestTypes type, const QString errorString);
|
||||
|
||||
void onSendPostRequestTriggered(const PostRequestTypes type, QVariant data);
|
||||
void onSendPostRequestTriggered(const PostRequestTypes type, const QByteArray data);
|
||||
void onPostReplySuccessful(const PostRequestTypes type, const QJsonDocument doc);
|
||||
void onPostReplyFailure(const PostRequestTypes type, const QString errorString);
|
||||
|
||||
@ -45,6 +45,9 @@ class ServerCommunicator : public QObject {
|
||||
|
||||
void currentBiddingRoundChanged(int round, bool isRunning);
|
||||
void biddingsChanged(int round, QList<bidding> biddings);
|
||||
void onlineUserAccountReceived(const QString mailAddress,
|
||||
const QString uuid,
|
||||
const QString accessToken);
|
||||
|
||||
private:
|
||||
GenericCore* m_core = nullptr;
|
||||
@ -60,6 +63,7 @@ class ServerCommunicator : public QObject {
|
||||
/// reply parser
|
||||
void currentBiddingRoundChangedReply(const QJsonDocument jsonDoc);
|
||||
void currentBiddingsReply(const QJsonDocument jsonDoc);
|
||||
void onlineUserAccountReply(const QJsonDocument jsonDoc);
|
||||
};
|
||||
|
||||
#endif // SERVERCOMMUNICATOR_H
|
||||
|
||||
Reference in New Issue
Block a user