{-# OPTIONS_GHC -fno-warn-orphans #-}

module Servant.Util.Combinators.Sorting.Client () where

import Universum

import qualified Data.Text as T
import Servant.API (ToHttpApiData (..), (:>))
import Servant.Client.Core (Client, HasClient (..))
import Servant.Server (Tagged (..))

import Servant.Util.Combinators.Sorting.Base


instance HasClient m subApi =>
         HasClient m (SortingParams provided base :> subApi) where
    type Client m (SortingParams provided base :> subApi) =
        SortingSpec provided base -> Client m subApi
    clientWithRoute :: Proxy m
-> Proxy (SortingParams provided base :> subApi)
-> Request
-> Client m (SortingParams provided base :> subApi)
clientWithRoute Proxy m
mp Proxy (SortingParams provided base :> subApi)
_ Request
req (SortingSpec [SortingItem]
sorting) =
        Proxy m
-> Proxy (SortParamsExpanded provided subApi)
-> Request
-> Maybe (Tagged provided [SortingItem])
-> Client m subApi
forall (m :: * -> *) api.
HasClient m api =>
Proxy m -> Proxy api -> Request -> Client m api
clientWithRoute Proxy m
mp (Proxy (SortParamsExpanded provided subApi)
forall k (t :: k). Proxy t
Proxy @(SortParamsExpanded provided subApi)) Request
req
            (Tagged provided [SortingItem]
-> Maybe (Tagged provided [SortingItem])
forall a. a -> Maybe a
Just (Tagged provided [SortingItem]
 -> Maybe (Tagged provided [SortingItem]))
-> Tagged provided [SortingItem]
-> Maybe (Tagged provided [SortingItem])
forall a b. (a -> b) -> a -> b
$ [SortingItem] -> Tagged provided [SortingItem]
forall k (s :: k) b. b -> Tagged s b
Tagged [SortingItem]
sorting)
    hoistClientMonad :: Proxy m
-> Proxy (SortingParams provided base :> subApi)
-> (forall x. mon x -> mon' x)
-> Client mon (SortingParams provided base :> subApi)
-> Client mon' (SortingParams provided base :> subApi)
hoistClientMonad Proxy m
mp Proxy (SortingParams provided base :> subApi)
_ forall x. mon x -> mon' x
hst Client mon (SortingParams provided base :> subApi)
subCli = Proxy m
-> Proxy subApi
-> (forall x. mon x -> mon' x)
-> Client mon subApi
-> Client mon' subApi
forall (m :: * -> *) api (mon :: * -> *) (mon' :: * -> *).
HasClient m api =>
Proxy m
-> Proxy api
-> (forall x. mon x -> mon' x)
-> Client mon api
-> Client mon' api
hoistClientMonad Proxy m
mp (Proxy subApi
forall k (t :: k). Proxy t
Proxy @subApi) forall x. mon x -> mon' x
hst (Client mon subApi -> Client mon' subApi)
-> (SortingSpec provided base -> Client mon subApi)
-> SortingSpec provided base
-> Client mon' subApi
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Client mon (SortingParams provided base :> subApi)
SortingSpec provided base -> Client mon subApi
subCli

instance ToHttpApiData (TaggedSortingItemsList provided) where
    toUrlPiece :: TaggedSortingItemsList provided -> Text
toUrlPiece (Tagged [SortingItem]
sorting) =
        Text -> [Text] -> Text
T.intercalate Text
"," ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ [SortingItem]
sorting [SortingItem] -> (SortingItem -> Text) -> [Text]
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \SortingItem{Text
SortingOrder
siOrder :: SortingItem -> SortingOrder
siName :: SortingItem -> Text
siOrder :: SortingOrder
siName :: Text
..} ->
             let order :: Text
order = case SortingOrder
siOrder of
                     SortingOrder
Ascendant  -> Text
"asc"
                     SortingOrder
Descendant -> Text
"desc"
             in Text
order Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"(" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
siName Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"