fix adding the same tag twice

This commit is contained in:
Inhji 2023-08-02 20:41:31 +02:00
parent 75a7571a5b
commit 2e8adf5704
2 changed files with 17 additions and 2 deletions

View file

@ -70,7 +70,10 @@ defmodule Chiya.Tags.TagUpdater do
end
defp add_tags(note, tags) do
Enum.each(tags, &add_tag(note, &1))
tags
|> Enum.uniq()
|> Enum.each(&add_tag(note, &1))
note
end
@ -102,7 +105,10 @@ defmodule Chiya.Tags.TagUpdater do
end
defp remove_tags(note, tags) do
Enum.each(tags, &remove_tag(note, &1))
tags
|> Enum.uniq()
|> Enum.each(&remove_tag(note, &1))
note
end

View file

@ -53,5 +53,14 @@ defmodule Chiya.TagUpdaterTest do
tag = List.first(note.tags)
assert tag.name == "foo"
end
test "with the same tag twice only adds unique tags" do
note = note_fixture()
assert note.tags == []
TagUpdater.update_tags(note, "foo,foo")
note = Chiya.Notes.get_note_preloaded!(note.id)
assert Enum.count(note.tags) == 1
end
end
end