blob: 28f1a093e37d9c46a4f7f062ff805f397b9fcdf3 (
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
65
|
{-# LANGUAGE OverloadedStrings #-}
module Model.Payment
( getPayments
, getMonthlyPayments
, createPayment
, deleteOwnPayment
) where
import Data.Text (Text)
import Data.Time.Clock (getCurrentTime)
import Control.Monad.IO.Class (liftIO)
import Database.Persist
import Model.Database
import Model.Frequency
import qualified Model.Json.Payment as P
getPayments :: Persist [P.Payment]
getPayments =
map getJsonPayment <$> selectList
[ PaymentDeletedAt ==. Nothing ]
[ Desc PaymentCreation ]
getMonthlyPayments :: Persist [Entity Payment]
getMonthlyPayments =
selectList
[ PaymentDeletedAt ==. Nothing
, PaymentFrequency ==. Monthly
]
[ Desc PaymentName ]
getJsonPayment :: Entity Payment -> P.Payment
getJsonPayment paymentEntity =
let payment = entityVal paymentEntity
in P.Payment
{ P.id = entityKey paymentEntity
, P.creation = paymentCreation payment
, P.name = paymentName payment
, P.cost = paymentCost payment
, P.userId = paymentUserId payment
, P.frequency = paymentFrequency payment
}
createPayment :: UserId -> Text -> Int -> Frequency -> Persist PaymentId
createPayment userId name cost frequency = do
now <- liftIO getCurrentTime
insert (Payment userId now name cost Nothing frequency)
deleteOwnPayment :: Entity User -> PaymentId -> Persist Bool
deleteOwnPayment user paymentId = do
mbPayment <- get paymentId
case mbPayment of
Just payment ->
if paymentUserId payment == entityKey user
then do
now <- liftIO getCurrentTime
update paymentId [PaymentDeletedAt =. Just now]
return True
else
return False
Nothing ->
return False
|