aboutsummaryrefslogtreecommitdiff
path: root/server/src/Controller/Category.hs
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/Controller/Category.hs')
-rw-r--r--server/src/Controller/Category.hs66
1 files changed, 49 insertions, 17 deletions
diff --git a/server/src/Controller/Category.hs b/server/src/Controller/Category.hs
index 8fbc8c8..36ce3fc 100644
--- a/server/src/Controller/Category.hs
+++ b/server/src/Controller/Category.hs
@@ -1,5 +1,6 @@
module Controller.Category
- ( list
+ ( listAll
+ , list
, create
, edit
, delete
@@ -7,37 +8,68 @@ module Controller.Category
import Control.Monad.IO.Class (liftIO)
import qualified Data.Text.Lazy as TL
+import Data.Validation (Validation (..))
import Network.HTTP.Types.Status (badRequest400, ok200)
import Web.Scotty hiding (delete)
-import Common.Model (CategoryId, CreateCategory (..),
- EditCategory (..))
+import Common.Model (CategoryId, CategoryPage (..),
+ CreateCategoryForm (..),
+ EditCategoryForm (..))
import qualified Common.Msg as Msg
-import Json (jsonId)
+import qualified Controller.Helper as ControllerHelper
+import Model.CreateCategory (CreateCategory (..))
+import Model.EditCategory (EditCategory (..))
import qualified Model.Query as Query
import qualified Persistence.Category as CategoryPersistence
import qualified Secure
+import qualified Validation.Category as CategoryValidation
-list :: ActionM ()
-list =
+listAll :: ActionM ()
+listAll =
Secure.loggedAction (\_ ->
- (liftIO . Query.run $ CategoryPersistence.list) >>= json
+ (liftIO . Query.run $ CategoryPersistence.listAll) >>= json
)
-create :: CreateCategory -> ActionM ()
-create (CreateCategory name color) =
+list :: Int -> Int -> ActionM ()
+list page perPage =
Secure.loggedAction (\_ ->
- (liftIO . Query.run $ CategoryPersistence.create name color) >>= jsonId
+ (liftIO . Query.run $ do
+ categories <- CategoryPersistence.list page perPage
+ count <- CategoryPersistence.count
+ return $ CategoryPage page categories count
+ ) >>= json
)
-edit :: EditCategory -> ActionM ()
-edit (EditCategory categoryId name color) =
- Secure.loggedAction (\_ -> do
- updated <- liftIO . Query.run $ CategoryPersistence.edit categoryId name color
- if updated
- then status ok200
- else status badRequest400
+create :: CreateCategoryForm -> ActionM ()
+create form =
+ Secure.loggedAction (\_ ->
+ (liftIO . Query.run $ do
+ case CategoryValidation.createCategory form of
+ Success (CreateCategory name color) -> do
+ Right <$> (CategoryPersistence.create name color)
+
+ Failure validationError ->
+ return $ Left validationError
+ ) >>= ControllerHelper.okOrBadRequest
+ )
+
+edit :: EditCategoryForm -> ActionM ()
+edit form =
+ Secure.loggedAction (\_ ->
+ (liftIO . Query.run $ do
+ case CategoryValidation.editCategory form of
+ Success (EditCategory categoryId name color) ->
+ do
+ isSuccess <- CategoryPersistence.edit categoryId name color
+ return $ if isSuccess then
+ Right ()
+ else
+ Left $ Msg.get Msg.Error_CategoryEdit
+
+ Failure validationError ->
+ return $ Left validationError
+ ) >>= ControllerHelper.okOrBadRequest
)
delete :: CategoryId -> ActionM ()