diff --git a/commodity/commodityservice.cpp b/commodity/commodityservice.cpp index 507d6db..8f8d903 100644 --- a/commodity/commodityservice.cpp +++ b/commodity/commodityservice.cpp @@ -40,3 +40,8 @@ ISeller *CommodityService::seller() { return NULL; } + +QString CommodityService::defaultSort() +{ + return "name"; +} diff --git a/commodity/commodityservice.h b/commodity/commodityservice.h index 028e98b..742ea3e 100644 --- a/commodity/commodityservice.h +++ b/commodity/commodityservice.h @@ -16,6 +16,7 @@ public: void addedToVoucher(int itemId, int countAdded) override; virtual ShopItemPtr shopItem(int itemId) override; ISeller *seller() override; + QString defaultSort() override; }; #endif // COMMODITYSERVICE_H diff --git a/core/service.h b/core/service.h index 7586464..893c6e4 100644 --- a/core/service.h +++ b/core/service.h @@ -31,7 +31,7 @@ public: m_pluginId = pluginId; } - QList > all(const QString &where = "") { + QList > all(const QString &where = "", const QString &order = "") { QList > ret; if (!checkPermission(PERM_READ)) { @@ -47,14 +47,35 @@ public: try { odb::result res; + QString ord = defaultSort(); - if (where.isEmpty()) + if (!order.isEmpty()) + { + ord = order; + } + + if (where.isEmpty() && ord.isEmpty()) { res = db->template query(); } else { - res = db->template query(where.toStdString()); + QString cond; + + if (where.isEmpty()) + { + cond = "1 ORDER BY " + ord; + } + else + { + cond = where; + if (!ord.isEmpty()) + { + cond += "ORDER BY " + ord; + } + } + + res = db->template query(cond.toStdString()); } for (typename odb::result::iterator it = res.begin(); it != res.end(); it++) { @@ -259,6 +280,10 @@ protected: obj->setProperty("updated", QDateTime::currentDateTime()); } } + + virtual QString defaultSort() { + return ""; + } }; #endif // SERVICE_H