aboutsummaryrefslogtreecommitdiff
path: root/server/src/Persistence/PaymentCategory.hs
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/Persistence/PaymentCategory.hs')
-rw-r--r--server/src/Persistence/PaymentCategory.hs48
1 files changed, 31 insertions, 17 deletions
diff --git a/server/src/Persistence/PaymentCategory.hs b/server/src/Persistence/PaymentCategory.hs
index 1e377b1..1cfd702 100644
--- a/server/src/Persistence/PaymentCategory.hs
+++ b/server/src/Persistence/PaymentCategory.hs
@@ -4,7 +4,7 @@ module Persistence.PaymentCategory
, save
) where
-import Data.Maybe (isJust, listToMaybe)
+import qualified Data.Maybe as Maybe
import Data.Text (Text)
import qualified Data.Text as T
import Data.Time.Clock (getCurrentTime)
@@ -40,27 +40,41 @@ listByCategory cat =
SQLite.query conn "SELECT * FROM payment_category WHERE category = ?" (Only cat)
)
-save :: Text -> CategoryId -> Query ()
+save :: Text -> CategoryId -> Query PaymentCategory
save newName categoryId =
Query (\conn -> do
now <- getCurrentTime
- hasPaymentCategory <- isJust <$> listToMaybe <$>
+ paymentCategory <- fmap (\(Row pc) -> pc) . Maybe.listToMaybe <$>
(SQLite.query
conn
"SELECT * FROM payment_category WHERE name = ?"
- (Only (formatPaymentName newName)) :: IO [Row])
- if hasPaymentCategory
- then
- SQLite.execute
- conn
- "UPDATE payment_category SET category = ?, edited_at = ? WHERE name = ?"
- (categoryId, now, formatPaymentName newName)
- else do
- SQLite.execute
- conn
- "INSERT INTO payment_category (name, category, created_at) VALUES (?, ?, ?)"
- (formatPaymentName newName, categoryId, now)
+ (Only formattedNewName))
+ case paymentCategory of
+ Just pc ->
+ do
+ SQLite.execute
+ conn
+ "UPDATE payment_category SET category = ?, edited_at = ? WHERE name = ?"
+ (categoryId, now, formattedNewName)
+ return $ PaymentCategory
+ (_paymentCategory_id pc)
+ formattedNewName
+ categoryId
+ (_paymentCategory_createdAt pc)
+ (Just now)
+ Nothing ->
+ do
+ SQLite.execute
+ conn
+ "INSERT INTO payment_category (name, category, created_at) VALUES (?, ?, ?)"
+ (formattedNewName, categoryId, now)
+ paymentCategoryId <- SQLite.lastInsertRowId conn
+ return $ PaymentCategory
+ paymentCategoryId
+ formattedNewName
+ categoryId
+ now
+ Nothing
)
where
- formatPaymentName :: Text -> Text
- formatPaymentName = T.unaccent . T.toLower
+ formattedNewName = T.unaccent . T.toLower $ newName