aboutsummaryrefslogtreecommitdiff
path: root/src/server/Controller/Income.hs
blob: 70e40ce77a2ffb0c74e096e158f5ce09097e2655 (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
{-# LANGUAGE OverloadedStrings #-}

module Controller.Income
  ( getIncomes
  , addIncome
  , deleteOwnIncome
  ) where

import Web.Scotty

import Network.HTTP.Types.Status (ok200, badRequest400)

import Control.Monad.IO.Class (liftIO)

import Database.Persist

import Data.Text (Text)
import qualified Data.Text.Lazy as TL

import qualified Secure

import Json (jsonId)

import Model.Database
import qualified Model.Income as Income
import qualified Model.Message.Key as Key
import qualified Model.Json.AddIncome as Json

getIncomes :: ActionM ()
getIncomes =
  Secure.loggedAction (\_ ->
    (liftIO $ map Income.getJsonIncome <$> runDb Income.getIncomes) >>= json
  )

addIncome :: Json.AddIncome -> ActionM ()
addIncome (Json.AddIncome date amount) =
  Secure.loggedAction (\user ->
    (liftIO . runDb $ Income.addIncome (entityKey user) date amount) >>= jsonId
  )

deleteOwnIncome :: Text -> ActionM ()
deleteOwnIncome incomeId =
  Secure.loggedAction (\user -> do
    deleted <- liftIO . runDb $ Income.deleteOwnIncome user (textToKey incomeId)
    if deleted
      then
        status ok200
      else do
        status badRequest400
        text . TL.pack . show $ Key.IncomeNotDeleted
  )