[Plugin] Balance: Return Transaction count when using limit parameter for pagination

This commit is contained in:
Ferdinand Thiessen 2021-01-29 20:08:34 +01:00
parent 2bbc4898e7
commit d1157f0f37
2 changed files with 13 additions and 9 deletions

View File

@ -164,6 +164,7 @@ def get_balance(userid, current_session: Session):
@login_required(permission=permissions.SHOW)
def get_transactions(userid, current_session: Session):
"""Get transactions of user, optionally filtered
Returns also count of transactions if limit is set (e.g. just count with limit = 0)
Route: ``/users/<userid>/balance/transactions`` | Method: ``GET``
@ -174,7 +175,7 @@ def get_transactions(userid, current_session: Session):
current_session: Session sent with Authorization Header
Returns:
JSON encoded list of transactions or HTTP error
JSON Object {transactions: Transaction[], count?: number} or HTTP error
"""
if userid != current_session._user.userid and not current_session._user.has_permission(permissions.SHOW_OTHER):
raise Forbidden
@ -191,15 +192,15 @@ def get_transactions(userid, current_session: Session):
limit = request.args.get("limit")
offset = request.args.get("offset")
try:
if limit:
if limit is not None:
limit = int(limit)
if offset:
if offset is not None:
offset = int(offset)
except ValueError:
raise BadRequest
transactions = balance_controller.get_transactions(user, start, end, limit, offset)
return jsonify(transactions)
transactions, count = balance_controller.get_transactions(user, start, end, limit, offset)
return {"transactions": transactions, "count": count}
@balance_bp.route("/users/<userid>/balance", methods=["PUT"])

View File

@ -101,17 +101,20 @@ def get_transaction(transaction_id) -> Transaction:
def get_transactions(user, start=None, end=None, limit=None, offset=None):
count = None
query = Transaction.query.filter((Transaction.sender_ == user) | (Transaction.receiver_ == user))
if start:
query = query.filter(start <= Transaction.time)
if end:
query = query.filter(Transaction.time <= end)
query = query.order_by(Transaction.time.desc())
query = query.order_by(Transaction.time)
if limit is not None:
count = query.count()
query = query.limit(limit)
if offset is not None:
query = query.offset(offset)
if limit is not None:
query = query.limit(limit)
return query.all()
return query.all(), count
def reverse_transaction(transaction: Transaction, author: User):