blob: 6df77e2a19171c84fba9a3a784106c0e09bdf05f (
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
|
{-# LANGUAGE OverloadedStrings #-}
module Model.PaymentCategory
( list
, listByCategory
, set
, edit
, delete
) where
import Data.Maybe (isJust)
import Data.Text (Text)
import Data.Time.Clock (getCurrentTime)
import qualified Data.Text as T
import Control.Monad.IO.Class (liftIO)
import Database.Persist
import Model.Database
import qualified Model.Json.PaymentCategory as Json
import qualified Utils.Text as T
list :: Persist [Json.PaymentCategory]
list = map getJsonPaymentCategory <$> selectList [] []
listByCategory :: CategoryId -> Persist [Entity PaymentCategory]
listByCategory category = selectList [ PaymentCategoryCategory ==. category ] []
getJsonPaymentCategory :: Entity PaymentCategory -> Json.PaymentCategory
getJsonPaymentCategory entity =
Json.PaymentCategory (paymentCategoryName pc) (paymentCategoryCategory pc)
where pc = entityVal entity
set :: Text -> CategoryId -> Persist ()
set name category = edit name name category
edit :: Text -> Text -> CategoryId -> Persist ()
edit oldName newName category = do
now <- liftIO getCurrentTime
mbPaymentCategory <- selectFirst [PaymentCategoryName ==. (formatPaymentName oldName)] []
if isJust mbPaymentCategory
then
updateWhere
[ PaymentCategoryName ==. (formatPaymentName oldName) ]
[ PaymentCategoryName =. (formatPaymentName newName)
, PaymentCategoryCategory =. category
, PaymentCategoryEditedAt =. Just now
]
else do
_ <- insert $ PaymentCategory (formatPaymentName newName) category now Nothing
return ()
formatPaymentName :: Text -> Text
formatPaymentName = T.unaccent . T.toLower
|