aboutsummaryrefslogtreecommitdiff
path: root/src/server/Model/Income.hs
blob: c0cac45611fc42157f1a4c24a39fc45c9d18312c (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
module Model.Income
  ( getJsonIncome
  , getIncomes
  , addIncome
  , deleteOwnIncome
  ) where

import Data.Time.Clock (UTCTime, getCurrentTime)

import Control.Monad.IO.Class (liftIO)

import Database.Persist

import Model.Database
import qualified Model.Json.Income as Json

getJsonIncome :: Entity Income -> Json.Income
getJsonIncome incomeEntity =
  Json.Income (entityKey incomeEntity) (incomeUserId income) (incomeCreation income) (incomeAmount income)
  where income = entityVal incomeEntity

getIncomes :: Persist [Entity Income]
getIncomes = selectList [IncomeDeletedAt ==. Nothing] []

addIncome :: UserId -> UTCTime -> Int -> Persist IncomeId
addIncome userId creation amount = do
  insert (Income userId creation amount Nothing)

deleteOwnIncome :: Entity User -> IncomeId -> Persist Bool
deleteOwnIncome user incomeId = do
  mbIncome <- get incomeId
  case mbIncome of
    Just income ->
      if incomeUserId income == entityKey user
        then do
          now <- liftIO getCurrentTime
          update incomeId [IncomeDeletedAt =. Just now]
          return True
        else
          return False
    Nothing ->
      return False