blob: 55edea54d6a2a2fd1f31076e64d3f87a58a56448 (
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
|
{-# LANGUAGE OverloadedStrings #-}
module Controller.Payment
( list
, create
, deleteOwn
) where
import Web.Scotty
import Network.HTTP.Types.Status (ok200, badRequest400)
import Database.Persist
import Control.Monad.IO.Class (liftIO)
import Data.Text (Text)
import qualified Data.Text.Lazy as TL
import qualified Secure
import Json (jsonId)
import Model.Database
import qualified Model.Payment as Payment
import Model.Message.Key (Key(PaymentNotDeleted))
import qualified Model.Json.CreatePayment as Json
list :: ActionM ()
list =
Secure.loggedAction (\_ -> do
(liftIO $ runDb Payment.list) >>= json
)
create :: Json.CreatePayment -> ActionM ()
create (Json.CreatePayment date name cost frequency) =
Secure.loggedAction (\user ->
(liftIO . runDb $ Payment.create (entityKey user) date name cost frequency) >>= jsonId
)
deleteOwn :: Text -> ActionM ()
deleteOwn paymentId =
Secure.loggedAction (\user -> do
deleted <- liftIO . runDb $ Payment.deleteOwn user (textToKey paymentId)
if deleted
then
status ok200
else do
status badRequest400
text . TL.pack . show $ PaymentNotDeleted
)
|