On sending (posting) item to the server the generated UUID is added to the local item.

This commit is contained in:
2026-02-02 16:13:44 +01:00
parent bedf8084d3
commit 63fe96fb2e
16 changed files with 145 additions and 33 deletions

View File

@ -29,7 +29,7 @@ QByteArray TableModel::generateExampleItems() {
array.append(itemObject);
}
rootObject.insert(ITEM_KEY_STRING, array);
rootObject.insert(ITEMS_KEY_STRING, array);
doc.setObject(rootObject);
return doc.toJson();
@ -83,6 +83,7 @@ QVariant TableModel::data(const QModelIndex& index, int role) const {
case InfoRole:
case AmountRole:
case FactorRole:
case IdRole:
return m_items.at(row)->data(role);
case ToStringRole:
return m_items.at(row)->toString();
@ -152,7 +153,7 @@ QJsonDocument TableModel::getAllItemsAsJsonDoc() const {
QJsonObject itemObject = item->toJsonObject();
array.append(itemObject);
}
rootObject.insert(ITEM_KEY_STRING, array);
rootObject.insert(ITEMS_KEY_STRING, array);
doc.setObject(rootObject);
return doc;
@ -175,11 +176,37 @@ QList<QStringList> TableModel::getItemsAsStringLists() const {
QByteArray TableModel::jsonDataForServer(const QModelIndex& currentIndex) const {
const QJsonObject itemObject = data(currentIndex, ToJsonRole).toJsonObject();
QJsonObject rootObject;
rootObject.insert("item", itemObject);
rootObject.insert(ITEM_KEY_STRING, itemObject);
const QJsonDocument jsonDoc(rootObject);
return jsonDoc.toJson(QJsonDocument::Compact);
}
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);
/// for each item values:
// NEXT iterate over all value items in the list (or disable updating multiple items for now)
// for (ModelItemValues itemValues : valueList) {
// }
// NEXT encapsulate into updateItem(const ModelItemValues& itemValues)
QModelIndex foundIndex = searchItemIndex(valueList.first());
qDebug() << "Search done!";
if (foundIndex == QModelIndex()) {
const QString errorMessage = "No matching item found!";
qWarning() << errorMessage;
return errorMessage;
} else {
qInfo() << "Item found!";
/// update existing item
QMap<int, QVariant> roles = valueList.first();
setItemData(foundIndex, roles);
/// return status what happened in this function (i. e. "Created x items, updated y items.")
// return data(foundIndex, ToStringRole).toString();
return QString("Item found at row %1.").arg(foundIndex.row());
}
}
bool TableModel::removeRows(int firstRow, int nRows, const QModelIndex& parentIndex) {
if (parentIndex != QModelIndex()) {
qWarning() << "Removing of child rows is not supported yet!";
@ -203,7 +230,7 @@ void TableModel::appendItems(const QByteArray& jsonDoc) { insertItems(-1, jsonDo
void TableModel::insertItems(int startPosition,
const QByteArray& jsonDoc,
const QModelIndex& parentIndex) {
const QList<ModelItemValues> valueList = JsonParser::toItemValuesList(jsonDoc, ITEM_KEY_STRING);
const QList<ModelItemValues> valueList = JsonParser::toItemValuesList(jsonDoc, ITEMS_KEY_STRING);
if (valueList.empty()) {
/// don't try inserting if no values to insert
@ -302,3 +329,60 @@ bool TableModel::isEmptyValueEqualToZero(const int role) const {
return false;
}
}
QModelIndex TableModel::searchItemIndex(const ModelItemValues givenItemValues) const {
// iterate over indexes to search item : see searchItem(...);
// for (const shared_ptr<ModelItem>& item : m_items) {
for (int row = 0; row < rowCount(); ++row) {
qDebug() << "Processing item at row" << row << "...";
QModelIndex itemIndex = index(row, 0);
if (isItemEqualToItemValues(itemIndex, givenItemValues)) {
qInfo() << "Found item at row" << row << "! Returning its index...";
return itemIndex;
}
}
qDebug() << "No matching item found. Returning empty pointer...";
return {};
}
bool TableModel::isItemEqualToItemValues(const QModelIndex& itemIndex,
const ModelItemValues givenItemValues) const {
/// do both have a UUID?
QVariant idOfItem = data(itemIndex, IdRole);
QVariant given = givenItemValues.value(IdRole);
if (idOfItem.isValid() && given.isValid()) {
/// are the UUIDs the same?
if (idOfItem.toString() == given.toString()) {
qInfo() << "UUIDs are the same.";
return true;
} else {
qDebug() << "UUIDs are NOT the same.";
return false;
}
} else {
/// are all other values the same? (for now only USER_FACING_ROLES are checked)
QListIterator<UserRoles> i(USER_FACING_ROLES);
while (i.hasNext()) {
const UserRoles role = i.next();
const QString roleName = ROLE_NAMES.value(role);
const QVariant valueOfItem = data(itemIndex, role);
const QVariant givenValue = givenItemValues.value(role);
if (STRING_ROLES.contains(role)) {
if (valueOfItem.toString() != givenValue.toString()) {
return false;
}
} else if (INT_ROLES.contains(role)) {
if (valueOfItem.toInt() != givenValue.toInt()) {
return false;
}
} else if (DOUBLE_ROLES.contains(role)) {
if (valueOfItem.toDouble() != givenValue.toDouble()) {
return false;
}
} else {
qCritical() << QString("Can't find data type for role %1!!!").arg(role);
}
}
return true;
}
}