Rename songs_number to songs_count and refactor error messages

This commit is contained in:
Chris McCord 2021-11-19 10:51:50 -05:00
parent 19241308ae
commit 023ddc2806
5 changed files with 50 additions and 43 deletions

View file

@ -15,7 +15,7 @@ defmodule LiveBeats.Accounts.User do
field :active_profile_user_id, :id field :active_profile_user_id, :id
field :avatar_url, :string field :avatar_url, :string
field :external_homepage_url, :string field :external_homepage_url, :string
field :songs_number, :integer field :songs_count, :integer
has_many :identities, Identity has_many :identities, Identity

View file

@ -149,6 +149,9 @@ defmodule LiveBeats.MediaLibrary do
def import_songs(%Accounts.User{} = user, changesets, consume_file) def import_songs(%Accounts.User{} = user, changesets, consume_file)
when is_map(changesets) and is_function(consume_file, 2) do when is_map(changesets) and is_function(consume_file, 2) do
# refetch user for fresh song count
user = Accounts.get_user!(user.id)
multi = multi =
Enum.reduce(changesets, Ecto.Multi.new(), fn {ref, chset}, acc -> Enum.reduce(changesets, Ecto.Multi.new(), fn {ref, chset}, acc ->
chset = chset =
@ -158,22 +161,21 @@ defmodule LiveBeats.MediaLibrary do
Ecto.Multi.insert(acc, {:song, ref}, chset) Ecto.Multi.insert(acc, {:song, ref}, chset)
end) end)
|> Ecto.Multi.run(:valid_songs_number, fn _repo, changes -> |> Ecto.Multi.run(:valid_songs_count, fn _repo, changes ->
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) validate_songs_limit(user.songs_count, new_songs_count)
end) end)
|> Ecto.Multi.update_all( |> Ecto.Multi.update_all(
:update_songs_number, :update_songs_count,
fn %{valid_songs_number: new_songs_count} -> fn %{valid_songs_count: new_count} ->
from(u in Accounts.User, from(u in Accounts.User,
where: u.id == ^user.id and u.songs_number == ^user.songs_number, where: u.id == ^user.id and u.songs_count == ^user.songs_count,
update: [inc: [songs_number: ^new_songs_count]] update: [inc: [songs_count: ^new_count]]
) )
end, 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 case result do
{1, _} -> {:ok, user} {1, _} -> {:ok, user}
_ -> {:error, :invalid} _ -> {:error, :invalid}
@ -195,7 +197,7 @@ defmodule LiveBeats.MediaLibrary do
failed_op = failed_op =
case failed_op do case failed_op do
{:song, _number} -> :invalid_song {:song, _number} -> :invalid_song
:is_songs_number_updated? -> :invalid :is_songs_count_updated? -> :invalid
failed_op -> failed_op failed_op -> failed_op
end end
@ -346,17 +348,16 @@ defmodule LiveBeats.MediaLibrary do
Ecto.Multi.new() Ecto.Multi.new()
|> Ecto.Multi.delete(:delete, song) |> Ecto.Multi.delete(:delete, song)
|> Ecto.Multi.update_all( |> Ecto.Multi.update_all(
:update_songs_number, :update_songs_count,
fn _ -> fn _ ->
from(u in Accounts.User, from(u in Accounts.User,
where: u.id == ^song.user_id, where: u.id == ^song.user_id,
update: [inc: [songs_number: -1]] update: [inc: [songs_count: -1]]
) )
end, end,
[] []
) )
|> Repo.transaction() |> Repo.transaction()
end end
def change_song(song_or_changeset, attrs \\ %{}) def change_song(song_or_changeset, attrs \\ %{})

View file

@ -56,12 +56,8 @@ defmodule LiveBeatsWeb.SongLive.UploadFormComponent do
|> put_flash(:info, "#{map_size(songs)} song(s) uploaded") |> put_flash(:info, "#{map_size(songs)} song(s) uploaded")
|> push_redirect(to: profile_path(current_user))} |> push_redirect(to: profile_path(current_user))}
{:error, error} -> {:error, {failed_op, reason}} ->
updated_socket = {:noreply, put_error(socket, {failed_op, reason})}
socket
|> update(:error_messages, &Enum.take(&1 ++ [error], -10))
{:noreply, updated_socket}
end end
end end
end end
@ -147,23 +143,29 @@ defmodule LiveBeatsWeb.SongLive.UploadFormComponent do
end) end)
end end
defp file_error(%{kind: :dropped} = assigns), do: ~H|dropped (exceeds limit of 10 files)| defp file_error(%{kind: :dropped} = assigns),
defp file_error(%{kind: :too_large} = assigns), do: ~H|larger than 10MB| do: ~H|<%= @label %>: dropped (exceeds limit of 10 files)|
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: :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), defp file_error(%{kind: :songs_limit_exceeded} = assigns),
do: ~H|You exceeded the limit of songs per account| 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 defp file_error(%{kind: %Ecto.Changeset{}} = assigns),
~H|<%= LiveBeatsWeb.ErrorHelpers.translate_changeset_errors(@kind) %>| do: ~H|<%= @label %>: <%= LiveBeatsWeb.ErrorHelpers.translate_changeset_errors(@kind) %>|
end
defp file_error(%{kind: {msg, opts}} = assigns) when is_binary(msg) and is_list(opts) do defp file_error(%{kind: {msg, opts}} = assigns) when is_binary(msg) and is_list(opts),
~H|<%= LiveBeatsWeb.ErrorHelpers.translate_error(@kind) %>| do: ~H|<%= @label %>: <%= LiveBeatsWeb.ErrorHelpers.translate_error(@kind) %>|
end
defp put_stats(socket, entry_ref, %MP3Stat{} = stat) do defp put_stats(socket, entry_ref, %MP3Stat{} = stat) do
if changeset = get_changeset(socket, entry_ref) do if changeset = get_changeset(socket, entry_ref) do
@ -203,11 +205,15 @@ defmodule LiveBeatsWeb.SongLive.UploadFormComponent do
socket socket
|> cancel_upload(:mp3, entry.ref) |> cancel_upload(:mp3, entry.ref)
|> drop_changeset(entry.ref) |> drop_changeset(entry.ref)
|> update(:error_messages, &Enum.take(&1 ++ [{entry.client_name, reason}], -10)) |> put_error({entry.client_name, reason})
end end
defp get_entry!(socket, entry_ref) do defp get_entry!(socket, entry_ref) do
Enum.find(socket.assigns.uploads.mp3.entries, fn entry -> entry.ref == entry_ref end) || Enum.find(socket.assigns.uploads.mp3.entries, fn entry -> entry.ref == entry_ref end) ||
raise "no entry found for ref #{inspect(entry_ref)}" raise "no entry found for ref #{inspect(entry_ref)}"
end end
defp put_error(socket, {label, msg}) do
update(socket, :error_messages, &Enum.take(&1 ++ [{label, msg}], -10))
end
end end

View file

@ -30,8 +30,8 @@
</h3> </h3>
<div class="mt-2 text-sm text-red-700"> <div class="mt-2 text-sm text-red-700">
<ul role="list" class="list-disc pl-5 space-y-1"> <ul role="list" class="list-disc pl-5 space-y-1">
<%= for {client_name, kind} <- @error_messages do %> <%= for {label, kind} <- @error_messages do %>
<li><%= client_name %>: <.file_error kind={kind} /></li> <li><.file_error label={label} kind={kind} /></li>
<% end %> <% end %>
</ul> </ul>
</div> </div>

View file

@ -3,18 +3,18 @@ defmodule LiveBeats.Repo.Migrations.AddSongsNumberToUsers do
def up do def up do
alter table(:users) do alter table(:users) do
add :songs_number, :integer, null: false, default: 0 add :songs_count, :integer, null: false, default: 0
end end
execute(" execute("
UPDATE users set songs_number = UPDATE users set songs_count =
(SELECT count (*) from songs (SELECT count (*) from songs
where songs.user_id = users.id)") where songs.user_id = users.id)")
end end
def down do def down do
alter table(:users) do alter table(:users) do
remove :songs_number remove :songs_count
end end
end end
end end