From 8062f1c9c34e9b25d76b22bd6ba2a1a99666279b Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 26 Mar 2017 22:56:41 +0200 Subject: Fix update payment category constraint error --- src/server/Model/PaymentCategory.hs | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) (limited to 'src/server/Model') diff --git a/src/server/Model/PaymentCategory.hs b/src/server/Model/PaymentCategory.hs index 6df77e2..3b0b858 100644 --- a/src/server/Model/PaymentCategory.hs +++ b/src/server/Model/PaymentCategory.hs @@ -3,19 +3,16 @@ module Model.PaymentCategory ( list , listByCategory - , set - , edit - , delete + , save ) where +import Control.Monad.IO.Class (liftIO) 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 qualified Data.Text as T import Model.Database import qualified Model.Json.PaymentCategory as Json @@ -32,19 +29,15 @@ 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 +save :: Text -> CategoryId -> Persist () +save newName category = do now <- liftIO getCurrentTime - mbPaymentCategory <- selectFirst [PaymentCategoryName ==. (formatPaymentName oldName)] [] + mbPaymentCategory <- selectFirst [PaymentCategoryName ==. (formatPaymentName newName)] [] if isJust mbPaymentCategory then updateWhere - [ PaymentCategoryName ==. (formatPaymentName oldName) ] - [ PaymentCategoryName =. (formatPaymentName newName) - , PaymentCategoryCategory =. category + [ PaymentCategoryName ==. (formatPaymentName newName) ] + [ PaymentCategoryCategory =. category , PaymentCategoryEditedAt =. Just now ] else do -- cgit v1.2.3