aboutsummaryrefslogtreecommitdiff
path: root/server/src/Controller/Category.hs
blob: 36ce3fcfdb68213b77317170ebe6156261a32b73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
module Controller.Category
  ( listAll
  , list
  , create
  , edit
  , delete
  ) where

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, CategoryPage (..),
                                            CreateCategoryForm (..),
                                            EditCategoryForm (..))
import qualified Common.Msg                as Msg

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

listAll :: ActionM ()
listAll =
  Secure.loggedAction (\_ ->
    (liftIO . Query.run $ CategoryPersistence.listAll) >>= json
  )

list :: Int -> Int -> ActionM ()
list page perPage =
  Secure.loggedAction (\_ ->
    (liftIO . Query.run $ do
      categories <- CategoryPersistence.list page perPage
      count <- CategoryPersistence.count
      return $ CategoryPage page categories count
    ) >>= json
  )

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 ()
delete categoryId =
  Secure.loggedAction (\_ -> do
    deleted <- liftIO . Query.run $ do
      -- TODO: delete only if no payment has this category
      CategoryPersistence.delete categoryId
    if deleted
      then
        status ok200
      else do
        status badRequest400
        text . TL.fromStrict $ Msg.get Msg.Category_NotDeleted
  )