aboutsummaryrefslogtreecommitdiff
path: root/server/src/Controller/Income.hs
blob: ed58ac882d38d9afea32271ac19bd581bb519029 (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
module Controller.Income
  ( create
  , edit
  , delete
  ) where

import           Control.Monad.IO.Class    (liftIO)
import qualified Network.HTTP.Types.Status as Status
import           Web.Scotty                hiding (delete)

import           Common.Model              (CreateIncome (..), EditIncome (..),
                                            IncomeId, User (..))

import           Json                      (jsonId)
import qualified Model.Query               as Query
import qualified Persistence.Income        as IncomePersistence
import qualified Secure

create :: CreateIncome -> ActionM ()
create (CreateIncome date amount) =
  Secure.loggedAction (\user ->
    (liftIO . Query.run $ IncomePersistence.create (_user_id user) date amount) >>= jsonId
  )

edit :: EditIncome -> ActionM ()
edit (EditIncome incomeId date amount) =
  Secure.loggedAction (\user -> do
    updated <- liftIO . Query.run $ IncomePersistence.edit (_user_id user) incomeId date amount
    if updated
      then status Status.ok200
      else status Status.badRequest400
  )

delete :: IncomeId -> ActionM ()
delete incomeId =
  Secure.loggedAction (\user -> do
    _ <- liftIO . Query.run $ IncomePersistence.delete (_user_id user) incomeId
    status Status.ok200
  )