aboutsummaryrefslogtreecommitdiff
path: root/server/src/Controller/Income.hs
blob: 3f623e5fa5c5ef1b3177b915a53e857ff40e0272 (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
module Controller.Income
  ( create
  , editOwn
  , deleteOwn
  ) where

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

import           Common.Model              (CreateIncome (..), EditIncome (..),
                                            IncomeId, User (..))
import qualified Common.Msg                as Msg

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
  )

editOwn :: EditIncome -> ActionM ()
editOwn (EditIncome incomeId date amount) =
  Secure.loggedAction (\user -> do
    updated <- liftIO . Query.run $ IncomePersistence.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 $ IncomePersistence.deleteOwn user incomeId
    if deleted
      then
        status ok200
      else do
        status badRequest400
        text . TL.fromStrict $ Msg.get Msg.Income_NotDeleted
  )