aboutsummaryrefslogtreecommitdiff
path: root/src/server/Controller/Payment.hs
blob: 117310a3048c466e4ec8cfb89ce3ffca58d57c6f (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
module Controller.Payment
  ( getPaymentsAction
  , createPaymentAction
  , deletePaymentAction
  , getTotalPaymentsAction
  ) 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 Secure

import Model.Database
import Model.Payment
import Model.Json.Message
import Model.Message
import Model.Message.Key (Key(PaymentNotDeleted))

getPaymentsAction :: ActionM ()
getPaymentsAction =
  Secure.loggedAction (\_ -> do
    payments <- liftIO $ runDb getPayments
    json payments
  )

createPaymentAction :: Text -> Int -> ActionM ()
createPaymentAction name cost =
  Secure.loggedAction (\user -> do
    paymentKey <- liftIO . runDb $ createPayment (entityKey user) name cost
    json . Message . paymentKeyToText $ paymentKey
  )

deletePaymentAction :: Text -> ActionM ()
deletePaymentAction paymentId =
  Secure.loggedAction (\user -> do
    deleted <- liftIO . runDb $ deleteOwnPayment user (textToKey paymentId)
    if deleted
      then
        status ok200
      else do
        status badRequest400
        json . Message . getMessage $ PaymentNotDeleted
  )

getTotalPaymentsAction :: ActionM ()
getTotalPaymentsAction =
  Secure.loggedAction (\_ -> do
    (liftIO . runDb $ getTotalPayments) >>= json
  )