42 lines
1.2 KiB
Haskell
42 lines
1.2 KiB
Haskell
{-# OPTIONS_GHC -fno-warn-missing-methods #-}
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
|
{-# LANGUAGE PackageImports #-}
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
module Types.Orphans where
|
|
|
|
import qualified "mateamt" Types as MT
|
|
|
|
import Servant.API
|
|
import Servant.HTML.Blaze
|
|
import Web.Internal.FormUrlEncoded
|
|
|
|
import qualified Data.Text as T
|
|
import Data.Maybe (fromJust)
|
|
|
|
instance MimeUnrender HTML MT.UserSubmit
|
|
instance FromForm MT.UserSubmit
|
|
|
|
instance MimeUnrender HTML MT.UserDetailsSubmit
|
|
instance FromForm MT.UserDetailsSubmit
|
|
|
|
instance MimeUnrender HTML [MT.PurchaseDetail]
|
|
instance FromForm [MT.PurchaseDetail] where
|
|
fromForm form =
|
|
let kvs = toListStable form
|
|
ks = fst (unzip kvs)
|
|
kpids = filter ("productSelect" `T.isPrefixOf`) ks
|
|
prods = filter ((`notElem` kpids) . fst) kvs
|
|
stripPid = snd . T.breakOnEnd "-"
|
|
digestForm key =
|
|
let pid = read (T.unpack $ stripPid key)
|
|
amount = read
|
|
(T.unpack . fromJust $
|
|
lookup ("productAmount-" <> stripPid key) prods)
|
|
in MT.PurchaseDetail pid amount
|
|
res = map digestForm kpids
|
|
in
|
|
Right res
|