aboutsummaryrefslogtreecommitdiff
path: root/server/src/Persistence
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/Persistence')
-rw-r--r--server/src/Persistence/Income.hs34
-rw-r--r--server/src/Persistence/Payment.hs34
-rw-r--r--server/src/Persistence/PaymentCategory.hs14
3 files changed, 35 insertions, 47 deletions
diff --git a/server/src/Persistence/Income.hs b/server/src/Persistence/Income.hs
index a863f85..cee9892 100644
--- a/server/src/Persistence/Income.hs
+++ b/server/src/Persistence/Income.hs
@@ -1,8 +1,8 @@
module Persistence.Income
( list
, create
- , editOwn
- , deleteOwn
+ , edit
+ , delete
) where
import Data.Maybe (listToMaybe)
@@ -12,7 +12,7 @@ import Database.SQLite.Simple (FromRow (fromRow), Only (Only))
import qualified Database.SQLite.Simple as SQLite
import Prelude hiding (id)
-import Common.Model (Income (..), IncomeId, User (..),
+import Common.Model (Income (..), IncomeId, PaymentId,
UserId)
import Model.Query (Query (Query))
@@ -47,8 +47,8 @@ create incomeUserId incomeDate incomeAmount =
SQLite.lastInsertRowId conn
)
-editOwn :: UserId -> IncomeId -> Day -> Int -> Query Bool
-editOwn incomeUserId incomeId incomeDate incomeAmount =
+edit :: UserId -> IncomeId -> Day -> Int -> Query Bool
+edit incomeUserId incomeId incomeDate incomeAmount =
Query (\conn -> do
mbIncome <- fmap (\(Row i) -> i) . listToMaybe <$>
SQLite.query conn "SELECT * FROM income WHERE id = ?" (Only incomeId)
@@ -68,21 +68,11 @@ editOwn incomeUserId incomeId incomeDate incomeAmount =
return False
)
-deleteOwn :: User -> IncomeId -> Query Bool
-deleteOwn user incomeId =
- Query (\conn -> do
- mbIncome <-
- fmap (\(Row i) -> i) . listToMaybe <$>
- SQLite.query conn "SELECT * FROM income WHERE id = ?" (Only incomeId)
- case mbIncome of
- Just income ->
- if _income_userId income == _user_id user
- then do
- now <- getCurrentTime
- SQLite.execute conn "UPDATE income SET deleted_at = ? WHERE id = ?" (now, incomeId)
- return True
- else
- return False
- Nothing ->
- return False
+delete :: UserId -> PaymentId -> Query ()
+delete userId paymentId =
+ Query (\conn ->
+ SQLite.execute
+ conn
+ "UPDATE income SET deleted_at = datetime('now') WHERE id = ? AND user_id = ?"
+ (paymentId, userId)
)
diff --git a/server/src/Persistence/Payment.hs b/server/src/Persistence/Payment.hs
index 272cd39..3d8f129 100644
--- a/server/src/Persistence/Payment.hs
+++ b/server/src/Persistence/Payment.hs
@@ -6,8 +6,8 @@ module Persistence.Payment
, listActiveMonthlyOrderedByName
, create
, createMany
- , editOwn
- , deleteOwn
+ , edit
+ , delete
) where
import Data.Maybe (listToMaybe)
@@ -129,8 +129,8 @@ createMany payments =
(map InsertRow payments)
)
-editOwn :: UserId -> PaymentId -> Text -> Int -> Day -> Frequency -> Query Bool
-editOwn userId paymentId paymentName paymentCost paymentDate paymentFrequency =
+edit :: UserId -> PaymentId -> Text -> Int -> Day -> Frequency -> Query Bool
+edit userId paymentId paymentName paymentCost paymentDate paymentFrequency =
Query (\conn -> do
mbPayment <- fmap (\(Row p) -> p) . listToMaybe <$>
SQLite.query conn "SELECT * FROM payment WHERE id = ?" (Only paymentId)
@@ -158,23 +158,11 @@ editOwn userId paymentId paymentName paymentCost paymentDate paymentFrequency =
return False
)
-deleteOwn :: UserId -> PaymentId -> Query Bool
-deleteOwn userId paymentId =
- Query (\conn -> do
- mbPayment <- listToMaybe <$>
- SQLite.query conn "SELECT * FROM payment WHERE id = ?" (Only paymentId)
- case mbPayment of
- Just (Row payment) ->
- if _payment_user payment == userId
- then do
- now <- getCurrentTime
- SQLite.execute
- conn
- "UPDATE payment SET deleted_at = ? WHERE id = ?"
- (now, paymentId)
- return True
- else
- return False
- Nothing ->
- return False
+delete :: UserId -> PaymentId -> Query ()
+delete userId paymentId =
+ Query (\conn ->
+ SQLite.execute
+ conn
+ "UPDATE payment SET deleted_at = datetime('now') WHERE id = ? AND user_id = ?"
+ (paymentId, userId)
)
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 ()