diff options
author | Joris | 2019-08-10 14:53:41 +0200 |
---|---|---|
committer | Joris | 2019-08-10 14:53:41 +0200 |
commit | fb8f0fe577e28dae69903413b761da50586e0099 (patch) | |
tree | 91149151facf24348ce1f9798edd5c70be795d11 /server/src/Persistence/PaymentCategory.hs | |
parent | 3943c50d5320f7137bd5acec4485dd56a2aa52b3 (diff) |
Remove payment category if unused after a payment is deleted
Diffstat (limited to 'server/src/Persistence/PaymentCategory.hs')
-rw-r--r-- | server/src/Persistence/PaymentCategory.hs | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/server/src/Persistence/PaymentCategory.hs b/server/src/Persistence/PaymentCategory.hs index 5fd035a..7dc363c 100644 --- a/server/src/Persistence/PaymentCategory.hs +++ b/server/src/Persistence/PaymentCategory.hs @@ -2,16 +2,17 @@ module Persistence.PaymentCategory ( list , listByCategory , save + , deleteIfUnused ) where import qualified Data.Maybe as Maybe import Data.Text (Text) +import qualified Data.Text as T import Data.Time.Clock (getCurrentTime) import Database.SQLite.Simple (FromRow (fromRow), Only (Only)) import qualified Database.SQLite.Simple as SQLite import Common.Model (CategoryId, PaymentCategory (..)) -import qualified Common.Util.Text as T import Model.Query (Query (Query)) @@ -76,4 +77,13 @@ save newName categoryId = Nothing ) where - formattedNewName = T.formatSearch newName + formattedNewName = T.toLower newName + +deleteIfUnused :: Text -> Query () +deleteIfUnused name = + Query (\conn -> + SQLite.execute + conn + "DELETE FROM payment_category WHERE name = lower(?) AND name IN (SELECT DISTINCT lower(name) FROM payment WHERE name = lower(?) AND deleted_at IS NOT NULL)" + (name, name) + ) >> return () |