Added support for default sorting. Commodity are sorted by name by default.

master
Josef Rokos 7 years ago
parent b69637165d
commit fe3602fcfc

@ -40,3 +40,8 @@ ISeller *CommodityService::seller()
{
return NULL;
}
QString CommodityService::defaultSort()
{
return "name";
}

@ -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

@ -31,7 +31,7 @@ public:
m_pluginId = pluginId;
}
QList<QSharedPointer<T> > all(const QString &where = "") {
QList<QSharedPointer<T> > all(const QString &where = "", const QString &order = "") {
QList<QSharedPointer<T> > ret;
if (!checkPermission(PERM_READ)) {
@ -47,14 +47,35 @@ public:
try
{
odb::result<T> res;
QString ord = defaultSort();
if (where.isEmpty())
if (!order.isEmpty())
{
ord = order;
}
if (where.isEmpty() && ord.isEmpty())
{
res = db->template query<T>();
}
else
{
res = db->template query<T>(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<T>(cond.toStdString());
}
for (typename odb::result<T>::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

Loading…
Cancel
Save