{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Servant.API.Auth.Token.Pagination(
PageParam
, Page
, PageSizeParam
, PageSize
, PagedList(..)
) where
import Data.Aeson.WithField
import Data.Monoid
import Data.Proxy
import Data.Swagger
import Data.Swagger.Internal.Schema
import Data.Text (pack)
import Data.Typeable
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 (Typeable i, Typeable a, ToSchema i, ToSchema a) => ToSchema (PagedList i a) where
declareNamedSchema p = do
s <- genericDeclareNamedSchema (schemaOptionsDropPrefix "pagedList") p
return $ rename nm s
where
nm = Just $ "PagedList " <> iname <> " " <> aname
iname = pack . show $ typeRep (Proxy :: Proxy i)
aname = pack . show $ typeRep (Proxy :: Proxy a)
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
}