diff options
author | 2024-12-18 19:11:03 +0100 | |
---|---|---|
committer | 2025-02-19 18:44:43 +0100 | |
commit | 9b0adc976101bc4f375b05cc475478187c595714 (patch) | |
tree | 67dde004c05d5bcf380c72f0134ac673f6a4f027 /backend/lib/ACMS | |
parent | 04a9bbfab5536791f4850903ebb339c846341184 (diff) |
add pagination to rest api
Diffstat (limited to 'backend/lib/ACMS')
-rw-r--r-- | backend/lib/ACMS/API/REST/Collection/Paginated.hs | 32 |
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 |