     annotation = Map.merge(annotation, %{tags: annotation.annotation_tags})
     changeset = Annotation.changeset(annotation, annotation_params)
 
-    case Repo.update(changeset) do
+    case update_annotation_and_tags(changeset) do
       {:ok, annotation} ->
         annotation = Repo.preload(annotation, :annotation_tags)
         conn

        {:error, changeset}
    end
  end
end

    end)
  end

  # Update annotation and tags
  defp update_annotation_and_tags(changeset) do
    tags = Ecto.Changeset.get_field(changeset, :tags)

    Repo.transaction(fn ->
      result = Repo.update(changeset)
      |> remove_tags()
      |> add_tags(tags)

      case result do
        {:error, changeset} ->
          Repo.rollback(changeset)

        {:ok, annotation, tags} ->
          Map.merge(annotation, %{tags: tags})
      end
    end)
  end

  # Add tags to an annotation
  #
  # Returns a {:ok, tags} or {:error, changeset} tuple
