aboutsummaryrefslogtreecommitdiff
path: root/src/server/Controller/User.hs
diff options
context:
space:
mode:
authorJoris2015-09-13 12:04:52 +0200
committerJoris2015-09-13 12:04:52 +0200
commit5babf01323bcb62a9880593165af70732f22751b (patch)
tree716373ce6f3ebeaa0bb532b30d2a3b65d894d128 /src/server/Controller/User.hs
parenta94813019b146d75f7556d10e8d0dfd50586f54d (diff)
downloadbudget-5babf01323bcb62a9880593165af70732f22751b.tar.gz
budget-5babf01323bcb62a9880593165af70732f22751b.tar.bz2
budget-5babf01323bcb62a9880593165af70732f22751b.zip
Adding income database table with a getter and a setter
Diffstat (limited to 'src/server/Controller/User.hs')
-rw-r--r--src/server/Controller/User.hs25
1 files changed, 21 insertions, 4 deletions
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
)