blob: 43e1306b48bbf40722f90f89db01932a3e91f7c9 (
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
#! /usr/bin/env nix-shell
#! nix-shell -i runghc -p "haskellPackages.ghcWithPackages (p: with p; [ yesod persistent-postgresql ])"
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleInstances #-}
import Yesod
import Database.Persist.Postgresql
import Control.Monad.Logger (runStderrLoggingT)
import Control.Monad.Reader
import Data.Time.Calendar
import Data.Text (Text)
import qualified Data.Text as Text
import Data.Map.Lazy (Map)
import qualified Data.Map.Lazy as Map
import Data.Aeson
share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
Item
kind Text
bought Day Maybe
expires Day Maybe
opened Day Maybe
deriving Show
|]
instance ToJSON Item where
toJSON Item{..} = object
[ "kind" .= itemKind
, "bought" .= itemBought
, "expires" .= itemExpires
, "opened" .= itemOpened
]
instance ToJSON (Entity Item) where
toJSON = entityIdToJSON
data BarInventory = BarInventory
{ sqlPool :: ConnectionPool
}
mkYesod "BarInventory" [parseRoutes|
/ InventoryR GET
|]
instance Yesod BarInventory
instance YesodPersist BarInventory where
type YesodPersistBackend BarInventory = SqlBackend
runDB action = runSqlPool action . sqlPool =<< getYesod
main = runStderrLoggingT . withPostgresqlPool "" 5 . runReaderT $ do
sqlPool <- ask
liftIO . warpEnv $ BarInventory{..}
getInventoryR :: Handler TypedContent
getInventoryR = do
stock <- runDB $ selectList [] []
selectRep $ do
provideRep . return . toJSON $ (stock :: [Entity Item])
|