Remove excessively defensive coding
This commit is contained in:
@@ -19,18 +19,28 @@ defmodule MusicLibrary.ScrobbleRules do
|
||||
|
||||
"""
|
||||
def list_scrobble_rules(opts \\ []) do
|
||||
query = from(r in ScrobbleRule, order_by: [desc: r.inserted_at])
|
||||
query =
|
||||
from r in ScrobbleRule,
|
||||
order_by: [desc: r.inserted_at]
|
||||
|
||||
query =
|
||||
case Keyword.get(opts, :type) do
|
||||
nil -> query
|
||||
type -> from(r in query, where: r.type == ^type)
|
||||
nil ->
|
||||
query
|
||||
|
||||
type ->
|
||||
from r in query,
|
||||
where: r.type == ^type
|
||||
end
|
||||
|
||||
query =
|
||||
case Keyword.get(opts, :enabled) do
|
||||
nil -> query
|
||||
enabled -> from(r in query, where: r.enabled == ^enabled)
|
||||
nil ->
|
||||
query
|
||||
|
||||
enabled ->
|
||||
from r in query,
|
||||
where: r.enabled == ^enabled
|
||||
end
|
||||
|
||||
Repo.all(query)
|
||||
@@ -164,10 +174,6 @@ defmodule MusicLibrary.ScrobbleRules do
|
||||
end
|
||||
end
|
||||
|
||||
def apply_album_rule(%ScrobbleRule{type: type}) do
|
||||
{:error, "Invalid rule type: #{type}. Expected 'album'"}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Applies an artist rule to all matching scrobbled tracks.
|
||||
|
||||
@@ -202,10 +208,6 @@ defmodule MusicLibrary.ScrobbleRules do
|
||||
end
|
||||
end
|
||||
|
||||
def apply_artist_rule(%ScrobbleRule{type: type}) do
|
||||
{:error, "Invalid rule type: #{type}. Expected 'artist'"}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Applies a single rule based on its type.
|
||||
|
||||
@@ -226,10 +228,6 @@ defmodule MusicLibrary.ScrobbleRules do
|
||||
apply_artist_rule(rule)
|
||||
end
|
||||
|
||||
def apply_rule(%ScrobbleRule{type: type}) do
|
||||
{:error, "Invalid rule type: #{type}"}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Applies all enabled rules.
|
||||
|
||||
@@ -242,17 +240,12 @@ defmodule MusicLibrary.ScrobbleRules do
|
||||
def apply_all_rules do
|
||||
rules = list_enabled_rules()
|
||||
|
||||
results =
|
||||
Enum.map(rules, fn rule ->
|
||||
case apply_rule(rule) do
|
||||
{:ok, count} -> {:ok, {rule.type, rule.match_value, count}}
|
||||
{:error, reason} -> {:error, {rule.type, rule.match_value, reason}}
|
||||
end
|
||||
end)
|
||||
|
||||
{:ok, results}
|
||||
rescue
|
||||
e -> {:error, "Failed to apply rules: #{Exception.message(e)}"}
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -274,10 +267,6 @@ defmodule MusicLibrary.ScrobbleRules do
|
||||
Repo.one(query) || 0
|
||||
end
|
||||
|
||||
def count_album_matches(%ScrobbleRule{type: type}) do
|
||||
{:error, "Invalid rule type: #{type}. Expected 'album'"}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Counts how many tracks would be affected by an artist rule.
|
||||
|
||||
@@ -297,10 +286,6 @@ defmodule MusicLibrary.ScrobbleRules do
|
||||
Repo.one(query) || 0
|
||||
end
|
||||
|
||||
def count_artist_matches(%ScrobbleRule{type: type}) do
|
||||
{:error, "Invalid rule type: #{type}. Expected 'artist'"}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Counts how many tracks would be affected by a rule.
|
||||
|
||||
@@ -317,8 +302,4 @@ defmodule MusicLibrary.ScrobbleRules do
|
||||
def count_rule_matches(%ScrobbleRule{type: "artist"} = rule) do
|
||||
count_artist_matches(rule)
|
||||
end
|
||||
|
||||
def count_rule_matches(%ScrobbleRule{type: type}) do
|
||||
{:error, "Invalid rule type: #{type}"}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -16,14 +16,8 @@ defmodule MusicLibrary.ScrobbleRules.Worker do
|
||||
def perform(%Oban.Job{args: _}) do
|
||||
Logger.info("Starting scrobble rules application")
|
||||
|
||||
case ScrobbleRules.apply_all_rules() do
|
||||
{:ok, results} ->
|
||||
results = ScrobbleRules.apply_all_rules()
|
||||
log_results(results)
|
||||
|
||||
{:error, reason} ->
|
||||
Logger.error("Failed to apply scrobble rules: #{inspect(reason)}")
|
||||
{:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
defp log_results(results) do
|
||||
|
||||
@@ -77,8 +77,8 @@ defmodule MusicLibraryWeb.ScrobbleRulesLive.Index do
|
||||
|
||||
@impl true
|
||||
def handle_event("apply_all_rules", _params, socket) do
|
||||
case ScrobbleRules.apply_all_rules() do
|
||||
{:ok, results} ->
|
||||
results = ScrobbleRules.apply_all_rules()
|
||||
|
||||
total_updated =
|
||||
results
|
||||
|> Enum.filter(fn {status, _} -> status == :ok end)
|
||||
@@ -91,11 +91,6 @@ defmodule MusicLibraryWeb.ScrobbleRulesLive.Index do
|
||||
)
|
||||
|
||||
{:noreply, put_flash(socket, :info, message)}
|
||||
|
||||
{:error, reason} ->
|
||||
message = gettext("Error applying rules: %{reason}", reason: reason)
|
||||
{:noreply, put_flash(socket, :error, message)}
|
||||
end
|
||||
end
|
||||
|
||||
defp rule_type_badge(type) do
|
||||
|
||||
@@ -1034,11 +1034,6 @@ msgstr ""
|
||||
msgid "Error applying rule: %{reason}"
|
||||
msgstr ""
|
||||
|
||||
#: lib/music_library_web/live/scrobble_rules_live/index.ex
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "Error applying rules: %{reason}"
|
||||
msgstr ""
|
||||
|
||||
#: lib/music_library_web/live/scrobble_rules_live/index.html.heex
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "Manage rules to fix data in scrobbled tracks"
|
||||
|
||||
@@ -1034,11 +1034,6 @@ msgstr ""
|
||||
msgid "Error applying rule: %{reason}"
|
||||
msgstr ""
|
||||
|
||||
#: lib/music_library_web/live/scrobble_rules_live/index.ex
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "Error applying rules: %{reason}"
|
||||
msgstr ""
|
||||
|
||||
#: lib/music_library_web/live/scrobble_rules_live/index.html.heex
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "Manage rules to fix data in scrobbled tracks"
|
||||
|
||||
@@ -188,16 +188,6 @@ defmodule MusicLibrary.ScrobbleRulesTest do
|
||||
assert updated_track.artist.musicbrainz_id == rule.target_musicbrainz_id
|
||||
end
|
||||
|
||||
test "apply_album_rule/1 with wrong type returns error" do
|
||||
rule = scrobble_rule_fixture(@valid_artist_attrs)
|
||||
assert {:error, _reason} = ScrobbleRules.apply_album_rule(rule)
|
||||
end
|
||||
|
||||
test "apply_artist_rule/1 with wrong type returns error" do
|
||||
rule = scrobble_rule_fixture(@valid_album_attrs)
|
||||
assert {:error, _reason} = ScrobbleRules.apply_artist_rule(rule)
|
||||
end
|
||||
|
||||
test "apply_rule/1 delegates to correct function based on type" do
|
||||
album_rule = scrobble_rule_fixture(@valid_album_attrs)
|
||||
artist_rule = scrobble_rule_fixture(@valid_artist_attrs)
|
||||
@@ -239,7 +229,7 @@ defmodule MusicLibrary.ScrobbleRulesTest do
|
||||
album: %{musicbrainz_id: "", title: "Different Album"}
|
||||
})
|
||||
|
||||
assert {:ok, results} = ScrobbleRules.apply_all_rules()
|
||||
assert results = ScrobbleRules.apply_all_rules()
|
||||
assert length(results) == 2
|
||||
|
||||
# Verify all results are successful
|
||||
|
||||
Reference in New Issue
Block a user