matebeamter/src/Control/Product.hs

94 lines
2.4 KiB
Haskell

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PackageImports #-}
module Control.Product where
import Servant
import Servant.Client
import Servant.Client.Core.Auth (mkAuthenticatedRequest)
import Data.String (fromString)
import qualified Data.Text as T
import Data.Text.I18n
import Control.Monad.IO.Class (liftIO)
-- imports from "mateamt"
import qualified "mateamt" Types as MT
-- internal imports
import Types
import View
import Util
import Client
import ClientAuth
import API
import Hash
productGetPriceList
:: Maybe T.Text
-> UserHandler ProductPriceListPage
productGetPriceList mcookie = do
(token, muser, loc, l10n, backend) <- controlInit mcookie
elist <- liftIO $ runClientM
(productShortList
(Just MT.AllProducts)
)
backend
case elist of
Right list ->
return (productPriceListPage l10n loc mcookie list)
Left err ->
throwError $
addMessage (fromString $ show err) $
redirect303 userSelectLink
productGetRefill
:: Maybe T.Text
-> UserHandler ProductRefillPage
productGetRefill mcookie = do
(token, muser, loc, l10n, backend) <- controlInit mcookie
case muser of
Just _ -> do
elist <- liftIO $ runClientM
(productShortList (Just MT.AllProducts))
backend
case elist of
Right list -> do
return $ productRefillPage l10n loc mcookie list
Left err ->
throwError $
addMessage (fromString $ show err) $
redirect303 userSelectLink
Nothing ->
redirectOverAuth Nothing (Just productGetRefillLink) Nothing
productPostRefill
:: Maybe T.Text
-> [MT.AmountRefill]
-> UserHandler ProductRefillPage
productPostRefill mcookie refills = do
(token, muser, loc, l10n, backend) <- controlInit mcookie
case muser of
Just _ -> do
eresult <- liftIO $ runClientM
(productStockRefill
(mkAuthenticatedRequest token authenticateReq)
refills
)
backend
case eresult of
Right _ ->
throwError $
addMessage (translate l10n loc "Refill successfull") $
redirect303 productGetRefillLink
Left err ->
handleClientErr err (read <$> muser) (Just productGetRefillLink)
Nothing ->
redirectOverAuth (read <$> muser) (Just productGetRefillLink) Nothing
where
translate l10n loc = localize l10n loc . gettext