module Servant.API.Auth.Token.Pagination(
PageParam
, Page
, PageSizeParam
, PageSize
, PagedList(..)
) where
import Data.Aeson.WithField
import Data.Proxy
import Data.Swagger
import GHC.Generics
import Servant.API
import Servant.API.Auth.Token.Internal.DeriveJson
import Servant.API.Auth.Token.Internal.Schema
import Servant.Docs
type PageParam = QueryParam "page" Page
type Page = Word
type PageSizeParam = QueryParam "size" PageSize
type PageSize = Word
instance ToParam PageParam where
toParam _ = DocQueryParam "page" ["0", "42"] "Index of page" Normal
instance ToParam PageSizeParam where
toParam _ = DocQueryParam "size" ["42", "10"] "Number of elements on page" Normal
data PagedList i a = PagedList {
pagedListItems :: ![WithId i a]
, pagedListPages :: !Word
} deriving (Generic, Show)
$(deriveJSON (derivePrefix "pagedList") ''PagedList)
instance (ToSchema i, ToSchema a) => ToSchema (PagedList i a) where
declareNamedSchema = genericDeclareNamedSchema $
schemaOptionsDropPrefix "pagedList"
instance (ToSample i, ToSample a) => ToSample (PagedList i a) where
toSamples _ = samples $ [s $ toSamples (Proxy :: Proxy (WithId i a))]
where
s as = PagedList {
pagedListItems = snd <$> as
, pagedListPages = 1
}