Rests model was added
This commit is contained in:
@@ -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
|
||||
|
||||
2
main.cpp
2
main.cpp
@@ -6,6 +6,7 @@
|
||||
#include <QQmlContext>
|
||||
|
||||
#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());
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
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();
|
||||
}
|
||||
23
models/restsmodel.h
Normal file
23
models/restsmodel.h
Normal file
@@ -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<QString, QString>;
|
||||
QMap<Key, QVariantMap> m_rests;
|
||||
};
|
||||
|
||||
#endif // RESTSMODEL_H
|
||||
@@ -17,6 +17,13 @@ public:
|
||||
static void registerModel(BaseModel *model);
|
||||
static BaseModel *model(const QString &name);
|
||||
|
||||
template <class C>
|
||||
static C *get(const QString &name) {
|
||||
C *res = dynamic_cast<C *>(instance()->m_models.value(name));
|
||||
Q_ASSERT(res);
|
||||
return res;
|
||||
}
|
||||
|
||||
private:
|
||||
ModelsRegister() = default;
|
||||
~ModelsRegister() = default;
|
||||
|
||||
@@ -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() },
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -3,8 +3,10 @@
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#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<QString, QVariantMap> m_rests;
|
||||
RestsModel *m_restsModel = ModelsRegister::get<RestsModel>("rests");
|
||||
};
|
||||
|
||||
#endif // RESTSVIEWMODEL_H
|
||||
|
||||
Reference in New Issue
Block a user