aboutsummaryrefslogtreecommitdiff
path: root/src/server/Model/Income.hs
blob: 2177617f446413f73c7cd96e90fb89d2b2bc7bf7 (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
module Model.Income
  ( getJsonIncome
  , getFirstIncome
  , getIncomes
  , setIncome
  ) where

import Data.Time.Clock (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 [] []

getFirstIncome :: UserId -> Persist (Maybe Income)
getFirstIncome userId =
  fmap entityVal <$> selectFirst [IncomeUserId ==. userId] [Asc IncomeCreation]

setIncome :: UserId -> Int -> Persist IncomeId
setIncome userId amount = do
  now <- liftIO getCurrentTime
  insert (Income userId now amount)