aboutsummaryrefslogtreecommitdiff
path: root/server/src/Controller
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/Controller')
-rw-r--r--server/src/Controller/Income.hs33
-rw-r--r--server/src/Controller/Payment.hs37
2 files changed, 36 insertions, 34 deletions
diff --git a/server/src/Controller/Income.hs b/server/src/Controller/Income.hs
index 3f623e5..ed58ac8 100644
--- a/server/src/Controller/Income.hs
+++ b/server/src/Controller/Income.hs
@@ -1,17 +1,15 @@
module Controller.Income
( create
- , editOwn
- , deleteOwn
+ , edit
+ , delete
) where
import Control.Monad.IO.Class (liftIO)
-import qualified Data.Text.Lazy as TL
-import Network.HTTP.Types.Status (badRequest400, ok200)
-import Web.Scotty
+import qualified Network.HTTP.Types.Status as Status
+import Web.Scotty hiding (delete)
import Common.Model (CreateIncome (..), EditIncome (..),
IncomeId, User (..))
-import qualified Common.Msg as Msg
import Json (jsonId)
import qualified Model.Query as Query
@@ -24,23 +22,18 @@ create (CreateIncome date amount) =
(liftIO . Query.run $ IncomePersistence.create (_user_id user) date amount) >>= jsonId
)
-editOwn :: EditIncome -> ActionM ()
-editOwn (EditIncome incomeId date amount) =
+edit :: EditIncome -> ActionM ()
+edit (EditIncome incomeId date amount) =
Secure.loggedAction (\user -> do
- updated <- liftIO . Query.run $ IncomePersistence.editOwn (_user_id user) incomeId date amount
+ updated <- liftIO . Query.run $ IncomePersistence.edit (_user_id user) incomeId date amount
if updated
- then status ok200
- else status badRequest400
+ then status Status.ok200
+ else status Status.badRequest400
)
-deleteOwn :: IncomeId -> ActionM ()
-deleteOwn incomeId =
+delete :: IncomeId -> ActionM ()
+delete incomeId =
Secure.loggedAction (\user -> do
- deleted <- liftIO . Query.run $ IncomePersistence.deleteOwn user incomeId
- if deleted
- then
- status ok200
- else do
- status badRequest400
- text . TL.fromStrict $ Msg.get Msg.Income_NotDeleted
+ _ <- liftIO . Query.run $ IncomePersistence.delete (_user_id user) incomeId
+ status Status.ok200
)
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
)