From 5babf01323bcb62a9880593165af70732f22751b Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 13 Sep 2015 12:04:52 +0200 Subject: Adding income database table with a getter and a setter --- src/server/Controller/User.hs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'src/server/Controller/User.hs') diff --git a/src/server/Controller/User.hs b/src/server/Controller/User.hs index bc99ea5..420a2d9 100644 --- a/src/server/Controller/User.hs +++ b/src/server/Controller/User.hs @@ -4,12 +4,17 @@ 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 @@ -18,21 +23,33 @@ import Json (jsonObject) import Model.Database import qualified Model.User as U +import qualified Model.Income as I getUsers :: ActionM () getUsers = - Secure.loggedAction (\_ -> do + Secure.loggedAction (\_ -> (liftIO $ map U.getJsonUser <$> runDb U.getUsers) >>= json ) whoAmI :: ActionM () whoAmI = - Secure.loggedAction (\user -> do + Secure.loggedAction (\user -> json (U.getJsonUser user) ) getIncome :: ActionM () getIncome = - Secure.loggedAction (\_ -> do - jsonObject [] + 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 ) -- cgit v1.2.3