blob: f3896616c7424b36907e6f861dede2e1e5f71bcd (
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
module Model.Income
( getJsonIncome
, getIncomes
, create
, editOwn
, deleteOwn
) 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] []
create :: UserId -> Day -> Int -> Persist IncomeId
create userId date amount = do
now <- liftIO getCurrentTime
insert (Income userId date amount now Nothing Nothing)
editOwn :: UserId -> IncomeId -> Day -> Int -> Persist Bool
editOwn userId incomeId date amount = do
mbIncome <- get incomeId
case mbIncome of
Just income ->
if incomeUserId income == userId
then do
now <- liftIO getCurrentTime
update incomeId
[ IncomeEditedAt =. Just now
, IncomeDate =. date
, IncomeAmount =. amount
]
return True
else
return False
Nothing ->
return False
deleteOwn :: Entity User -> IncomeId -> Persist Bool
deleteOwn 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
|