diff --git a/lib/live_beats/accounts/user.ex b/lib/live_beats/accounts/user.ex index f4bd555..6a10dac 100644 --- a/lib/live_beats/accounts/user.ex +++ b/lib/live_beats/accounts/user.ex @@ -15,7 +15,7 @@ defmodule LiveBeats.Accounts.User do field :active_profile_user_id, :id field :avatar_url, :string field :external_homepage_url, :string - field :songs_number, :integer + field :songs_count, :integer has_many :identities, Identity diff --git a/lib/live_beats/media_library.ex b/lib/live_beats/media_library.ex index 5c694cf..f5e20ef 100644 --- a/lib/live_beats/media_library.ex +++ b/lib/live_beats/media_library.ex @@ -149,6 +149,9 @@ defmodule LiveBeats.MediaLibrary do def import_songs(%Accounts.User{} = user, changesets, consume_file) when is_map(changesets) and is_function(consume_file, 2) do + # refetch user for fresh song count + user = Accounts.get_user!(user.id) + multi = Enum.reduce(changesets, Ecto.Multi.new(), fn {ref, chset}, acc -> chset = @@ -158,22 +161,21 @@ defmodule LiveBeats.MediaLibrary do Ecto.Multi.insert(acc, {:song, ref}, chset) end) - |> Ecto.Multi.run(:valid_songs_number, fn _repo, changes -> - user = Accounts.get_user!(user.id) + |> Ecto.Multi.run(:valid_songs_count, fn _repo, changes -> new_songs_count = changes |> Enum.filter(&match?({{:song, _ref}, _}, &1)) |> Enum.count() - validate_songs_limit(user.songs_number, new_songs_count) + validate_songs_limit(user.songs_count, new_songs_count) end) |> Ecto.Multi.update_all( - :update_songs_number, - fn %{valid_songs_number: new_songs_count} -> + :update_songs_count, + fn %{valid_songs_count: new_count} -> from(u in Accounts.User, - where: u.id == ^user.id and u.songs_number == ^user.songs_number, - update: [inc: [songs_number: ^new_songs_count]] + where: u.id == ^user.id and u.songs_count == ^user.songs_count, + update: [inc: [songs_count: ^new_count]] ) end, [] ) - |> Ecto.Multi.run(:is_songs_number_updated?, fn _repo, %{update_songs_number: result} -> + |> Ecto.Multi.run(:is_songs_count_updated?, fn _repo, %{update_songs_count: result} -> case result do {1, _} -> {:ok, user} _ -> {:error, :invalid} @@ -195,7 +197,7 @@ defmodule LiveBeats.MediaLibrary do failed_op = case failed_op do {:song, _number} -> :invalid_song - :is_songs_number_updated? -> :invalid + :is_songs_count_updated? -> :invalid failed_op -> failed_op end @@ -346,17 +348,16 @@ defmodule LiveBeats.MediaLibrary do Ecto.Multi.new() |> Ecto.Multi.delete(:delete, song) |> Ecto.Multi.update_all( - :update_songs_number, - fn _ -> - from(u in Accounts.User, - where: u.id == ^song.user_id, - update: [inc: [songs_number: -1]] - ) - end, - [] - ) + :update_songs_count, + fn _ -> + from(u in Accounts.User, + where: u.id == ^song.user_id, + update: [inc: [songs_count: -1]] + ) + end, + [] + ) |> Repo.transaction() - end def change_song(song_or_changeset, attrs \\ %{}) diff --git a/lib/live_beats_web/live/song_live/upload_form_component.ex b/lib/live_beats_web/live/song_live/upload_form_component.ex index 1d3eec5..f5520d7 100644 --- a/lib/live_beats_web/live/song_live/upload_form_component.ex +++ b/lib/live_beats_web/live/song_live/upload_form_component.ex @@ -56,12 +56,8 @@ defmodule LiveBeatsWeb.SongLive.UploadFormComponent do |> put_flash(:info, "#{map_size(songs)} song(s) uploaded") |> push_redirect(to: profile_path(current_user))} - {:error, error} -> - updated_socket = - socket - |> update(:error_messages, &Enum.take(&1 ++ [error], -10)) - - {:noreply, updated_socket} + {:error, {failed_op, reason}} -> + {:noreply, put_error(socket, {failed_op, reason})} end end end @@ -147,23 +143,29 @@ defmodule LiveBeatsWeb.SongLive.UploadFormComponent do end) end - defp file_error(%{kind: :dropped} = assigns), do: ~H|dropped (exceeds limit of 10 files)| - defp file_error(%{kind: :too_large} = assigns), do: ~H|larger than 10MB| - defp file_error(%{kind: :not_accepted} = assigns), do: ~H|not a valid MP3 file| - defp file_error(%{kind: :too_many_files} = assigns), do: ~H|too many files| + defp file_error(%{kind: :dropped} = assigns), + do: ~H|<%= @label %>: dropped (exceeds limit of 10 files)| + + defp file_error(%{kind: :too_large} = assigns), + do: ~H|<%= @label %>: larger than 10MB| + + defp file_error(%{kind: :not_accepted} = assigns), + do: ~H|<%= @label %>: not a valid MP3 file| + + defp file_error(%{kind: :too_many_files} = assigns), + do: ~H|too many files| defp file_error(%{kind: :songs_limit_exceeded} = assigns), do: ~H|You exceeded the limit of songs per account| - defp file_error(%{kind: :invalid} = assigns), do: ~H|Something went wrong| + defp file_error(%{kind: :invalid} = assigns), + do: ~H|Something went wrong| - defp file_error(%{kind: %Ecto.Changeset{} = changeset} = assigns) do - ~H|<%= LiveBeatsWeb.ErrorHelpers.translate_changeset_errors(@kind) %>| - end + defp file_error(%{kind: %Ecto.Changeset{}} = assigns), + do: ~H|<%= @label %>: <%= LiveBeatsWeb.ErrorHelpers.translate_changeset_errors(@kind) %>| - defp file_error(%{kind: {msg, opts}} = assigns) when is_binary(msg) and is_list(opts) do - ~H|<%= LiveBeatsWeb.ErrorHelpers.translate_error(@kind) %>| - end + defp file_error(%{kind: {msg, opts}} = assigns) when is_binary(msg) and is_list(opts), + do: ~H|<%= @label %>: <%= LiveBeatsWeb.ErrorHelpers.translate_error(@kind) %>| defp put_stats(socket, entry_ref, %MP3Stat{} = stat) do if changeset = get_changeset(socket, entry_ref) do @@ -203,11 +205,15 @@ defmodule LiveBeatsWeb.SongLive.UploadFormComponent do socket |> cancel_upload(:mp3, entry.ref) |> drop_changeset(entry.ref) - |> update(:error_messages, &Enum.take(&1 ++ [{entry.client_name, reason}], -10)) + |> put_error({entry.client_name, reason}) end defp get_entry!(socket, entry_ref) do Enum.find(socket.assigns.uploads.mp3.entries, fn entry -> entry.ref == entry_ref end) || raise "no entry found for ref #{inspect(entry_ref)}" end + + defp put_error(socket, {label, msg}) do + update(socket, :error_messages, &Enum.take(&1 ++ [{label, msg}], -10)) + end end diff --git a/lib/live_beats_web/live/song_live/upload_form_component.html.heex b/lib/live_beats_web/live/song_live/upload_form_component.html.heex index d0de3ab..9a4e84a 100644 --- a/lib/live_beats_web/live/song_live/upload_form_component.html.heex +++ b/lib/live_beats_web/live/song_live/upload_form_component.html.heex @@ -30,8 +30,8 @@