summaryrefslogtreecommitdiff
path: root/Handler/InventoryListing.hs
blob: e3c062ecf637446652a6afc1aac39f6ce2e7640d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
module Handler.InventoryListing where

import Import
import Handler.Common

getInventoryListingR, postInventoryListingR :: Handler TypedContent
getInventoryListingR = postInventoryListingR
postInventoryListingR = do
  ((insertResult, fsInsertForm), fsInsertEncoding) <- runFormPost $ itemForm Nothing

  mapM_ (addMessage "formError" . toHtml) =<< case insertResult of 
    FormSuccess newItem -> [] <$ runDB (insert newItem)
    FormFailure errors -> return errors
    _ -> return []

  (sortOn entityVal -> stock) <- runDB $ selectList [] []

  selectRep $ do
    provideJson (stock :: [Entity Item])
    provideRep . defaultLayout $ inventoryListing InventoryState
      { formState = Just InsertForm{..}
      , ..
      }

putInventoryListingR :: Handler Value
putInventoryListingR = returnJson =<< runDB . insertEntity =<< (requireCheckJsonBody :: Handler Item)