decrement songs_number with multi.update_all

This commit is contained in:
Berenice Medel 2021-11-18 15:50:05 -06:00
parent 64969fca31
commit 43bb03fb42

View file

@ -161,12 +161,7 @@ defmodule LiveBeats.MediaLibrary do
|> Ecto.Multi.run(:valid_songs_number, fn _repo, changes -> |> Ecto.Multi.run(:valid_songs_number, fn _repo, changes ->
user = Accounts.get_user!(user.id) user = Accounts.get_user!(user.id)
new_songs_count = changes |> Enum.filter(&match?({{:song, _ref}, _}, &1)) |> Enum.count() new_songs_count = changes |> Enum.filter(&match?({{:song, _ref}, _}, &1)) |> Enum.count()
validate_songs_limit(user.songs_number, new_songs_count)
if user.songs_number + new_songs_count <= @max_songs do
{:ok, new_songs_count}
else
{:error, :songs_limit_exceeded}
end
end) end)
|> Ecto.Multi.update_all( |> Ecto.Multi.update_all(
:update_songs_number, :update_songs_number,
@ -199,7 +194,7 @@ defmodule LiveBeats.MediaLibrary do
{:error, failed_op, failed_val, _changes} -> {:error, failed_op, failed_val, _changes} ->
failed_op = failed_op =
case failed_op do case failed_op do
{:song, number} -> "Song ##{String.to_integer(number) + 1}" {:song, _number} -> :invalid_song
:is_songs_number_updated? -> :invalid :is_songs_number_updated? -> :invalid
failed_op -> failed_op failed_op -> failed_op
end end
@ -350,15 +345,16 @@ defmodule LiveBeats.MediaLibrary do
Ecto.Multi.new() Ecto.Multi.new()
|> Ecto.Multi.delete(:delete, song) |> Ecto.Multi.delete(:delete, song)
|> Ecto.Multi.update(:update_songs_number, fn _ -> |> Ecto.Multi.update_all(
user = Accounts.get_user!(song.user_id) :update_songs_number,
fn _ ->
user_params = %{ from(u in Accounts.User,
songs_number: user.songs_number - 1 where: u.id == ^song.user_id,
} update: [inc: [songs_number: -1]]
)
Accounts.User.songs_changeset(user, user_params) end,
end) []
)
|> Repo.transaction() |> Repo.transaction()
end end
@ -384,4 +380,12 @@ defmodule LiveBeats.MediaLibrary do
end end
defp topic(user_id) when is_integer(user_id), do: "profile:#{user_id}" defp topic(user_id) when is_integer(user_id), do: "profile:#{user_id}"
defp validate_songs_limit(user_songs, new_songs_count) do
if user_songs + new_songs_count <= @max_songs do
{:ok, new_songs_count}
else
{:error, :songs_limit_exceeded}
end
end
end end