From 5264238212e5f4f661cdef4dcc917a603f77d28b Mon Sep 17 00:00:00 2001 From: "Denis V. Dedkov" Date: Thu, 13 Apr 2023 17:57:27 +0200 Subject: [PATCH] Rests model was added --- CMakeLists.txt | 1 + main.cpp | 2 ++ models/basemodel.cpp | 23 +++++++++--------- models/basemodel.h | 3 +-- models/restsmodel.cpp | 40 ++++++++++++++++++++++++++++++++ models/restsmodel.h | 23 ++++++++++++++++++ services/modelsregister.h | 7 ++++++ viewmodels/productsviewmodel.cpp | 2 +- viewmodels/restsviewmodel.cpp | 35 ++-------------------------- viewmodels/restsviewmodel.h | 7 +++--- 10 files changed, 91 insertions(+), 52 deletions(-) create mode 100644 models/restsmodel.cpp create mode 100644 models/restsmodel.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 8155fd2..b232193 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,7 @@ set(PROJECT_SOURCES qml/qml.qrc models/basemodel.h models/basemodel.cpp models/usersmodel.h models/usersmodel.cpp + models/restsmodel.h models/restsmodel.cpp viewmodels/usersviewmodel.h viewmodels/usersviewmodel.cpp viewmodels/storesviewmodel.h viewmodels/storesviewmodel.cpp viewmodels/productsviewmodel.h viewmodels/productsviewmodel.cpp diff --git a/main.cpp b/main.cpp index aee057f..19b91af 100644 --- a/main.cpp +++ b/main.cpp @@ -6,6 +6,7 @@ #include #include "models/usersmodel.h" +#include "models/restsmodel.h" #include "viewmodels/usersviewmodel.h" #include "viewmodels/productsviewmodel.h" @@ -45,6 +46,7 @@ int main(int argc, char *argv[]) engine.addImportPath("qrc:/"); ModelsRegister::registerModel(new UsersModel()); + ModelsRegister::registerModel(new RestsModel()); engine.rootContext()->setContextProperty("beerService", BeerService::instance()); diff --git a/models/basemodel.cpp b/models/basemodel.cpp index 14a9f68..fd1e23f 100644 --- a/models/basemodel.cpp +++ b/models/basemodel.cpp @@ -36,9 +36,18 @@ QVariant BaseModel::itemProperty(const QString &itemId, const QString &propertyN return item(itemId).value(propertyName, def); } -void BaseModel::addItem(const QVariantMap &item) const +void BaseModel::submitItem(const QVariantMap &item) const { - service()->sendCommand(entity(), BeerService::ActionAdd, item); + QString itemId = item.value("id").toString(); + if (itemId.isEmpty()) { + service()->sendCommand(entity(), BeerService::ActionAdd, item); + } else { + QVariantMap modItem = this->item(itemId); + for (auto it = item.constBegin(); it != item.constEnd(); ++it) { + modItem[it.key()] = it.value(); + } + service()->sendCommand(entity(), BeerService::ActionModify, modItem); + } } void BaseModel::deleteItem(const QString &itemId) const @@ -46,16 +55,6 @@ void BaseModel::deleteItem(const QString &itemId) const service()->sendCommand(entity(), BeerService::ActionDelete, QVariantMap { { "id", itemId } }); } -void BaseModel::modifyItem(const QString &itemId, const QVariantMap &properties) const -{ - QVariantMap item = this->item(itemId); - for (auto it = properties.constBegin(); it != properties.constEnd(); ++it) { - item[it.key()] = it.value(); - } - - service()->sendCommand(entity(), BeerService::ActionModify, item); -} - void BaseModel::created(const QVariant &data) { modified(data); diff --git a/models/basemodel.h b/models/basemodel.h index 0c50afb..54a727d 100644 --- a/models/basemodel.h +++ b/models/basemodel.h @@ -21,9 +21,8 @@ public: QVariantMap item(const QString &itemId) const; QVariant itemProperty(const QString &itemId, const QString &propertyName, const QVariant &def = QVariant{}) const; - void addItem(const QVariantMap &item) const; + void submitItem(const QVariantMap &item) const; void deleteItem(const QString &itemId) const; - void modifyItem(const QString &itemId, const QVariantMap &properties) const; public slots: void created(const QVariant &data); diff --git a/models/restsmodel.cpp b/models/restsmodel.cpp new file mode 100644 index 0000000..075e12a --- /dev/null +++ b/models/restsmodel.cpp @@ -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(); +} diff --git a/models/restsmodel.h b/models/restsmodel.h new file mode 100644 index 0000000..485060b --- /dev/null +++ b/models/restsmodel.h @@ -0,0 +1,23 @@ +#ifndef RESTSMODEL_H +#define RESTSMODEL_H + +#include "models/basemodel.h" + +class RestsModel : public BaseModel +{ +public: + explicit RestsModel(QObject *parent = nullptr); + + float productRest(const QString &storeId, const QString &productId); + void submitRest(const QString &storeId, const QString &productId, float rest); + +private: + QVariantMap findRest(const QString &storeId, const QString &productId); + void loadCache(); + void clearCache(); + + using Key = QPair; + QMap m_rests; +}; + +#endif // RESTSMODEL_H diff --git a/services/modelsregister.h b/services/modelsregister.h index 3c23dcc..514c7da 100644 --- a/services/modelsregister.h +++ b/services/modelsregister.h @@ -17,6 +17,13 @@ public: static void registerModel(BaseModel *model); static BaseModel *model(const QString &name); + template + static C *get(const QString &name) { + C *res = dynamic_cast(instance()->m_models.value(name)); + Q_ASSERT(res); + return res; + } + private: ModelsRegister() = default; ~ModelsRegister() = default; diff --git a/viewmodels/productsviewmodel.cpp b/viewmodels/productsviewmodel.cpp index cfb291c..f826945 100644 --- a/viewmodels/productsviewmodel.cpp +++ b/viewmodels/productsviewmodel.cpp @@ -60,7 +60,7 @@ void ProductsViewModel::setOrderValue(const QString &productId, float value) void ProductsViewModel::submitOrder() { BaseModel *ordersModel = ModelsRegister::model("orders"); - ordersModel->addItem({ + ordersModel->submitItem({ { "userId", settings()->selectedUserId() }, { "storeId", settings()->selectedStoreId() }, { "products", m_order.values() }, diff --git a/viewmodels/restsviewmodel.cpp b/viewmodels/restsviewmodel.cpp index acd1c75..b0c9d97 100644 --- a/viewmodels/restsviewmodel.cpp +++ b/viewmodels/restsviewmodel.cpp @@ -1,16 +1,12 @@ #include "restsviewmodel.h" -#include "models/basemodel.h" #include "services/settingsservice.h" RestsViewModel::RestsViewModel(QObject *parent) : QObject{parent} { - connect(m_restsModel, &BaseModel::dataChanged, this, &RestsViewModel::reloadRests); connect(m_productsModel, &BaseModel::dataChanged, this, &RestsViewModel::productsChanged); connect(settings(), &SettingsService::selectedStoreIdChanged, this, &RestsViewModel::productsChanged); - - reloadRests(); } QVariantList RestsViewModel::products() const @@ -21,11 +17,10 @@ QVariantList RestsViewModel::products() const const QVariantList products = m_productsModel->items(); for (const QVariant &product : products) { QString productId = product.toMap().value("id").toString(); - QVariantMap rest = m_rests.value(storeId).value(productId).toMap(); res << QVariantMap { { "productId", productId }, { "title", m_productsModel->itemProperty(productId, "name").toString() }, - { "rest", rest.value("rest", 0.0).toFloat() } + { "rest", m_restsModel->productRest(storeId, productId) } }; } @@ -34,36 +29,10 @@ QVariantList RestsViewModel::products() const void RestsViewModel::setProductRest(const QString &productId, float rest) { - QString storeId = settings()->selectedStoreId(); - QVariantMap productRest = m_rests.value(storeId).value(productId).toMap(); - productRest["rest"] = rest; - productRest["storeId"] = storeId; - productRest["productId"] = productId; - QString restId = productRest.value("id").toString(); - if (restId.isEmpty()) { - m_restsModel->addItem(productRest); - } else { - m_restsModel->modifyItem(restId, productRest); - } + m_restsModel->submitRest(settings()->selectedStoreId(), productId, rest); } SettingsService *RestsViewModel::settings() const { return SettingsService::instance(); } - -void RestsViewModel::reloadRests() -{ - m_rests.clear(); - - const QVariantList rests = m_restsModel->items(); - for (const QVariant &varRest : rests) { - QVariantMap rest = varRest.toMap(); - QString storeId = rest.value("storeId").toString(); - QVariantMap storeRests = m_rests.value(storeId); - storeRests[rest.value("productId").toString()] = rest; - m_rests[storeId] = storeRests; - } - - emit productsChanged(); -} diff --git a/viewmodels/restsviewmodel.h b/viewmodels/restsviewmodel.h index 953e722..cb2e2a6 100644 --- a/viewmodels/restsviewmodel.h +++ b/viewmodels/restsviewmodel.h @@ -3,8 +3,10 @@ #include +#include "models/restsmodel.h" #include "services/modelsregister.h" +class RestsModel; class SettingsService; class RestsViewModel : public QObject { @@ -24,12 +26,9 @@ signals: private: SettingsService *settings() const; - void reloadRests(); BaseModel *m_productsModel = ModelsRegister::model("products"); - BaseModel *m_restsModel = ModelsRegister::model("rests"); - - QMap m_rests; + RestsModel *m_restsModel = ModelsRegister::get("rests"); }; #endif // RESTSVIEWMODEL_H