[pricelist] add serverside filtering for getDrinks
This commit is contained in:
parent
26a00ed6a6
commit
8fb74358e7
|
@ -218,6 +218,8 @@ def get_drinks(identifier=None):
|
||||||
else:
|
else:
|
||||||
limit = request.args.get("limit")
|
limit = request.args.get("limit")
|
||||||
offset = request.args.get("offset")
|
offset = request.args.get("offset")
|
||||||
|
search_name = request.args.get("search_name")
|
||||||
|
search_key = request.args.get("search_key")
|
||||||
try:
|
try:
|
||||||
if limit is not None:
|
if limit is not None:
|
||||||
limit = int(limit)
|
limit = int(limit)
|
||||||
|
@ -225,7 +227,9 @@ def get_drinks(identifier=None):
|
||||||
offset = int(offset)
|
offset = int(offset)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise BadRequest
|
raise BadRequest
|
||||||
drinks, count = pricelist_controller.get_drinks(public=public, limit=limit, offset=offset)
|
drinks, count = pricelist_controller.get_drinks(
|
||||||
|
public=public, limit=limit, offset=offset, search_name=search_name, search_key=search_key
|
||||||
|
)
|
||||||
logger.debug(f"GET drink {drinks}, {count}")
|
logger.debug(f"GET drink {drinks}, {count}")
|
||||||
# return jsonify({"drinks": drinks, "count": count})
|
# return jsonify({"drinks": drinks, "count": count})
|
||||||
return jsonify({"drinks": drinks, "count": count})
|
return jsonify({"drinks": drinks, "count": count})
|
||||||
|
|
|
@ -131,12 +131,28 @@ def _create_public_drink(drink):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def get_drinks(name=None, public=False, limit=None, offset=None):
|
def get_drinks(name=None, public=False, limit=None, offset=None, search_name=None, search_key=None):
|
||||||
count = None
|
count = None
|
||||||
if name:
|
if name:
|
||||||
query = Drink.query.filter(Drink.name.contains(name))
|
query = Drink.query.filter(Drink.name.contains(name))
|
||||||
else:
|
else:
|
||||||
query = Drink.query
|
query = Drink.query
|
||||||
|
if search_name:
|
||||||
|
if search_key == "name":
|
||||||
|
query = query.filter(Drink.name.contains(search_name))
|
||||||
|
elif search_key == "article_id":
|
||||||
|
query = query.filter(Drink.article_id.contains(search_name))
|
||||||
|
elif search_key == "drink_type":
|
||||||
|
query = query.filter(Drink.type.has(DrinkType.name.contains(search_name)))
|
||||||
|
elif search_key == "tags":
|
||||||
|
query = query.filter(Drink.tags.any(Tag.name.contains(search_name)))
|
||||||
|
else:
|
||||||
|
query = query.filter(
|
||||||
|
(Drink.name.contains(search_name))
|
||||||
|
| (Drink.article_id.contains(search_name))
|
||||||
|
| (Drink.type.has(DrinkType.name.contains(search_name)))
|
||||||
|
| (Drink.tags.any(Tag.name.contains(search_name)))
|
||||||
|
)
|
||||||
|
|
||||||
if limit is not None:
|
if limit is not None:
|
||||||
count = query.count()
|
count = query.count()
|
||||||
|
|
Loading…
Reference in New Issue