summaryrefslogtreecommitdiff
path: root/Handler/InventoryListing.hs
blob: c2ec5d1282cfbd7bc19601ba1dbad824d3660c88 (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
27
28
29
30
31
32
33
module Handler.InventoryListing where

import Import
import Handler.Common

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

  case insertResult of 
    FormSuccess (Item{..} `WithType` t) -> runDB $ do
      upsertBy (UniqueKind itemNormKind) (Kind itemNormKind t) [ KindType =. t ]
      insert Item{..}
      return ()
    FormFailure errors -> mapM_ (addMessage "formError" . toHtml) errors
    _ -> return ()

  (stockSort -> stock) <- runDB $ withTypes =<< selectList [] []

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

putInventoryListingR :: Handler Value
putInventoryListingR = do
  (Item{..} `WithType` t) <- requireCheckJsonBody
  returnJson <=< runDB $ do
    upsertBy (UniqueKind itemNormKind) (Kind itemNormKind t) [ KindType =. t ]
    withType =<< insertEntity Item{..}