From 9840690af16f5b594db26cabbd9a881f098c5a0b Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Tue, 21 Apr 2026 12:47:57 +0100 Subject: [PATCH] Fix nil-index crash in move_record_in_set --- lib/music_library/record_sets.ex | 38 ++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/lib/music_library/record_sets.ex b/lib/music_library/record_sets.ex index f31d177d..f960bb1a 100644 --- a/lib/music_library/record_sets.ex +++ b/lib/music_library/record_sets.ex @@ -165,27 +165,31 @@ defmodule MusicLibrary.RecordSets do ) |> Repo.all() - index = Enum.find_index(items, fn i -> i.record_id == record_id end) + case Enum.find_index(items, fn i -> i.record_id == record_id end) do + nil -> + :ok - swap_index = - case direction do - :up -> index - 1 - :down -> index + 1 - end + index -> + swap_index = + case direction do + :up -> index - 1 + :down -> index + 1 + end - if index && swap_index >= 0 && swap_index < length(items) do - item_a = Enum.at(items, index) - item_b = Enum.at(items, swap_index) + if swap_index >= 0 and swap_index < length(items) do + item_a = Enum.at(items, index) + item_b = Enum.at(items, swap_index) - Repo.transaction(fn -> - item_a - |> Ecto.Changeset.change(position: item_b.position) - |> Repo.update!() + Repo.transaction(fn -> + item_a + |> Ecto.Changeset.change(position: item_b.position) + |> Repo.update!() - item_b - |> Ecto.Changeset.change(position: item_a.position) - |> Repo.update!() - end) + item_b + |> Ecto.Changeset.change(position: item_a.position) + |> Repo.update!() + end) + end end {:ok, get_record_set!(record_set.id)}