From 2e8adf57048235d43fdb5fa14d37859341fc0750 Mon Sep 17 00:00:00 2001 From: Inhji Date: Wed, 2 Aug 2023 20:41:31 +0200 Subject: [PATCH] fix adding the same tag twice --- lib/chiya/tags/tag_updater.ex | 10 ++++++++-- test/chiya/tag_updater_test.exs | 9 +++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/chiya/tags/tag_updater.ex b/lib/chiya/tags/tag_updater.ex index b5c334e..1649ba4 100644 --- a/lib/chiya/tags/tag_updater.ex +++ b/lib/chiya/tags/tag_updater.ex @@ -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 diff --git a/test/chiya/tag_updater_test.exs b/test/chiya/tag_updater_test.exs index 02885af..940499a 100644 --- a/test/chiya/tag_updater_test.exs +++ b/test/chiya/tag_updater_test.exs @@ -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