about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2025-07-07 13:58:01 -0500
committerFrederick Muriuki Muriithi2025-07-07 13:58:01 -0500
commit9e130a93838091bb0f23484193045744a354393e (patch)
tree7a93306817601688ae2aca5cbf9ddcf92a5daf25
parentdcd9f3763b0dbb8c2b2d2571566ab4b3a02ccade (diff)
downloadgn-auth-9e130a93838091bb0f23484193045744a354393e.tar.gz
Enable limiting the number of items returned.
-rw-r--r--gn_auth/auth/authorisation/resources/groups/views.py36
1 files changed, 33 insertions, 3 deletions
diff --git a/gn_auth/auth/authorisation/resources/groups/views.py b/gn_auth/auth/authorisation/resources/groups/views.py
index 746e23c..0c1fefd 100644
--- a/gn_auth/auth/authorisation/resources/groups/views.py
+++ b/gn_auth/auth/authorisation/resources/groups/views.py
@@ -35,11 +35,41 @@ groups = Blueprint("groups", __name__)
 @require_oauth("profile group")
 def list_groups():
     """Return the list of groups that exist."""
+    _kwargs = {key: value for key, value in request_json().items()}
+    _total = 0
+    _start = int(_kwargs.get("start", "0"))
+    _length = int(_kwargs.get("length", "0"))
+    def __add_total_group_count__(conn, groups_list):
+        with db.cursor(conn) as cursor:
+            cursor.execute("SELECT COUNT(*) FROM groups")
+            _total = int(cursor.fetchone()["COUNT(*)"])
+
+        return {
+            "total-groups": _total,
+            "groups": groups_list
+        }
+
+    def __slice__(groups_details):
+        _groups = groups_details["groups"]
+        return {
+            **groups_details,
+            "total-filtered": len(_groups),
+            "groups": _groups[
+                _start:_start+(_length if bool(_length) else len(_groups))]
+        }
+
     with db.connection(current_app.config["AUTH_DB"]) as conn:
-        the_groups = all_groups(conn)
+        return jsonify(all_groups(conn).then(
+            partial(__add_total_group_count__, conn)
+        ).then(
+            __slice__
+        ).maybe(
+            {
+                "groups": [],
+                "message": "No groups found!"
+            },
+            lambda _grpdata: _grpdata))
 
-    return jsonify(the_groups.maybe(
-        [], lambda grps: [asdict(grp) for grp in grps]))
 
 @groups.route("/create", methods=["POST"])
 @require_oauth("profile group")