Skip to content

Commit

Permalink
Deduplicate tags before inserting them
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-si committed Mar 14, 2024
1 parent e5a4640 commit 0c2f923
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion tasklite-core/source/Lib.hs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import Protolude qualified as P
import Control.Arrow ((>>>))
import Data.Aeson as Aeson (KeyValue ((.=)), encode, object)
import Data.Coerce (coerce)
import Data.List (nub)
import Data.Generics (Data, constrFields, toConstr)
import Data.Hourglass (
DateTime (dtTime),
Expand Down Expand Up @@ -369,7 +370,8 @@ updateTask connection task = do

insertTags :: Connection -> Maybe DateTime -> Task -> [Text] -> IO ()
insertTags connection mbCreatedUtc task tags = do
taskToTags <- forM tags $ \tag -> do
let uniqueTags = nub tags
taskToTags <- forM uniqueTags $ \tag -> do
tagUlid <- getULID
pure $
TaskToTag
Expand Down
21 changes: 21 additions & 0 deletions tasklite-core/test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,27 @@ testSuite conf now = do
}
_ -> P.die "More than one task found"

it "deduplicates tags when adding a task" $ do
withMemoryDb conf $ \memConn -> do
_ <- addTask conf memConn ["Buy milk +drink +drink"]
(tasks :: [FullTask]) <- query_ memConn "SELECT * FROM tasks_view"
case tasks of
[updatedTask] -> do
updatedTask `shouldSatisfy` (\task -> task.ulid /= "")
updatedTask `shouldSatisfy` (\task -> task.modified_utc /= "")
updatedTask `shouldSatisfy` (\task -> task.user /= "")
updatedTask
{ FullTask.ulid = ""
, FullTask.modified_utc = ""
, FullTask.user = ""
}
`shouldBe` emptyFullTask
{ FullTask.body = "Buy milk"
, FullTask.priority = Just 2.0
, FullTask.tags = Just ["drink"]
}
_ -> P.die "More than one task found"

it "logs a task" $ do
withMemoryDb conf $ \memConn -> do
result <- logTask conf memConn ["Just a test"]
Expand Down

0 comments on commit 0c2f923

Please sign in to comment.