{-# LANGUAGE DataKinds #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE FlexibleContexts #-} module Util where import Servant hiding (addHeader) import Servant.Client.Core.Request import Servant.Client.Core.Auth import Servant.Client.Core.Request (addHeader) import qualified Data.Text as T import Data.Text.Lazy.Encoding import Data.ByteString.Builder import Data.List (intercalate) import Data.List.Split (chunksOf) import Data.String (fromString) import Control.Monad.Error import Web.Cookie import Text.Printf (printf) setCookie :: SetCookie -> Request -> Request setCookie c = addHeader "Set-Cookie" (decodeUtf8 . toLazyByteString $ renderSetCookie c) formatMoney :: Int -> T.Text formatMoney amount = pre <> t <> "," <> c where pre = if amount < 0 then "-" else "" t = fromString $ reverse (intercalate "." $ chunksOf 3 $ reverse $ fst sp) c = fromString $ snd sp sp = tail <$> break (== '.') (printf "%.2f" (abs amount)) redirect303 :: MonadError ServerError m => Link -> m a redirect303 link = throwError $ err303 { errHeaders = [ ("Location", "/" <> (fromString $ show $ linkURI $ link)) ] }