When receiving online user credentials insert them into the model.

This commit is contained in:
2026-02-19 09:10:06 +01:00
parent faf01d6e15
commit 7d31ac8806
8 changed files with 121 additions and 47 deletions

View File

@ -70,7 +70,25 @@ QByteArray JsonParser::itemValuesListToJson(const QList<ModelItemValues>& itemVa
return jsonDoc.toJson(QJsonDocument::Compact); 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 JsonParser::extractItemArray(const QJsonDocument& doc, const QString& objectName) {
QJsonArray itemArray; QJsonArray itemArray;
@ -83,6 +101,8 @@ QJsonArray JsonParser::extractItemArray(const QJsonDocument& doc, const QString&
return itemArray; return itemArray;
} }
JsonParser::JsonParser() {}
ModelItemValues JsonParser::jsonObjectToItemValues(const QJsonObject& itemJsonObject) { ModelItemValues JsonParser::jsonObjectToItemValues(const QJsonObject& itemJsonObject) {
ModelItemValues values; ModelItemValues values;

View File

@ -18,6 +18,8 @@ class JsonParser {
const QString& rootValueName = ""); const QString& rootValueName = "");
static QByteArray itemValuesListToJson(const QList<ModelItemValues>& itemValuesList, static QByteArray itemValuesListToJson(const QList<ModelItemValues>& itemValuesList,
const QString& objectName = ""); const QString& objectName = "");
static QByteArray ToJsonObject(const QHash<QString, QVariant>& Values,
const QString& objectName = "");
private: private:
explicit JsonParser(); explicit JsonParser();

View File

@ -162,14 +162,21 @@ void GenericCore::onBiddingsChanged(int round, QList<bidding> biddings) {
void GenericCore::onCreateOnlineAccountTriggered(const QString& mailAddress) { void GenericCore::onCreateOnlineAccountTriggered(const QString& mailAddress) {
qInfo() << "Creating online account for:" << mailAddress; qInfo() << "Creating online account for:" << mailAddress;
// QJsonDocument onlineCredentialsDoc = m_mainModel->getOnlineCredentialsAsJsonDoc(mailAddress); QHash<QString, QVariant> hash;
// emit createOnlineUser(onlineCredentialsDoc.toJson()); hash.insert("email", mailAddress);
emit sendPostRequest(RegisterUser, 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() { void GenericCore::setupModels() {
m_mainModel = make_shared<TableModel>(m_modelUndoStack); m_mainModel = make_shared<TableModel>(m_modelUndoStack);

View File

@ -50,8 +50,9 @@ class GenericCore : public QObject {
void onBiddingsChanged(int round, QList<bidding> biddings); void onBiddingsChanged(int round, QList<bidding> biddings);
void onCreateOnlineAccountTriggered(const QString& mailAddress); void onCreateOnlineAccountTriggered(const QString& mailAddress);
// void onOnlineUserCreatedReceived(const QJsonDocument& jsonDoc); void onOnlineUserAccountReceived(const QString mailAddress,
// void onOnlineUserExistedReceived(const QJsonDocument jsonDoc); const QString uuid,
const QString accessToken);
signals: signals:
void displayStatusMessage(QString message); void displayStatusMessage(QString message);
@ -59,7 +60,7 @@ class GenericCore : public QObject {
/// *** server communication *** /// *** server communication ***
/// request signals /// request signals
void sendGetRequest(GetRequestTypes type, QVariant data = QVariant()); void sendGetRequest(GetRequestTypes type, QVariant data = QVariant());
void sendPostRequest(PostRequestTypes type, QVariant data = QVariant()); void sendPostRequest(PostRequestTypes type, QByteArray data);
/// response signals /// response signals
void currentBiddingRoundChanged(int round, bool isRunning); void currentBiddingRoundChanged(int round, bool isRunning);

View File

@ -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) { bool TableModel::removeRows(int firstRow, int nRows, const QModelIndex& parentIndex) {
if (parentIndex != QModelIndex()) { if (parentIndex != QModelIndex()) {
qWarning() << "Removing of child rows is not supported yet!"; qWarning() << "Removing of child rows is not supported yet!";
@ -282,29 +290,6 @@ void TableModel::insertItems(int startPosition,
m_undoStack->push(insertCommand); 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 TableModel::nExpectedBiddings() const {
int result = 0; int result = 0;
for (auto i = m_items.begin(), end = m_items.end(); i != end; ++i) { for (auto i = m_items.begin(), end = m_items.end(); i != end; ++i) {
@ -352,6 +337,29 @@ qreal TableModel::biddingAverage3() const {
return averageBidding; 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) { void TableModel::execInsertItems(const int firstRow, const QList<ModelItemValues> valueList) {
const int nRows = valueList.size(); const int nRows = valueList.size();
qDebug() << "Inserting" << nRows << "items..."; qDebug() << "Inserting" << nRows << "items...";
@ -387,7 +395,6 @@ void TableModel::execEditItemData(const int row, const QMap<int, QVariant>& chan
roles.insert(0, Qt::DisplayRole); roles.insert(0, Qt::DisplayRole);
emit dataChanged(firstIndex, lastIndex, roles.toVector()); emit dataChanged(firstIndex, lastIndex, roles.toVector());
// NEXT check which roles are changed & trigger only changed properties
if (roles.contains(BiddingTypeRole) || roles.contains(ShareAmountRole) || if (roles.contains(BiddingTypeRole) || roles.contains(ShareAmountRole) ||
roles.contains(ShareTypeRole)) { roles.contains(ShareTypeRole)) {
emit nExpectedBiddingsChanged(); emit nExpectedBiddingsChanged();
@ -576,3 +583,29 @@ qreal TableModel::totalSharesWithBiddings(const UserRoles biddingRole) const {
qInfo() << "Biddings exist for " << result << "shares!"; qInfo() << "Biddings exist for " << result << "shares!";
return result; 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;
}

View File

@ -44,6 +44,7 @@ class TableModel : public QAbstractTableModel {
QString updateItemsFromJson(const QByteArray& jsonData); QString updateItemsFromJson(const QByteArray& jsonData);
bool updateItem(const ModelItemValues& itemValues); bool updateItem(const ModelItemValues& itemValues);
void setOnlineCredentials(const QString& mail, const QString& uuid, const QString& token);
public slots: public slots:
// bool insertRows(int position, int rows, const QModelIndex& parentIndex = QModelIndex()) // bool insertRows(int position, int rows, const QModelIndex& parentIndex = QModelIndex())
// override; // override;
@ -105,6 +106,8 @@ class TableModel : public QAbstractTableModel {
int biddingSum(const UserRoles biddingRole) const; int biddingSum(const UserRoles biddingRole) const;
qreal averageBiddingAmount(const UserRoles biddingRole) const; qreal averageBiddingAmount(const UserRoles biddingRole) const;
qreal totalSharesWithBiddings(const UserRoles biddingRole) const; qreal totalSharesWithBiddings(const UserRoles biddingRole) const;
QModelIndex getIndexByRoleValue(const QString& valueString, const int role) const;
}; };
#endif // TABLEMODEL_H #endif // TABLEMODEL_H

View File

@ -23,6 +23,8 @@ ServerCommunicator::ServerCommunicator(GenericCore* core)
connect(m_core, &GenericCore::sendPostRequest, this, connect(m_core, &GenericCore::sendPostRequest, this,
&ServerCommunicator::onSendPostRequestTriggered); &ServerCommunicator::onSendPostRequestTriggered);
connect(this, &ServerCommunicator::biddingsChanged, m_core, &GenericCore::onBiddingsChanged); connect(this, &ServerCommunicator::biddingsChanged, m_core, &GenericCore::onBiddingsChanged);
connect(this, &ServerCommunicator::onlineUserAccountReceived, m_core,
&GenericCore::onOnlineUserAccountReceived);
} }
bool ServerCommunicator::sslSupported() const { bool ServerCommunicator::sslSupported() const {
@ -133,7 +135,8 @@ void ServerCommunicator::onGetReplyFailure(const GetRequestTypes type, const QSt
m_core->displayStatusMessage(message); m_core->displayStatusMessage(message);
} }
void ServerCommunicator::onSendPostRequestTriggered(const PostRequestTypes type, QVariant data) { void ServerCommunicator::onSendPostRequestTriggered(const PostRequestTypes type,
const QByteArray data) {
QString path; QString path;
switch (type) { switch (type) {
case RegisterUser: case RegisterUser:
@ -152,16 +155,7 @@ void ServerCommunicator::onSendPostRequestTriggered(const PostRequestTypes type,
const QNetworkRequest request = m_serviceApi->createRequest(path); const QNetworkRequest request = m_serviceApi->createRequest(path);
QJsonDocument doc = QJsonDocument(); m_restManager->post(request, data, this, [this, type](QRestReply& reply) {
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()) { if (reply.isSuccess()) {
int statusCode = reply.httpStatus(); int statusCode = reply.httpStatus();
qInfo() << "Request successful. Status code:" << statusCode; qInfo() << "Request successful. Status code:" << statusCode;
@ -187,7 +181,7 @@ void ServerCommunicator::onPostReplySuccessful(const PostRequestTypes type,
switch (type) { switch (type) {
case RegisterUser: case RegisterUser:
qInfo() << "Register user successful:" << type; qInfo() << "Register user successful:" << type;
m_core->displayStatusMessage(doc.toJson()); onlineUserAccountReply(doc);
break; break;
default: default:
qWarning() << "Can't match request type:" << type; qWarning() << "Can't match request type:" << type;
@ -225,6 +219,16 @@ void ServerCommunicator::currentBiddingsReply(const QJsonDocument jsonDoc) {
.depotWishOne = "one", .depotWishOne = "one",
.depotWishTwo = "two"}; .depotWishTwo = "two"};
const QList<bidding> biddings{dummyBidding}; const QList<bidding> biddings{dummyBidding};
emit biddingsChanged(roundNumber, biddings); 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);
}

View File

@ -29,7 +29,7 @@ class ServerCommunicator : public QObject {
void onGetReplySuccessful(const GetRequestTypes type, const QJsonDocument doc); void onGetReplySuccessful(const GetRequestTypes type, const QJsonDocument doc);
void onGetReplyFailure(const GetRequestTypes type, const QString errorString); 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 onPostReplySuccessful(const PostRequestTypes type, const QJsonDocument doc);
void onPostReplyFailure(const PostRequestTypes type, const QString errorString); void onPostReplyFailure(const PostRequestTypes type, const QString errorString);
@ -45,6 +45,9 @@ class ServerCommunicator : public QObject {
void currentBiddingRoundChanged(int round, bool isRunning); void currentBiddingRoundChanged(int round, bool isRunning);
void biddingsChanged(int round, QList<bidding> biddings); void biddingsChanged(int round, QList<bidding> biddings);
void onlineUserAccountReceived(const QString mailAddress,
const QString uuid,
const QString accessToken);
private: private:
GenericCore* m_core = nullptr; GenericCore* m_core = nullptr;
@ -60,6 +63,7 @@ class ServerCommunicator : public QObject {
/// reply parser /// reply parser
void currentBiddingRoundChangedReply(const QJsonDocument jsonDoc); void currentBiddingRoundChangedReply(const QJsonDocument jsonDoc);
void currentBiddingsReply(const QJsonDocument jsonDoc); void currentBiddingsReply(const QJsonDocument jsonDoc);
void onlineUserAccountReply(const QJsonDocument jsonDoc);
}; };
#endif // SERVERCOMMUNICATOR_H #endif // SERVERCOMMUNICATOR_H