2015-04-04 04:46:33 +00:00
|
|
|
module Handler.Summary where
|
|
|
|
|
|
|
|
import Import
|
2015-04-10 00:53:55 +00:00
|
|
|
import qualified Data.List as L
|
2015-04-11 17:44:13 +00:00
|
|
|
import Data.Aeson
|
|
|
|
import Data.Conduit.Binary
|
2015-04-04 04:46:33 +00:00
|
|
|
|
|
|
|
getSummaryR :: Handler Html
|
|
|
|
getSummaryR = do
|
2015-04-04 06:18:15 +00:00
|
|
|
master <- getYesod
|
2015-04-04 04:46:33 +00:00
|
|
|
bevs <- runDB $ selectList [] [Asc BeverageIdent]
|
|
|
|
defaultLayout $ do
|
|
|
|
$(widgetFile "summary")
|
2015-04-10 00:53:55 +00:00
|
|
|
|
|
|
|
getSummaryJsonR :: Handler RepJson
|
|
|
|
getSummaryJsonR = do
|
|
|
|
master <- getYesod
|
|
|
|
bevs <- runDB $ selectList [] [Asc BeverageIdent]
|
|
|
|
return $
|
|
|
|
repJson $ array $
|
|
|
|
map (\(Entity _ bev) ->
|
|
|
|
object [ "name" .= beverageIdent bev
|
|
|
|
, "value" .= beverageAmount bev
|
2015-04-11 17:44:13 +00:00
|
|
|
, "price" .= ((fromIntegral (beveragePrice bev)) / 100 :: Double)
|
2015-04-10 00:53:55 +00:00
|
|
|
, "currency" .= appCurrency (appSettings master)
|
|
|
|
]
|
|
|
|
) bevs
|
2015-04-11 17:44:13 +00:00
|
|
|
|
2015-04-16 00:12:03 +00:00
|
|
|
data BevStore = BevStore
|
|
|
|
{ bevStoreIdent :: Text
|
|
|
|
, bevStorePrice :: Int
|
|
|
|
, bevStoreAmount :: Int
|
|
|
|
, bevStoreAlertAmount :: Int
|
|
|
|
}
|
|
|
|
|
|
|
|
instance ToJSON BevStore where
|
|
|
|
toJSON (BevStore ident price amount alertAmount) =
|
2015-04-11 17:44:13 +00:00
|
|
|
object
|
|
|
|
[ "name" .= ident
|
|
|
|
, "price" .= price
|
|
|
|
, "amount" .= amount
|
|
|
|
, "alertAt" .= alertAmount
|
|
|
|
]
|
|
|
|
|
2015-04-16 00:12:03 +00:00
|
|
|
instance FromJSON BevStore where
|
|
|
|
parseJSON (Object o) = BevStore
|
2015-04-11 17:44:13 +00:00
|
|
|
<$> o .: "name"
|
|
|
|
<*> o .: "price"
|
|
|
|
<*> o .: "amount"
|
|
|
|
<*> o .: "alertAt"
|
|
|
|
-- For errors
|
|
|
|
parseJSON _ = mzero
|
|
|
|
|
|
|
|
getInventoryJsonR :: Handler RepJson
|
|
|
|
getInventoryJsonR = do
|
|
|
|
bevs <- runDB $ selectList [] [Asc BeverageIdent]
|
|
|
|
return $
|
|
|
|
repJson $ array $
|
2015-04-16 00:12:03 +00:00
|
|
|
map (\(Entity _ bev) -> toJSON $ BevStore
|
|
|
|
(beverageIdent bev)
|
|
|
|
(beveragePrice bev)
|
|
|
|
(beverageAmount bev)
|
|
|
|
(beverageAlertAmount bev)
|
|
|
|
) bevs
|
2015-04-11 17:44:13 +00:00
|
|
|
|
|
|
|
getUploadInventoryJsonR :: Handler Html
|
|
|
|
getUploadInventoryJsonR = do
|
|
|
|
(uploadJsonWidget, enctype) <- generateFormPost uploadJsonForm
|
|
|
|
defaultLayout $ do
|
|
|
|
$(widgetFile "uploadJson")
|
|
|
|
|
|
|
|
postUploadInventoryJsonR :: Handler Html
|
|
|
|
postUploadInventoryJsonR = do
|
|
|
|
((res, _), _) <- runFormPost uploadJsonForm
|
|
|
|
case res of
|
|
|
|
FormSuccess file -> do
|
|
|
|
case fileContentType file == "application/json" of
|
|
|
|
True -> do
|
|
|
|
source <- runResourceT $ fileSource file $$ sinkLbs
|
2015-04-16 00:12:03 +00:00
|
|
|
bevs <- return $ fromMaybe [] $ (decode source :: Maybe [BevStore])
|
2015-04-11 17:44:13 +00:00
|
|
|
_ <- return $ map insOrUpd bevs
|
|
|
|
setMessageI MsgRestoreSuccess
|
|
|
|
redirect $ HomeR
|
|
|
|
False -> do
|
|
|
|
setMessageI MsgNotJson
|
|
|
|
redirect $ UploadInventoryJsonR
|
|
|
|
_ -> do
|
|
|
|
setMessageI MsgErrorOccured
|
|
|
|
redirect $ UploadInventoryJsonR
|
|
|
|
|
|
|
|
uploadJsonForm :: Form FileInfo
|
|
|
|
uploadJsonForm = renderDivs
|
|
|
|
$ areq fileField (fieldSettingsLabel MsgSelectFile) Nothing
|
|
|
|
|
2015-04-16 00:12:03 +00:00
|
|
|
insOrUpd :: BevStore -> Handler ()
|
2015-04-11 17:44:13 +00:00
|
|
|
insOrUpd bev = do
|
2015-04-16 00:12:03 +00:00
|
|
|
meb <- runDB $ getBy $ UniqueBeverage $ bevStoreIdent bev
|
2015-04-11 17:44:13 +00:00
|
|
|
case meb of
|
|
|
|
Just eb -> do
|
|
|
|
runDB $ update (entityKey eb)
|
2015-04-16 00:12:03 +00:00
|
|
|
[ BeveragePrice =. bevStorePrice bev
|
|
|
|
, BeverageAmount =. bevStoreAmount bev
|
|
|
|
, BeverageAlertAmount =. bevStoreAlertAmount bev
|
2015-04-11 17:44:13 +00:00
|
|
|
]
|
|
|
|
Nothing -> do
|
2015-04-16 00:12:03 +00:00
|
|
|
runDB $ insert_ $ Beverage
|
|
|
|
(bevStoreIdent bev)
|
|
|
|
(bevStorePrice bev)
|
|
|
|
(bevStoreAmount bev)
|
|
|
|
(bevStoreAlertAmount bev)
|
|
|
|
Nothing
|