yammat/Handler/Modify.hs
2015-05-19 05:37:22 +02:00

68 lines
2.3 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
, BeverageCorrectedAmount +=. ((beverageAmount nBev) - (beverageAmount bev))
, BeverageMl =. beverageMl nBev
, BeverageAvatar =. beverageAvatar 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)
<*> pure (beverageCorrectedAmount bev)
<*> areq volumeField (fieldSettingsLabel MsgVolume) (Just $ beverageMl bev)
<*> aopt (selectField avatars) (fieldSettingsLabel MsgSelectAvatar) (Just $ beverageAvatar bev)
where
avatars = do
ents <- runDB $ selectList [] [Asc AvatarIdent]
optionsPairs $ map (\ent -> ((avatarIdent $ entityVal ent), entityKey ent)) ents
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