Fix nil-index crash in move_record_in_set
This commit is contained in:
@@ -165,27 +165,31 @@ defmodule MusicLibrary.RecordSets do
|
|||||||
)
|
)
|
||||||
|> Repo.all()
|
|> 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 =
|
index ->
|
||||||
case direction do
|
swap_index =
|
||||||
:up -> index - 1
|
case direction do
|
||||||
:down -> index + 1
|
:up -> index - 1
|
||||||
end
|
:down -> index + 1
|
||||||
|
end
|
||||||
|
|
||||||
if index && swap_index >= 0 && swap_index < length(items) do
|
if swap_index >= 0 and swap_index < length(items) do
|
||||||
item_a = Enum.at(items, index)
|
item_a = Enum.at(items, index)
|
||||||
item_b = Enum.at(items, swap_index)
|
item_b = Enum.at(items, swap_index)
|
||||||
|
|
||||||
Repo.transaction(fn ->
|
Repo.transaction(fn ->
|
||||||
item_a
|
item_a
|
||||||
|> Ecto.Changeset.change(position: item_b.position)
|
|> Ecto.Changeset.change(position: item_b.position)
|
||||||
|> Repo.update!()
|
|> Repo.update!()
|
||||||
|
|
||||||
item_b
|
item_b
|
||||||
|> Ecto.Changeset.change(position: item_a.position)
|
|> Ecto.Changeset.change(position: item_a.position)
|
||||||
|> Repo.update!()
|
|> Repo.update!()
|
||||||
end)
|
end)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
{:ok, get_record_set!(record_set.id)}
|
{:ok, get_record_set!(record_set.id)}
|
||||||
|
|||||||
Reference in New Issue
Block a user