From 7d31ac8806b0ecc31ab3868c1f6e943b206f1087 Mon Sep 17 00:00:00 2001 From: Bent Witthold Date: Thu, 19 Feb 2026 09:10:06 +0100 Subject: [PATCH] When receiving online user credentials insert them into the model. --- libs/BeetRoundCore/formats/jsonparser.cpp | 22 ++++- libs/BeetRoundCore/formats/jsonparser.h | 2 + libs/BeetRoundCore/genericcore.cpp | 17 ++-- libs/BeetRoundCore/genericcore.h | 7 +- libs/BeetRoundCore/model/tablemodel.cpp | 81 +++++++++++++------ libs/BeetRoundCore/model/tablemodel.h | 3 + .../network/servercommunicator.cpp | 30 ++++--- .../network/servercommunicator.h | 6 +- 8 files changed, 121 insertions(+), 47 deletions(-) diff --git a/libs/BeetRoundCore/formats/jsonparser.cpp b/libs/BeetRoundCore/formats/jsonparser.cpp index 3067cc6..6e477fc 100644 --- a/libs/BeetRoundCore/formats/jsonparser.cpp +++ b/libs/BeetRoundCore/formats/jsonparser.cpp @@ -70,7 +70,25 @@ QByteArray JsonParser::itemValuesListToJson(const QList& itemVa return jsonDoc.toJson(QJsonDocument::Compact); } -JsonParser::JsonParser() {} +QByteArray JsonParser::ToJsonObject(const QHash& values, + const QString& objectName) { + QJsonDocument jsonDoc; + QJsonObject rootObject; + QJsonObject itemObject; + + QHashIterator 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; diff --git a/libs/BeetRoundCore/formats/jsonparser.h b/libs/BeetRoundCore/formats/jsonparser.h index bb920fd..fee12ec 100644 --- a/libs/BeetRoundCore/formats/jsonparser.h +++ b/libs/BeetRoundCore/formats/jsonparser.h @@ -18,6 +18,8 @@ class JsonParser { const QString& rootValueName = ""); static QByteArray itemValuesListToJson(const QList& itemValuesList, const QString& objectName = ""); + static QByteArray ToJsonObject(const QHash& Values, + const QString& objectName = ""); private: explicit JsonParser(); diff --git a/libs/BeetRoundCore/genericcore.cpp b/libs/BeetRoundCore/genericcore.cpp index e4ef5b1..dcc83fb 100644 --- a/libs/BeetRoundCore/genericcore.cpp +++ b/libs/BeetRoundCore/genericcore.cpp @@ -162,14 +162,21 @@ void GenericCore::onBiddingsChanged(int round, QList 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 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(m_modelUndoStack); diff --git a/libs/BeetRoundCore/genericcore.h b/libs/BeetRoundCore/genericcore.h index b87be80..c3e5609 100644 --- a/libs/BeetRoundCore/genericcore.h +++ b/libs/BeetRoundCore/genericcore.h @@ -50,8 +50,9 @@ class GenericCore : public QObject { void onBiddingsChanged(int round, QList 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); diff --git a/libs/BeetRoundCore/model/tablemodel.cpp b/libs/BeetRoundCore/model/tablemodel.cpp index ba7bab9..969d82a 100644 --- a/libs/BeetRoundCore/model/tablemodel.cpp +++ b/libs/BeetRoundCore/model/tablemodel.cpp @@ -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 valueList) { const int nRows = valueList.size(); qDebug() << "Inserting" << nRows << "items..."; @@ -387,7 +395,6 @@ void TableModel::execEditItemData(const int row, const QMap& 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; +} diff --git a/libs/BeetRoundCore/model/tablemodel.h b/libs/BeetRoundCore/model/tablemodel.h index 9d7d03d..2303b4b 100644 --- a/libs/BeetRoundCore/model/tablemodel.h +++ b/libs/BeetRoundCore/model/tablemodel.h @@ -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 diff --git a/libs/BeetRoundCore/network/servercommunicator.cpp b/libs/BeetRoundCore/network/servercommunicator.cpp index f3db0f5..cc332fe 100644 --- a/libs/BeetRoundCore/network/servercommunicator.cpp +++ b/libs/BeetRoundCore/network/servercommunicator.cpp @@ -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 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); +} diff --git a/libs/BeetRoundCore/network/servercommunicator.h b/libs/BeetRoundCore/network/servercommunicator.h index f69275d..1e0bbf6 100644 --- a/libs/BeetRoundCore/network/servercommunicator.h +++ b/libs/BeetRoundCore/network/servercommunicator.h @@ -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 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