Rests model was added

This commit is contained in:
2023-04-13 17:57:27 +02:00
parent fc1d96437f
commit 5264238212
10 changed files with 91 additions and 52 deletions

40
models/restsmodel.cpp Normal file
View File

@@ -0,0 +1,40 @@
#include "restsmodel.h"
RestsModel::RestsModel(QObject *parent) : BaseModel { "rests", parent }
{
connect(this, &BaseModel::dataChanged, this, &RestsModel::clearCache);
}
float RestsModel::productRest(const QString &storeId, const QString &productId)
{
return findRest(storeId, productId).value("rest", 0.0).toFloat();
}
void RestsModel::submitRest(const QString &storeId, const QString &productId, float rest)
{
QVariantMap productRest = findRest(storeId, productId);
productRest["rest"] = rest;
submitItem(productRest);
}
QVariantMap RestsModel::findRest(const QString &storeId, const QString &productId)
{
Key restKey(storeId, productId);
if (m_rests.isEmpty()) {
loadCache();
}
return m_rests.value(restKey, { { "storeId", storeId}, { "productId", productId} });
}
void RestsModel::loadCache()
{
for (const QVariant &var : items()) {
QVariantMap rest = var.toMap();
m_rests[Key(rest.value("storeId").toString(), rest.value("productId").toString())] = rest;
}
}
void RestsModel::clearCache()
{
m_rests.clear();
}