blob: e1936f0c86af3ebbf7940cfb0944ca8b51b0c247 (
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
|
module Controller.Payment
( list
, create
, editOwn
, deleteOwn
) where
import Control.Monad.IO.Class (liftIO)
import qualified Network.HTTP.Types.Status as Status
import Web.Scotty
import Common.Model (CreatePayment (..),
EditPayment (..), PaymentId,
User (..))
import qualified Json
import qualified Model.Query as Query
import qualified Persistence.Payment as PaymentPersistence
import qualified Persistence.PaymentCategory as PaymentCategoryPersistence
import qualified Secure
list :: ActionM ()
list =
Secure.loggedAction (\_ ->
(liftIO . Query.run $ PaymentPersistence.listActive) >>= json
)
create :: CreatePayment -> ActionM ()
create (CreatePayment name cost date category frequency) =
Secure.loggedAction (\user ->
(liftIO . Query.run $ do
PaymentCategoryPersistence.save name category
PaymentPersistence.create (_user_id user) name cost date frequency
) >>= Json.jsonId
)
editOwn :: EditPayment -> ActionM ()
editOwn (EditPayment paymentId name cost date category frequency) =
Secure.loggedAction (\user -> do
updated <- liftIO . Query.run $ do
edited <- PaymentPersistence.editOwn (_user_id user) paymentId name cost date frequency
_ <- if edited
then PaymentCategoryPersistence.save name category >> return ()
else return ()
return edited
if updated
then status Status.ok200
else status Status.badRequest400
)
deleteOwn :: PaymentId -> ActionM ()
deleteOwn paymentId =
Secure.loggedAction (\user -> do
deleted <- liftIO . Query.run $ PaymentPersistence.deleteOwn (_user_id user) paymentId
if deleted
then status Status.ok200
else status Status.badRequest400
)
|