aboutsummaryrefslogtreecommitdiff
path: root/server/src/Controller
diff options
context:
space:
mode:
authorJoris2019-10-23 22:35:27 +0200
committerJoris2019-10-23 22:35:27 +0200
commite4b32ce15f8c92f3b477d3f3d4d301ba08f9b5e3 (patch)
tree274c3ef2111b7c7975bc1fb0aa2c5728ba19999c /server/src/Controller
parentf968c8ce63e1aec119b1e6f414cf27e2c0294bcb (diff)
Edit an income
Diffstat (limited to 'server/src/Controller')
-rw-r--r--server/src/Controller/Income.hs21
1 files changed, 13 insertions, 8 deletions
diff --git a/server/src/Controller/Income.hs b/server/src/Controller/Income.hs
index b40976b..236e032 100644
--- a/server/src/Controller/Income.hs
+++ b/server/src/Controller/Income.hs
@@ -11,11 +11,12 @@ import qualified Network.HTTP.Types.Status as Status
import Web.Scotty hiding (delete)
import Common.Model (CreateIncomeForm (..),
- EditIncome (..), IncomeId,
+ EditIncomeForm (..), IncomeId,
User (..))
import qualified Controller.Helper as ControllerHelper
import Model.CreateIncome (CreateIncome (..))
+import Model.EditIncome (EditIncome (..))
import qualified Model.Query as Query
import qualified Persistence.Income as IncomePersistence
import qualified Secure
@@ -40,13 +41,17 @@ create form =
) >>= ControllerHelper.jsonOrBadRequest
)
-edit :: EditIncome -> ActionM ()
-edit (EditIncome incomeId date amount) =
- Secure.loggedAction (\user -> do
- updated <- liftIO . Query.run $ IncomePersistence.edit (_user_id user) incomeId date amount
- if updated
- then status Status.ok200
- else status Status.badRequest400
+edit :: EditIncomeForm -> ActionM ()
+edit form =
+ Secure.loggedAction (\user ->
+ (liftIO . Query.run $ do
+ case IncomeValidation.editIncome form of
+ Success (EditIncome incomeId amount date) -> do
+ Right <$> (IncomePersistence.edit (_user_id user) incomeId date amount)
+
+ Failure validationError ->
+ return $ Left validationError
+ ) >>= ControllerHelper.jsonOrBadRequest
)
delete :: IncomeId -> ActionM ()