aboutsummaryrefslogtreecommitdiff
path: root/src/server/Controller/User.hs
blob: 420a2d90a78f670907371121df7da45038e3481d (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
{-# 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
  )