aboutsummaryrefslogtreecommitdiff
path: root/server/src/Controller
diff options
context:
space:
mode:
authorJoris2019-08-11 22:40:09 +0200
committerJoris2019-08-11 22:40:09 +0200
commit2d79ab0e0a11f55255fc21a5dfab1598d3beeba3 (patch)
tree80ab3cd98cebfb9694f66aa7718f6bc5d1c83d22 /server/src/Controller
parentc542424b7b41c78a170763f6996c12f56b359860 (diff)
Add payment clone
Diffstat (limited to 'server/src/Controller')
-rw-r--r--server/src/Controller/Payment.hs29
1 files changed, 17 insertions, 12 deletions
diff --git a/server/src/Controller/Payment.hs b/server/src/Controller/Payment.hs
index 3d857be..c700240 100644
--- a/server/src/Controller/Payment.hs
+++ b/server/src/Controller/Payment.hs
@@ -10,9 +10,9 @@ import qualified Network.HTTP.Types.Status as Status
import Web.Scotty hiding (delete)
import Common.Model (CreatePayment (..),
- CreatedPayment (..),
EditPayment (..), Payment (..),
- PaymentId, User (..))
+ PaymentId, SavedPayment (..),
+ User (..))
import qualified Model.Query as Query
import qualified Persistence.Payment as PaymentPersistence
import qualified Persistence.PaymentCategory as PaymentCategoryPersistence
@@ -33,7 +33,7 @@ create createPayment@(CreatePayment name cost date category frequency) =
(liftIO . Query.run $ do
pc <- PaymentCategoryPersistence.save name category
p <- PaymentPersistence.create (_user_id user) name cost date frequency
- return $ CreatedPayment p pc
+ return $ SavedPayment p pc
) >>= json
Just validationError ->
do
@@ -44,15 +44,20 @@ create createPayment@(CreatePayment name cost date category frequency) =
edit :: EditPayment -> ActionM ()
edit (EditPayment paymentId name cost date category frequency) =
Secure.loggedAction (\user -> do
- updated <- liftIO . Query.run $ do
- edited <- PaymentPersistence.edit (_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
+ result <- liftIO . Query.run $ do
+ editedPayment <- PaymentPersistence.edit (_user_id user) paymentId name cost date frequency
+ case editedPayment of
+ Just p -> do
+ pc <- PaymentCategoryPersistence.save name category
+ PaymentCategoryPersistence.deleteIfUnused name
+ return $ Just (p, pc)
+ Nothing ->
+ return Nothing
+ case result of
+ Just (p, pc) ->
+ json $ SavedPayment p pc
+ Nothing ->
+ status Status.badRequest400
)
delete :: PaymentId -> ActionM ()