[pricelist] add serverside sorting for pricelist, sorting by name for drinks

This commit is contained in:
Tim Gröger 2021-11-15 09:19:50 +01:00
parent dba60fdab8
commit 57a03a80cc
2 changed files with 37 additions and 4 deletions

View File

@ -269,6 +269,8 @@ def get_pricelist():
search_key = request.args.get("search_key") search_key = request.args.get("search_key")
ingredient = request.args.get("ingredient", type=bool) ingredient = request.args.get("ingredient", type=bool)
receipt = request.args.get("receipt", type=bool) receipt = request.args.get("receipt", type=bool)
descending = request.args.get("descending", type=bool)
sortBy = request.args.get("sortBy")
try: try:
if limit is not None: if limit is not None:
limit = int(limit) limit = int(limit)
@ -278,10 +280,18 @@ def get_pricelist():
ingredient = bool(ingredient) ingredient = bool(ingredient)
if receipt is not None: if receipt is not None:
receipt = bool(receipt) receipt = bool(receipt)
if descending is not None:
descending = bool(descending)
except ValueError: except ValueError:
raise BadRequest raise BadRequest
pricelist, count = pricelist_controller.get_pricelist( pricelist, count = pricelist_controller.get_pricelist(
public=public, limit=limit, offset=offset, search_name=search_name, search_key=search_key public=public,
limit=limit,
offset=offset,
search_name=search_name,
search_key=search_key,
descending=descending,
sortBy=sortBy,
) )
logger.debug(f"GET pricelist {pricelist}, {count}") logger.debug(f"GET pricelist {pricelist}, {count}")
return jsonify({"pricelist": pricelist, "count": count}) return jsonify({"pricelist": pricelist, "count": count})

View File

@ -161,6 +161,7 @@ def get_drinks(
| (Drink.type.has(DrinkType.name.contains(search_name))) | (Drink.type.has(DrinkType.name.contains(search_name)))
| (Drink.tags.any(Tag.name.contains(search_name))) | (Drink.tags.any(Tag.name.contains(search_name)))
) )
query = query.order_by(Drink.name.asc())
if limit is not None: if limit is not None:
count = query.count() count = query.count()
@ -176,11 +177,13 @@ def get_drinks(
return drinks, count return drinks, count
def get_pricelist(public=False, limit=None, offset=None, search_name=None, search_key=None): def get_pricelist(
public=False, limit=None, offset=None, search_name=None, search_key=None, sortBy=None, descending=False
):
count = None count = None
query = DrinkPrice.query query = DrinkPrice.query
if public: if public:
query.filter(DrinkPrice.public) query = query.filter(DrinkPrice.public)
if search_name: if search_name:
if search_key == "name": if search_key == "name":
query = query.filter(DrinkPrice._volume.has(DrinkPriceVolume._drink.has(Drink.name.contains(search_name)))) query = query.filter(DrinkPrice._volume.has(DrinkPriceVolume._drink.has(Drink.name.contains(search_name))))
@ -217,16 +220,36 @@ def get_pricelist(public=False, limit=None, offset=None, search_name=None, searc
) )
| ( | (
DrinkPrice._volume.has( DrinkPrice._volume.has(
DrinkPriceVolume._drink.has(Drink.tags.any(Tag.name.conaitns(search_name))) DrinkPriceVolume._drink.has(Drink.tags.any(Tag.name.contains(search_name)))
) )
) )
| (DrinkPrice.description.contains(search_name)) | (DrinkPrice.description.contains(search_name))
) )
if sortBy == "type":
query = (
query.join(DrinkPrice._volume)
.join(DrinkPriceVolume._drink)
.join(Drink.type)
.order_by(DrinkType.name.desc() if descending else DrinkType.name.asc())
)
elif sortBy == "volume":
query = query.join(DrinkPrice._volume).order_by(
DrinkPriceVolume.volume.desc() if descending else DrinkPriceVolume.volume.asc()
)
elif sortBy == "price":
query = query.order_by(DrinkPrice.price.desc() if descending else DrinkPrice.price.asc())
else:
query = (
query.join(DrinkPrice._volume)
.join(DrinkPriceVolume._drink)
.order_by(Drink.name.desc() if descending else Drink.name.asc())
)
if limit is not None: if limit is not None:
count = query.count() count = query.count()
query = query.limit(limit) query = query.limit(limit)
if offset is not None: if offset is not None:
query = query.offset(offset) query = query.offset(offset)
prices = query.all() prices = query.all()
for price in prices: for price in prices:
price._volume.drink = price._volume._drink price._volume.drink = price._volume._drink