blob: 62ab0ed60340cfe81b350c6285ae7292e883c113 (
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
|
module Model.Income
( getJsonIncome
, getIncomes
, createIncome
, deleteOwnIncome
) where
import Data.Time.Clock (getCurrentTime)
import Data.Time.Calendar (Day)
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) (incomeDate income) (incomeAmount income)
where income = entityVal incomeEntity
getIncomes :: Persist [Entity Income]
getIncomes = selectList [IncomeDeletedAt ==. Nothing] []
createIncome :: UserId -> Day -> Int -> Persist IncomeId
createIncome userId date amount = do
now <- liftIO getCurrentTime
insert (Income userId date amount now 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
|