aboutsummaryrefslogtreecommitdiff
path: root/src/server/Controller/Category.hs
diff options
context:
space:
mode:
authorJoris2017-03-24 09:21:06 +0000
committerJoris2017-03-24 09:21:06 +0000
commitc0ac16a713c4e53cf6af8e72a6d5f6b8ac5d6456 (patch)
tree8a438430cee7411259fc395d8f3898488e85d750 /src/server/Controller/Category.hs
parent293eb8295162bf0a038f488237db9c9d1316c04d (diff)
parentcfca18262c1ff48dcb683ddab7d03cf8e55573ff (diff)
downloadbudget-c0ac16a713c4e53cf6af8e72a6d5f6b8ac5d6456.tar.gz
budget-c0ac16a713c4e53cf6af8e72a6d5f6b8ac5d6456.tar.bz2
budget-c0ac16a713c4e53cf6af8e72a6d5f6b8ac5d6456.zip
Merge branch 'features/categories' into 'master'
Features/categories See merge request !1
Diffstat (limited to 'src/server/Controller/Category.hs')
-rw-r--r--src/server/Controller/Category.hs54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/server/Controller/Category.hs b/src/server/Controller/Category.hs
new file mode 100644
index 0000000..19109a3
--- /dev/null
+++ b/src/server/Controller/Category.hs
@@ -0,0 +1,54 @@
+{-# LANGUAGE OverloadedStrings #-}
+
+module Controller.Category
+ ( create
+ , edit
+ , delete
+ ) where
+
+import Control.Monad.IO.Class (liftIO)
+
+import Data.Text (Text)
+import Network.HTTP.Types.Status (ok200, badRequest400)
+import qualified Data.Text.Lazy as TL
+import Web.Scotty hiding (delete)
+
+import Json (jsonId)
+import Model.Database
+import qualified Model.Category as Category
+import qualified Model.Json.CreateCategory as Json
+import qualified Model.Json.EditCategory as Json
+import qualified Model.Message.Key as Key
+import qualified Model.PaymentCategory as PaymentCategory
+import qualified Secure
+
+create :: Json.CreateCategory -> ActionM ()
+create (Json.CreateCategory name color) =
+ Secure.loggedAction (\_ ->
+ (liftIO . runDb $ Category.create name color) >>= jsonId
+ )
+
+edit :: Json.EditCategory -> ActionM ()
+edit (Json.EditCategory categoryId name color) =
+ Secure.loggedAction (\_ -> do
+ updated <- liftIO . runDb $ Category.edit categoryId name color
+ if updated
+ then status ok200
+ else status badRequest400
+ )
+
+delete :: Text -> ActionM ()
+delete categoryId =
+ Secure.loggedAction (\_ -> do
+ deleted <- liftIO . runDb $ do
+ paymentCategories <- PaymentCategory.listByCategory (textToKey categoryId)
+ if null paymentCategories
+ then Category.delete (textToKey categoryId)
+ else return False
+ if deleted
+ then
+ status ok200
+ else do
+ status badRequest400
+ text . TL.pack . show $ Key.CategoryNotDeleted
+ )