Rests model was added
This commit is contained in:
40
models/restsmodel.cpp
Normal file
40
models/restsmodel.cpp
Normal 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();
|
||||
}
|
||||
Reference in New Issue
Block a user