aboutsummaryrefslogtreecommitdiffstats
path: root/backend/lib/ACMS/API
diff options
context:
space:
mode:
authorLibravatar Alexander Foremny <aforemny@posteo.de>2024-12-18 19:11:03 +0100
committerLibravatar Alexander Foremny <aforemny@posteo.de>2025-02-19 18:44:43 +0100
commit9b0adc976101bc4f375b05cc475478187c595714 (patch)
tree67dde004c05d5bcf380c72f0134ac673f6a4f027 /backend/lib/ACMS/API
parent04a9bbfab5536791f4850903ebb339c846341184 (diff)
add pagination to rest api
Diffstat (limited to 'backend/lib/ACMS/API')
-rw-r--r--backend/lib/ACMS/API/REST/Collection/Paginated.hs32
1 files changed, 32 insertions, 0 deletions
diff --git a/backend/lib/ACMS/API/REST/Collection/Paginated.hs b/backend/lib/ACMS/API/REST/Collection/Paginated.hs
new file mode 100644
index 0000000..ad98888
--- /dev/null
+++ b/backend/lib/ACMS/API/REST/Collection/Paginated.hs
@@ -0,0 +1,32 @@
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE RecordWildCards #-}
+
+module ACMS.API.REST.Collection.Paginated where
+
+import ACMS.API.REST (APIMonad, fetch, restRequest)
+import Collection
+import Data.Aeson qualified as A
+import Data.Function ((&))
+import GHC.Generics (Generic)
+import Text.Printf (printf)
+
+data Pagination = Pagination
+ { limit :: Int,
+ offset :: Int
+ }
+
+data Paginated a = Paginated
+ { count :: Int,
+ data_ :: [a]
+ }
+ deriving (Eq, Show, Generic)
+
+instance (A.FromJSON a) => A.FromJSON (Paginated a)
+
+instance (A.ToJSON a) => A.ToJSON (Paginated a)
+
+list :: (APIMonad m) => Pagination -> Collection -> m (Paginated A.Object)
+list p c =
+ restRequest (printf "/collection/%s/paginated/%d/%d" c.name p.limit p.offset)
+ & fetch
+ >>= A.throwDecode