yammat/Handler/Modify.hs

65 lines
2.1 KiB
Haskell
Raw Normal View History

2015-04-04 04:46:33 +00:00
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
2015-04-09 22:40:58 +00:00
setMessageI MsgItemUnknown
2015-04-04 04:46:33 +00:00
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
, BeverageAvatar =. beverageAvatar nBev
2015-04-04 04:46:33 +00:00
]
2015-04-09 22:40:58 +00:00
setMessageI MsgEditSuccess
2015-04-04 04:46:33 +00:00
redirect $ SummaryR
_ -> do
2015-04-09 22:40:58 +00:00
setMessageI MsgEditFail
2015-04-04 04:46:33 +00:00
redirect $ SummaryR
Nothing -> do
2015-04-09 22:40:58 +00:00
setMessageI MsgItemUnknown
2015-04-04 04:46:33 +00:00
redirect $ SummaryR
modifyForm :: Beverage -> Form Beverage
modifyForm bev = renderDivs $ Beverage
2015-04-09 22:40:58 +00:00
<$> 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)
2015-04-16 00:12:03 +00:00
<*> 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
2015-04-04 04:46:33 +00:00
getDeleteBeverageR :: BeverageId -> Handler Html
getDeleteBeverageR bId = do
mBev <- runDB $ get bId
case mBev of
Just bev -> do
runDB $ delete bId
2015-04-09 22:40:58 +00:00
setMessageI MsgItemDeleted
2015-04-04 04:46:33 +00:00
redirect $ HomeR
Nothing -> do
2015-04-09 22:40:58 +00:00
setMessageI MsgItemUnknown
2015-04-04 04:46:33 +00:00
redirect $ HomeR