matebeamter/src/Util.hs

55 lines
1.2 KiB
Haskell
Raw Normal View History

2019-09-08 10:48:57 +00:00
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE OverloadedStrings #-}
2019-09-18 13:24:23 +00:00
{-# LANGUAGE FlexibleContexts #-}
2019-09-08 15:27:38 +00:00
module Util where
2019-09-08 10:48:57 +00:00
import Servant hiding (addHeader)
2019-09-11 04:27:54 +00:00
import Servant.Client.Core.Request
2019-09-08 10:48:57 +00:00
import Servant.Client.Core.Auth
import Servant.Client.Core.Request (addHeader)
2019-09-11 04:27:54 +00:00
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)
2019-09-18 13:24:23 +00:00
import Control.Monad.Error
2019-09-11 04:27:54 +00:00
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))
2019-09-18 13:24:23 +00:00
redirect303
:: MonadError ServerError m
=> Link
-> m a
redirect303 link =
throwError $ err303
{ errHeaders =
[ ("Location", "/" <> (fromString $ show $ linkURI $ link))
]
}