mateamt/src/Control/Product.hs

106 lines
2.5 KiB
Haskell
Raw Normal View History

2019-08-14 16:04:16 +00:00
{-# LANGUAGE OverloadedStrings #-}
module Control.Product where
import Servant
import Control.Monad (void)
2019-10-14 20:50:42 +00:00
import Control.Monad.Reader (asks)
2019-08-14 16:04:16 +00:00
2019-09-09 11:17:43 +00:00
import Data.Maybe (fromMaybe)
2019-08-14 16:04:16 +00:00
-- internal imports
import Types
import Model
2019-09-15 12:59:22 +00:00
productNew
:: Maybe (Int, AuthMethod)
-> ProductSubmit
-> MateHandler Int
2019-08-14 16:04:16 +00:00
productNew (Just _) bevsub = do
2019-10-14 20:50:42 +00:00
conn <- asks rsConnection
2019-08-14 16:04:16 +00:00
bevid <- insertProduct bevsub conn
void $ insertNewEmptyAmount bevid bevsub conn
return bevid
productNew Nothing _ =
2019-10-14 20:50:42 +00:00
throwError err401
2019-08-14 16:04:16 +00:00
2019-09-15 12:59:22 +00:00
productOverview
:: Int
-> MateHandler ProductOverview
2019-08-14 16:04:16 +00:00
productOverview pid = do
2019-10-14 20:50:42 +00:00
conn <- asks rsConnection
2019-08-14 16:04:16 +00:00
productOverviewSelectSingle pid conn
2019-09-15 12:59:22 +00:00
productStockRefill
:: Maybe (Int, AuthMethod)
-> [AmountRefill]
-> MateHandler ()
2019-12-14 23:03:49 +00:00
productStockRefill (Just _) amorefs = do
conn <- asks rsConnection
prods <- mapM
(\refill -> productSelectSingle (amountRefillProductId refill) conn)
amorefs
if all (not . null) prods
then
if
all
(\refill ->
(>= 0) (amountRefillAmountSingles refill) &&
(>= 0) (amountRefillAmountCrates refill)
)
amorefs
then do
void $ manualProductAmountRefill amorefs conn
else
throwError $ err400
{ errBody = "Amounts less than 0 are not acceptable."
}
2019-08-14 16:04:16 +00:00
else
2019-09-06 19:44:31 +00:00
throwError $ err400
2019-12-14 23:03:49 +00:00
{ errBody = "Non-existent Products are non-refillable."
2019-08-14 16:04:16 +00:00
}
productStockRefill Nothing _ =
2019-09-11 11:54:18 +00:00
throwError $ err401
2019-09-06 19:44:31 +00:00
{ errBody = "No Authentication present."
2019-08-14 16:04:16 +00:00
}
2019-09-15 12:59:22 +00:00
productStockUpdate
:: Maybe (Int, AuthMethod)
-> [AmountUpdate]
-> MateHandler ()
2019-12-13 22:48:09 +00:00
productStockUpdate (Just (_, method)) amoups =
if method `elem` [PrimaryPass, ChallengeResponse]
then
if all ((>= 0) . amountUpdateRealAmount) amoups
then do
conn <- asks rsConnection
void $ manualProductAmountUpdate amoups conn
else
throwError $ err400
{ errBody = "Amounts less than 0 are not acceptable."
}
2019-08-14 16:04:16 +00:00
else
2019-12-13 22:48:09 +00:00
throwError $ err401
{ errBody = "Wrong Authentication present."
2019-08-14 16:04:16 +00:00
}
productStockUpdate Nothing _ =
2019-09-11 11:54:18 +00:00
throwError $ err401
2019-09-06 19:44:31 +00:00
{ errBody = "No Authentication present."
2019-08-14 16:04:16 +00:00
}
2019-09-15 12:59:22 +00:00
productList
:: Maybe ProductRefine
-> MateHandler [ProductOverview]
2019-09-09 10:57:08 +00:00
productList mrefine = do
2019-10-14 20:50:42 +00:00
conn <- asks rsConnection
2019-09-09 10:57:08 +00:00
productOverviewSelect (fromMaybe AvailableProducts mrefine) conn
productShortList
:: Maybe ProductRefine
-> MateHandler [ProductShortOverview]
productShortList mrefine = do
2019-10-14 20:50:42 +00:00
conn <- asks rsConnection
productShortOverviewSelect (fromMaybe AvailableProducts mrefine) conn