aboutsummaryrefslogtreecommitdiff
path: root/server/src/Persistence
diff options
context:
space:
mode:
authorJoris2019-08-05 21:53:30 +0200
committerJoris2019-08-05 21:53:30 +0200
commitbc81084933f8ec1bfe6c2834defd12243117fdd9 (patch)
tree116d5e8ccd5f234d7376f4f14c74657d7c7e4792 /server/src/Persistence
parent2741f47ef7b87255203bc2f7f7b2b9140c70b8f0 (diff)
Use updated payment categories from payment add in payment’s table
Diffstat (limited to 'server/src/Persistence')
-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