aboutsummaryrefslogtreecommitdiff
path: root/server/src/Controller/Payment.hs
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/Controller/Payment.hs')
-rw-r--r--server/src/Controller/Payment.hs37
1 files changed, 23 insertions, 14 deletions
diff --git a/server/src/Controller/Payment.hs b/server/src/Controller/Payment.hs
index e82fd49..3d857be 100644
--- a/server/src/Controller/Payment.hs
+++ b/server/src/Controller/Payment.hs
@@ -1,18 +1,18 @@
module Controller.Payment
( list
, create
- , editOwn
- , deleteOwn
+ , edit
+ , delete
) where
import Control.Monad.IO.Class (liftIO)
import qualified Network.HTTP.Types.Status as Status
-import Web.Scotty
+import Web.Scotty hiding (delete)
import Common.Model (CreatePayment (..),
CreatedPayment (..),
- EditPayment (..), PaymentId,
- User (..))
+ EditPayment (..), Payment (..),
+ PaymentId, User (..))
import qualified Model.Query as Query
import qualified Persistence.Payment as PaymentPersistence
import qualified Persistence.PaymentCategory as PaymentCategoryPersistence
@@ -41,11 +41,11 @@ create createPayment@(CreatePayment name cost date category frequency) =
json validationError
)
-editOwn :: EditPayment -> ActionM ()
-editOwn (EditPayment paymentId 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.editOwn (_user_id user) paymentId name cost date frequency
+ edited <- PaymentPersistence.edit (_user_id user) paymentId name cost date frequency
_ <- if edited
then PaymentCategoryPersistence.save name category >> return ()
else return ()
@@ -55,11 +55,20 @@ editOwn (EditPayment paymentId name cost date category frequency) =
else status Status.badRequest400
)
-deleteOwn :: PaymentId -> ActionM ()
-deleteOwn paymentId =
+delete :: PaymentId -> ActionM ()
+delete paymentId =
Secure.loggedAction (\user -> do
- deleted <- liftIO . Query.run $ PaymentPersistence.deleteOwn (_user_id user) paymentId
- if deleted
- then status Status.ok200
- else status Status.badRequest400
+ deleted <- liftIO . Query.run $ do
+ payment <- PaymentPersistence.find paymentId
+ case payment of
+ Just p | _payment_user p == _user_id user -> do
+ PaymentPersistence.delete (_user_id user) paymentId
+ PaymentCategoryPersistence.deleteIfUnused (_payment_name p)
+ return True
+ _ ->
+ return False
+ if deleted then
+ status Status.ok200
+ else
+ status Status.badRequest400
)