{-# LANGUAGE OverloadedStrings #-} module Controller.User ( getUsers , whoAmI , getIncome , setIncome ) where import Web.Scotty import Network.HTTP.Types.Status (ok200) import Control.Monad.IO.Class (liftIO) import Database.Persist import qualified Data.Aeson.Types as Json import qualified Secure import Json (jsonObject) import Model.Database import qualified Model.User as U import qualified Model.Income as I getUsers :: ActionM () getUsers = Secure.loggedAction (\_ -> (liftIO $ map U.getJsonUser <$> runDb U.getUsers) >>= json ) whoAmI :: ActionM () whoAmI = Secure.loggedAction (\user -> json (U.getJsonUser user) ) getIncome :: ActionM () getIncome = Secure.loggedAction (\user -> do mbIncome <- liftIO . runDb . I.getIncome $ entityKey user case mbIncome of Just income -> jsonObject [("income", Json.Number . fromIntegral . incomeAmount $ income)] Nothing -> jsonObject [] ) setIncome :: Int -> ActionM () setIncome amount = Secure.loggedAction (\user -> (liftIO . runDb $ I.setIncome (entityKey user) amount) >> status ok200 )