aboutsummaryrefslogtreecommitdiffstats
path: root/backend/lib/ACMS/API/REST/Collection/Paginated.hs
blob: 159754a6076ae0b5b3ca8f5c9239a5a8a055e8e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

module ACMS.API.REST.Collection.Paginated where

import ACMS.API.Fetch
import ACMS.API.REST (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