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

module Controller.Income
  ( create
  , editOwn
  , deleteOwn
  ) where

import Control.Monad.IO.Class (liftIO)
import Network.HTTP.Types.Status (ok200, badRequest400)
import qualified Data.Text.Lazy as TL
import Web.Scotty

import Json (jsonId)
import Model.Income (IncomeId)
import qualified Model.Income as Income
import qualified Model.Json.CreateIncome as Json
import qualified Model.Json.EditIncome as Json
import qualified Model.Message.Key as Key
import qualified Model.Query as Query
import qualified Model.User as User
import qualified Secure

create :: Json.CreateIncome -> ActionM ()
create (Json.CreateIncome date amount) =
  Secure.loggedAction (\user ->
    (liftIO . Query.run $ Income.create (User.id user) date amount) >>= jsonId
  )

editOwn :: Json.EditIncome -> ActionM ()
editOwn (Json.EditIncome incomeId date amount) =
  Secure.loggedAction (\user -> do
    updated <- liftIO . Query.run $ Income.editOwn (User.id user) incomeId date amount
    if updated
      then status ok200
      else status badRequest400
  )

deleteOwn :: IncomeId -> ActionM ()
deleteOwn incomeId =
  Secure.loggedAction (\user -> do
    deleted <- liftIO . Query.run $ Income.deleteOwn user incomeId
    if deleted
      then
        status ok200
      else do
        status badRequest400
        text . TL.pack . show $ Key.IncomeNotDeleted
  )