yammat/Handler/Modify.hs
2015-04-10 01:30:25 +02:00

58 lines
1.8 KiB
Haskell

module Handler.Modify where
import Import
import Handler.Common
getModifyR :: BeverageId -> Handler Html
getModifyR bId = do
mBev <- runDB $ get bId
case mBev of
Just bev -> do
(modifyWidget, enctype) <- generateFormPost $ modifyForm bev
defaultLayout $ do
$(widgetFile "modify")
Nothing -> do
setMessageI MsgItemUnknown
redirect $ SummaryR
postModifyR :: BeverageId -> Handler Html
postModifyR bId = do
mBev <- runDB $ get bId
case mBev of
Just bev -> do
((res, _), _) <- runFormPost $ modifyForm bev
case res of
FormSuccess nBev -> do
runDB $ update bId
[ BeverageIdent =. beverageIdent nBev
, BeveragePrice =. beveragePrice nBev
, BeverageAmount =. beverageAmount nBev
, BeverageAlertAmount =. beverageAlertAmount nBev
]
setMessageI MsgEditSuccess
redirect $ SummaryR
_ -> do
setMessageI MsgEditFail
redirect $ SummaryR
Nothing -> do
setMessageI MsgItemUnknown
redirect $ SummaryR
modifyForm :: Beverage -> Form Beverage
modifyForm bev = renderDivs $ Beverage
<$> areq textField (fieldSettingsLabel MsgName) (Just $ beverageIdent bev)
<*> areq currencyField (fieldSettingsLabel MsgPrice) (Just $ beveragePrice bev)
<*> areq amountField (fieldSettingsLabel MsgCurrentStock) (Just $ beverageAmount bev)
<*> areq amountField (fieldSettingsLabel MsgAnnouncedStock) (Just $ beverageAlertAmount bev)
getDeleteBeverageR :: BeverageId -> Handler Html
getDeleteBeverageR bId = do
mBev <- runDB $ get bId
case mBev of
Just bev -> do
runDB $ delete bId
setMessageI MsgItemDeleted
redirect $ HomeR
Nothing -> do
setMessageI MsgItemUnknown
redirect $ HomeR