diff options
Diffstat (limited to 'src/server/Controller')
-rw-r--r-- | src/server/Controller/Payment.hs | 18 |
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 () |