Split TableModel::updateItemsFromJson(...) into two functions and added support for multiple items in JSON data.
This commit is contained in:
@ -158,9 +158,7 @@ void GenericCore::onItemsFetchFailure(const QString errorString) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void GenericCore::onPostRequestSuccessful(const QByteArray responseData) {
|
void GenericCore::onPostRequestSuccessful(const QByteArray responseData) {
|
||||||
// NEXT search local item and set server generated UUID;
|
|
||||||
const QString message = m_mainModel->updateItemsFromJson(responseData);
|
const QString message = m_mainModel->updateItemsFromJson(responseData);
|
||||||
|
|
||||||
emit displayStatusMessage(message);
|
emit displayStatusMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -182,27 +182,30 @@ QByteArray TableModel::jsonDataForServer(const QModelIndex& currentIndex) const
|
|||||||
}
|
}
|
||||||
|
|
||||||
QString TableModel::updateItemsFromJson(const QByteArray& jsonData) {
|
QString TableModel::updateItemsFromJson(const QByteArray& jsonData) {
|
||||||
/// convert JSON data into a list of item values
|
|
||||||
const QList<ModelItemValues> valueList = JsonParser::toItemValuesList(jsonData, ITEM_KEY_STRING);
|
const QList<ModelItemValues> valueList = JsonParser::toItemValuesList(jsonData, ITEM_KEY_STRING);
|
||||||
/// for each item values:
|
int nChangedItems = 0;
|
||||||
// NEXT iterate over all value items in the list (or disable updating multiple items for now)
|
for (const ModelItemValues& itemValues : valueList) {
|
||||||
// for (ModelItemValues itemValues : valueList) {
|
bool updateHappened = updateItem(itemValues);
|
||||||
// }
|
if (updateHappened) {
|
||||||
// NEXT encapsulate into updateItem(const ModelItemValues& itemValues)
|
nChangedItems++;
|
||||||
QModelIndex foundIndex = searchItemIndex(valueList.first());
|
}
|
||||||
|
}
|
||||||
|
return QString("Found and updated %1 item(s).").arg(nChangedItems);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TableModel::updateItem(const ModelItemValues& itemValues) {
|
||||||
|
QModelIndex foundIndex = searchItemIndex(itemValues);
|
||||||
|
|
||||||
qDebug() << "Search done!";
|
qDebug() << "Search done!";
|
||||||
if (foundIndex == QModelIndex()) {
|
if (foundIndex == QModelIndex()) {
|
||||||
const QString errorMessage = "No matching item found!";
|
const QString errorMessage = "No matching item found!";
|
||||||
qWarning() << errorMessage;
|
qWarning() << errorMessage;
|
||||||
return errorMessage;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
qInfo() << "Item found!";
|
qInfo() << "Item found!";
|
||||||
/// update existing item
|
/// update existing item
|
||||||
QMap<int, QVariant> roles = valueList.first();
|
setItemData(foundIndex, itemValues);
|
||||||
setItemData(foundIndex, roles);
|
return true;
|
||||||
/// return status what happened in this function (i. e. "Created x items, updated y items.")
|
|
||||||
return QString("Item found at row %1.").arg(foundIndex.row());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -41,6 +41,7 @@ class TableModel : public QAbstractTableModel {
|
|||||||
QByteArray jsonDataForServer(const QModelIndex& currentIndex) const;
|
QByteArray jsonDataForServer(const QModelIndex& currentIndex) const;
|
||||||
|
|
||||||
QString updateItemsFromJson(const QByteArray& jsonData);
|
QString updateItemsFromJson(const QByteArray& jsonData);
|
||||||
|
bool updateItem(const ModelItemValues& itemValues);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
// bool insertRows(int position, int rows, const QModelIndex& parentIndex = QModelIndex())
|
// bool insertRows(int position, int rows, const QModelIndex& parentIndex = QModelIndex())
|
||||||
|
|||||||
Reference in New Issue
Block a user