aboutsummaryrefslogtreecommitdiff
path: root/server/src/Controller
diff options
context:
space:
mode:
authorJoris2019-10-20 12:02:21 +0200
committerJoris2019-10-20 12:02:21 +0200
commit7aadcc97f9df0e2daccbe8a8726d8bc6c63d67f4 (patch)
tree3637cc06f6378fc3ea04844f15fe43bc04155007 /server/src/Controller
parent6e9e34e92a244ab6c38d135d46f9f5bb01391906 (diff)
Add income
Diffstat (limited to 'server/src/Controller')
-rw-r--r--server/src/Controller/Income.hs23
1 files changed, 17 insertions, 6 deletions
diff --git a/server/src/Controller/Income.hs b/server/src/Controller/Income.hs
index ed58ac8..e013849 100644
--- a/server/src/Controller/Income.hs
+++ b/server/src/Controller/Income.hs
@@ -5,21 +5,32 @@ module Controller.Income
) where
import Control.Monad.IO.Class (liftIO)
+import Data.Validation (Validation (Failure, Success))
import qualified Network.HTTP.Types.Status as Status
import Web.Scotty hiding (delete)
-import Common.Model (CreateIncome (..), EditIncome (..),
- IncomeId, User (..))
+import Common.Model (CreateIncomeForm (..),
+ EditIncome (..), IncomeId,
+ User (..))
-import Json (jsonId)
+import qualified Controller.Helper as ControllerHelper
+import Model.CreateIncome (CreateIncome (..))
import qualified Model.Query as Query
import qualified Persistence.Income as IncomePersistence
import qualified Secure
+import qualified Validation.Income as IncomeValidation
-create :: CreateIncome -> ActionM ()
-create (CreateIncome date amount) =
+create :: CreateIncomeForm -> ActionM ()
+create form =
Secure.loggedAction (\user ->
- (liftIO . Query.run $ IncomePersistence.create (_user_id user) date amount) >>= jsonId
+ (liftIO . Query.run $ do
+ case IncomeValidation.createIncome form of
+ Success (CreateIncome amount date) -> do
+ Right <$> (IncomePersistence.create (_user_id user) date amount)
+
+ Failure validationError ->
+ return $ Left validationError
+ ) >>= ControllerHelper.jsonOrBadRequest
)
edit :: EditIncome -> ActionM ()