mirror of
https://github.com/fly-apps/live_beats.git
synced 2024-11-21 15:41:00 +00:00
Rename songs_number to songs_count and refactor error messages
This commit is contained in:
parent
19241308ae
commit
023ddc2806
5 changed files with 50 additions and 43 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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 \\ %{})
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -30,8 +30,8 @@
|
|||
</h3>
|
||||
<div class="mt-2 text-sm text-red-700">
|
||||
<ul role="list" class="list-disc pl-5 space-y-1">
|
||||
<%= for {client_name, kind} <- @error_messages do %>
|
||||
<li><%= client_name %>: <.file_error kind={kind} /></li>
|
||||
<%= for {label, kind} <- @error_messages do %>
|
||||
<li><.file_error label={label} kind={kind} /></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</div>
|
||||
|
|
|
@ -3,18 +3,18 @@ defmodule LiveBeats.Repo.Migrations.AddSongsNumberToUsers do
|
|||
|
||||
def up do
|
||||
alter table(:users) do
|
||||
add :songs_number, :integer, null: false, default: 0
|
||||
add :songs_count, :integer, null: false, default: 0
|
||||
end
|
||||
|
||||
execute("
|
||||
UPDATE users set songs_number =
|
||||
UPDATE users set songs_count =
|
||||
(SELECT count (*) from songs
|
||||
where songs.user_id = users.id)")
|
||||
end
|
||||
|
||||
def down do
|
||||
alter table(:users) do
|
||||
remove :songs_number
|
||||
remove :songs_count
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue