Use Enum for scrobble rule type
This commit is contained in:
@@ -152,7 +152,7 @@ defmodule MusicLibrary.ScrobbleRules do
|
||||
{:error, :invalid_rule_type}
|
||||
|
||||
"""
|
||||
def apply_album_rule(%ScrobbleRule{type: "album"} = rule) do
|
||||
def apply_album_rule(%ScrobbleRule{type: :album} = rule) do
|
||||
update_query =
|
||||
from(t in Track,
|
||||
where: fragment("json_extract(?, '$.title') = ?", t.album, ^rule.match_value),
|
||||
@@ -186,7 +186,7 @@ defmodule MusicLibrary.ScrobbleRules do
|
||||
{:error, :invalid_rule_type}
|
||||
|
||||
"""
|
||||
def apply_artist_rule(%ScrobbleRule{type: "artist"} = rule) do
|
||||
def apply_artist_rule(%ScrobbleRule{type: :artist} = rule) do
|
||||
update_query =
|
||||
from(t in Track,
|
||||
where: fragment("json_extract(?, '$.name') = ?", t.artist, ^rule.match_value),
|
||||
@@ -220,11 +220,11 @@ defmodule MusicLibrary.ScrobbleRules do
|
||||
{:error, "Invalid rule type"}
|
||||
|
||||
"""
|
||||
def apply_rule(%ScrobbleRule{type: "album"} = rule) do
|
||||
def apply_rule(%ScrobbleRule{type: :album} = rule) do
|
||||
apply_album_rule(rule)
|
||||
end
|
||||
|
||||
def apply_rule(%ScrobbleRule{type: "artist"} = rule) do
|
||||
def apply_rule(%ScrobbleRule{type: :artist} = rule) do
|
||||
apply_artist_rule(rule)
|
||||
end
|
||||
|
||||
@@ -234,7 +234,7 @@ defmodule MusicLibrary.ScrobbleRules do
|
||||
## Examples
|
||||
|
||||
iex> apply_all_rules()
|
||||
{:ok, [{"album", 5}, {"artist", 10}]}
|
||||
{:ok, [{:album, 5}, {:artist, 10}]}
|
||||
|
||||
"""
|
||||
def apply_all_rules do
|
||||
@@ -257,7 +257,7 @@ defmodule MusicLibrary.ScrobbleRules do
|
||||
5
|
||||
|
||||
"""
|
||||
def count_album_matches(%ScrobbleRule{type: "album"} = rule) do
|
||||
def count_album_matches(%ScrobbleRule{type: :album} = rule) do
|
||||
query =
|
||||
from(t in Track,
|
||||
where: fragment("json_extract(?, '$.title') = ?", t.album, ^rule.match_value),
|
||||
@@ -276,7 +276,7 @@ defmodule MusicLibrary.ScrobbleRules do
|
||||
10
|
||||
|
||||
"""
|
||||
def count_artist_matches(%ScrobbleRule{type: "artist"} = rule) do
|
||||
def count_artist_matches(%ScrobbleRule{type: :artist} = rule) do
|
||||
query =
|
||||
from(t in Track,
|
||||
where: fragment("json_extract(?, '$.name') = ?", t.artist, ^rule.match_value),
|
||||
@@ -295,11 +295,11 @@ defmodule MusicLibrary.ScrobbleRules do
|
||||
5
|
||||
|
||||
"""
|
||||
def count_rule_matches(%ScrobbleRule{type: "album"} = rule) do
|
||||
def count_rule_matches(%ScrobbleRule{type: :album} = rule) do
|
||||
count_album_matches(rule)
|
||||
end
|
||||
|
||||
def count_rule_matches(%ScrobbleRule{type: "artist"} = rule) do
|
||||
def count_rule_matches(%ScrobbleRule{type: :artist} = rule) do
|
||||
count_artist_matches(rule)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,7 +5,7 @@ defmodule MusicLibrary.ScrobbleRules.ScrobbleRule do
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
id: integer() | nil,
|
||||
type: String.t(),
|
||||
type: :album | :artist,
|
||||
match_value: String.t(),
|
||||
target_musicbrainz_id: String.t(),
|
||||
enabled: boolean(),
|
||||
@@ -14,10 +14,8 @@ defmodule MusicLibrary.ScrobbleRules.ScrobbleRule do
|
||||
updated_at: NaiveDateTime.t() | nil
|
||||
}
|
||||
|
||||
@valid_types ~w(album artist)
|
||||
|
||||
schema "scrobble_rules" do
|
||||
field :type, :string
|
||||
field :type, Ecto.Enum, values: [:album, :artist]
|
||||
field :match_value, :string
|
||||
field :target_musicbrainz_id, Ecto.UUID
|
||||
field :enabled, :boolean, default: true
|
||||
@@ -31,6 +29,5 @@ defmodule MusicLibrary.ScrobbleRules.ScrobbleRule do
|
||||
scrobble_rule
|
||||
|> cast(attrs, [:type, :match_value, :target_musicbrainz_id, :enabled, :description])
|
||||
|> validate_required([:type, :match_value, :target_musicbrainz_id])
|
||||
|> validate_inclusion(:type, @valid_types)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -24,8 +24,8 @@ defmodule MusicLibraryWeb.ScrobbleRulesLive.FormComponent do
|
||||
field={@form[:type]}
|
||||
label={gettext("Rule Type")}
|
||||
options={[
|
||||
{gettext("Album"), "album"},
|
||||
{gettext("Artist"), "artist"}
|
||||
{gettext("Album"), :album},
|
||||
{gettext("Artist"), :artist}
|
||||
]}
|
||||
placeholder={gettext("Select a rule type")}
|
||||
/>
|
||||
@@ -119,17 +119,17 @@ defmodule MusicLibraryWeb.ScrobbleRulesLive.FormComponent do
|
||||
|
||||
defp match_value_label(type) do
|
||||
case type do
|
||||
"album" -> gettext("Album Title")
|
||||
"artist" -> gettext("Artist Name")
|
||||
_ -> gettext("Match Value")
|
||||
:album -> gettext("Album Title")
|
||||
:artist -> gettext("Artist Name")
|
||||
_other -> gettext("Match Value")
|
||||
end
|
||||
end
|
||||
|
||||
defp match_value_placeholder(type) do
|
||||
case type do
|
||||
"album" -> gettext("e.g. The Dark Side of the Moon")
|
||||
"artist" -> gettext("e.g. Pink Floyd")
|
||||
_ -> gettext("Enter the value to match")
|
||||
:album -> gettext("e.g. The Dark Side of the Moon")
|
||||
:artist -> gettext("e.g. Pink Floyd")
|
||||
_other -> gettext("Enter the value to match")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user