summaryrefslogtreecommitdiff
path: root/Handler/Item.hs
blob: 25302756f6d2474bfed9e5a0b3154144b3c371d5 (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
34
35
module Handler.Item where

import Import

getItemR :: ItemId -> Handler TypedContent
getItemR itemId = do
  entity <- runDB $ withType =<< Entity itemId <$> get404 itemId
  selectRep $ do
    provideJson entity
    provideRep (redirect $ InventoryListingR :#: itemId :: Handler Html)

putItemR :: ItemId -> Handler Value
putItemR itemId = do
  (Item{..} `WithType` t) <- requireCheckJsonBody
  returnJson <=< runDB $ do
    entity <- Entity itemId <$> (updateGet itemId [ ItemKind =. itemKind
                                                  , ItemNormKind =. itemNormKind
                                                  , ItemBought =. itemBought
                                                  , ItemExpires =. itemExpires
                                                  , ItemOpened =. itemOpened
                                                  , ItemRunningLow =. itemRunningLow
                                                  ])
    update (itemFkType $ entityVal entity) [ KindType =. t ]
    withType entity

patchItemR :: ItemId -> Handler Value
patchItemR itemId = do
  (itemUpdates, typeUpdates) <- toUpdate <$> (requireCheckJsonBody :: Handler ItemDiffs)
  returnJson <=< runDB $ do
    entity <- Entity itemId <$> updateGet itemId itemUpdates
    update (itemFkType $ entityVal entity) typeUpdates
    withType entity

deleteItemR :: ItemId -> Handler ()
deleteItemR = runDB . delete