aboutsummaryrefslogtreecommitdiff
path: root/src/server/Controller
diff options
context:
space:
mode:
authorJoris2016-01-03 23:37:14 +0100
committerJoris2016-01-03 23:46:19 +0100
commitd9df5c3fcffe12aac239b58ccf2fd82c19c3be62 (patch)
treeaee62828e85c9d30e2beb5954062942f0d5d53f4 /src/server/Controller
parentd22d10da342520163014dda255d5d9bd5e1a80c0 (diff)
Validate add payment server side
Diffstat (limited to 'src/server/Controller')
-rw-r--r--src/server/Controller/Payment.hs18
1 files changed, 12 insertions, 6 deletions
diff --git a/src/server/Controller/Payment.hs b/src/server/Controller/Payment.hs
index d2a9258..e94b300 100644
--- a/src/server/Controller/Payment.hs
+++ b/src/server/Controller/Payment.hs
@@ -10,23 +10,24 @@ module Controller.Payment
import Web.Scotty
-import Network.HTTP.Types.Status (ok200)
+import Network.HTTP.Types.Status (ok200, badRequest400)
import Database.Persist
import Control.Monad.IO.Class (liftIO)
import Data.Text (Text)
+import qualified Data.Text as T
+import qualified Data.Aeson.Types as Json
import qualified Secure
-import Json (jsonError)
+import Json (jsonObject, jsonError)
import Model.Database
import qualified Model.Payment as P
import Model.Frequency
import Model.Json.Number
-import qualified Model.Json.PaymentId as JP
import Model.Message
import Model.Message.Key (Key(PaymentNotDeleted))
@@ -42,11 +43,16 @@ getMonthlyPayments =
(liftIO $ runDb (P.getUserMonthlyPayments (entityKey user))) >>= json
)
-createPayment :: Text -> Int -> Frequency -> ActionM ()
+createPayment :: Text -> Text -> Frequency -> ActionM ()
createPayment name cost frequency =
Secure.loggedAction (\user -> do
- paymentId <- liftIO . runDb $ P.createPayment (entityKey user) name cost frequency
- json (JP.PaymentId paymentId)
+ creationResult <- liftIO . runDb $ P.createPayment (entityKey user) name cost frequency
+ case creationResult of
+ Left errors -> do
+ status badRequest400
+ jsonObject . map (\(a, b) -> (a, Json.String . T.pack . show $ b)) $ errors
+ Right paymentId ->
+ jsonObject [("id", Json.Number . fromIntegral . keyToInt64 $ paymentId)]
)
deletePayment :: Text -> ActionM ()