From d84b462a711ce95593ff05a7581e722562c3835a Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Tue, 14 Mar 2017 01:06:28 +0100 Subject: Implement old bar.hs --- Handler/UpdateItem.hs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Handler/UpdateItem.hs (limited to 'Handler/UpdateItem.hs') diff --git a/Handler/UpdateItem.hs b/Handler/UpdateItem.hs new file mode 100644 index 0000000..353572b --- /dev/null +++ b/Handler/UpdateItem.hs @@ -0,0 +1,33 @@ +module Handler.UpdateItem where + +import Import + +import Handler.Common + +getUpdateItemR, postUpdateItemR :: ItemId -> Handler TypedContent +getUpdateItemR = postUpdateItemR +postUpdateItemR fsUpdateItem = do + Just entity <- fmap (Entity fsUpdateItem) <$> runDB (get fsUpdateItem) + + ((updateResult, fsUpdateForm), fsUpdateEncoding) <- runFormPost . itemForm . Just $ entityVal entity + + mapM_ (addMessage "formError" . toHtml) =<< case updateResult of + FormSuccess Item{..} -> [] <$ runDB (update fsUpdateItem [ ItemKind =. itemKind + , ItemNormKind =. normalizeKind itemKind + , ItemBought =. itemBought + , ItemExpires =. itemExpires + , ItemOpened =. itemOpened + ]) + FormFailure errors -> return errors + _ -> return [] + + selectRep $ do + provideRep $ case updateResult of + FormSuccess _ -> redirect $ InventoryListingR :#: fsUpdateItem :: Handler Html + _ -> do + (sortOn entityVal -> stock) <- runDB $ selectList [] [] + defaultLayout $ inventoryListing InventoryState + { formState = Just UpdateForm{..} + , .. + } + provideJson () -- cgit v1.2.3