diff --git a/lib/cannery/ammo.ex b/lib/cannery/ammo.ex index ea9c853..3c62ab6 100644 --- a/lib/cannery/ammo.ex +++ b/lib/cannery/ammo.ex @@ -3,6 +3,7 @@ defmodule Cannery.Ammo do The Ammo context. """ + import CanneryWeb.Gettext import Ecto.Query, warn: false alias Cannery.{Accounts.User, Containers, Repo} alias Cannery.ActivityLog.ShotGroup @@ -350,18 +351,21 @@ defmodule Cannery.Ammo do def create_ammo_groups( %{"ammo_type_id" => ammo_type_id, "container_id" => container_id} = attrs, multiplier, - %User{id: user_id} = user + %User{} = user ) - when multiplier >= 1 and multiplier <= @ammo_group_create_limit do - # validate ammo type and container ids belong to user - _valid_ammo_type = get_ammo_type!(ammo_type_id, user) - _valid_container = Containers.get_container!(container_id, user) - + when multiplier >= 1 and multiplier <= @ammo_group_create_limit and + not (ammo_type_id |> is_nil()) and not (container_id |> is_nil()) do now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second) changesets = Enum.map(1..multiplier, fn _count -> - %AmmoGroup{} |> AmmoGroup.create_changeset(attrs |> Map.put("user_id", user_id)) + %AmmoGroup{} + |> AmmoGroup.create_changeset( + get_ammo_type!(ammo_type_id, user), + Containers.get_container!(container_id, user), + user, + attrs + ) end) if changesets |> Enum.all?(fn %{valid?: valid} -> valid end) do @@ -386,8 +390,27 @@ defmodule Cannery.Ammo do end end - def create_ammo_groups(invalid_attrs, _multiplier, _user) do - {:error, %AmmoGroup{} |> AmmoGroup.create_changeset(invalid_attrs)} + def create_ammo_groups( + %{"ammo_type_id" => ammo_type_id, "container_id" => container_id} = attrs, + _multiplier, + user + ) + when not (ammo_type_id |> is_nil()) and not (container_id |> is_nil()) do + changeset = + %AmmoGroup{} + |> AmmoGroup.create_changeset( + get_ammo_type!(ammo_type_id, user), + Containers.get_container!(container_id, user), + user, + attrs + ) + |> Changeset.add_error(:multiplier, dgettext("errors", "Invalid multiplier")) + + {:error, changeset} + end + + def create_ammo_groups(invalid_attrs, _multiplier, user) do + {:error, %AmmoGroup{} |> AmmoGroup.create_changeset(nil, nil, user, invalid_attrs)} end @doc """ @@ -436,18 +459,4 @@ defmodule Cannery.Ammo do @spec delete_ammo_group!(AmmoGroup.t(), User.t()) :: AmmoGroup.t() def delete_ammo_group!(%AmmoGroup{user_id: user_id} = ammo_group, %User{id: user_id}), do: ammo_group |> Repo.delete!() - - @doc """ - Returns an `%Changeset{}` for tracking ammo_group changes. - - ## Examples - - iex> change_ammo_group(ammo_group) - %Changeset{data: %AmmoGroup{}} - - """ - @spec change_ammo_group(AmmoGroup.t()) :: Changeset.t(AmmoGroup.t()) - @spec change_ammo_group(AmmoGroup.t(), attrs :: map()) :: Changeset.t(AmmoGroup.t()) - def change_ammo_group(%AmmoGroup{} = ammo_group, attrs \\ %{}), - do: AmmoGroup.update_changeset(ammo_group, attrs) end diff --git a/lib/cannery/ammo/ammo_group.ex b/lib/cannery/ammo/ammo_group.ex index 1dfe618..09426f0 100644 --- a/lib/cannery/ammo/ammo_group.ex +++ b/lib/cannery/ammo/ammo_group.ex @@ -7,6 +7,7 @@ defmodule Cannery.Ammo.AmmoGroup do """ use Ecto.Schema + import CanneryWeb.Gettext import Ecto.Changeset alias Cannery.Ammo.{AmmoGroup, AmmoType} alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Containers.Container} @@ -48,22 +49,49 @@ defmodule Cannery.Ammo.AmmoGroup do @type id :: UUID.t() @doc false - @spec create_changeset(new_ammo_group(), attrs :: map()) :: Changeset.t(new_ammo_group()) - def create_changeset(ammo_group, attrs) do + @spec create_changeset( + new_ammo_group(), + AmmoType.t() | nil, + Container.t() | nil, + User.t(), + attrs :: map() + ) :: Changeset.t(new_ammo_group()) + def create_changeset( + ammo_group, + %AmmoType{id: ammo_type_id}, + %Container{id: container_id, user_id: user_id}, + %User{id: user_id}, + attrs + ) + when not (ammo_type_id |> is_nil()) and not (container_id |> is_nil()) and + not (user_id |> is_nil()) do ammo_group - |> cast(attrs, [:count, :price_paid, :notes, :staged, :ammo_type_id, :container_id, :user_id]) + |> change(ammo_type_id: ammo_type_id) + |> change(user_id: user_id) + |> change(container_id: container_id) + |> cast(attrs, [:count, :price_paid, :notes, :staged]) |> validate_number(:count, greater_than: 0) |> validate_required([:count, :staged, :ammo_type_id, :container_id, :user_id]) end + @doc """ + Invalid changeset, used to prompt user to select ammo type and container + """ + def create_changeset(ammo_group, _invalid_ammo_type, _invalid_container, _invalid_user, attrs) do + ammo_group + |> cast(attrs, [:ammo_type_id, :container_id]) + |> validate_required([:ammo_type_id, :container_id]) + |> add_error(:invalid, dgettext("errors", "Please select an ammo type and container")) + end + @doc false @spec update_changeset(t() | new_ammo_group(), attrs :: map()) :: Changeset.t(t() | new_ammo_group()) def update_changeset(ammo_group, attrs) do ammo_group - |> cast(attrs, [:count, :price_paid, :notes, :staged, :ammo_type_id, :container_id]) + |> cast(attrs, [:count, :price_paid, :notes, :staged]) |> validate_number(:count, greater_than_or_equal_to: 0) - |> validate_required([:count, :staged, :ammo_type_id, :container_id]) + |> validate_required([:count, :staged]) end @doc """ diff --git a/lib/cannery_web/components/move_ammo_group_component.ex b/lib/cannery_web/components/move_ammo_group_component.ex index 8a8c3de..06f6b03 100644 --- a/lib/cannery_web/components/move_ammo_group_component.ex +++ b/lib/cannery_web/components/move_ammo_group_component.ex @@ -22,7 +22,7 @@ defmodule CanneryWeb.Components.MoveAmmoGroupComponent do assigns, socket ) do - changeset = Ammo.change_ammo_group(ammo_group) + changeset = ammo_group |> AmmoGroup.update_changeset(%{}) containers = Containers.list_containers(current_user) diff --git a/lib/cannery_web/live/ammo_group_live/form_component.ex b/lib/cannery_web/live/ammo_group_live/form_component.ex index 2e79341..b17a1db 100644 --- a/lib/cannery_web/live/ammo_group_live/form_component.ex +++ b/lib/cannery_web/live/ammo_group_live/form_component.ex @@ -25,7 +25,7 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do socket = socket |> assign(:ammo_group_create_limit, @ammo_group_create_limit) - |> assign(:changeset, Ammo.change_ammo_group(ammo_group)) + |> assign(:changeset, ammo_group |> AmmoGroup.update_changeset(%{})) |> assign(:ammo_types, Ammo.list_ammo_types(current_user)) |> assign_new(:containers, fn -> Containers.list_containers(current_user) end) @@ -36,7 +36,7 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do def handle_event( "validate", %{"ammo_group" => ammo_group_params}, - %{assigns: %{action: action, ammo_group: ammo_group}} = socket + %{assigns: %{action: action, ammo_group: ammo_group, current_user: user}} = socket ) do changeset_action = case action do @@ -44,7 +44,24 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do :edit -> :update end - changeset = ammo_group |> Ammo.change_ammo_group(ammo_group_params) + changeset = + case action do + :new -> + ammo_type = + if ammo_group_params |> Map.has_key?("ammo_type_id"), + do: ammo_group_params |> Map.get("ammo_type_id") |> Ammo.get_ammo_type!(user), + else: nil + + container = + if ammo_group_params |> Map.has_key?("container_id"), + do: ammo_group_params |> Map.get("container_id") |> Containers.get_container!(user), + else: nil + + ammo_group |> AmmoGroup.create_changeset(ammo_type, container, user, ammo_group_params) + + :edit -> + ammo_group |> AmmoGroup.update_changeset(ammo_group_params) + end changeset = case changeset |> Changeset.apply_action(changeset_action) do diff --git a/priv/gettext/de/LC_MESSAGES/errors.po b/priv/gettext/de/LC_MESSAGES/errors.po index 7a3b94d..0289b9b 100644 --- a/priv/gettext/de/LC_MESSAGES/errors.po +++ b/priv/gettext/de/LC_MESSAGES/errors.po @@ -152,13 +152,13 @@ msgid "Tag could not be added" msgstr "Tag konnte nicht hinzugefügt werden" #, elixir-autogen, elixir-format -#: lib/cannery/activity_log/shot_group.ex:102 +#: lib/cannery/activity_log/shot_group.ex:115 msgid "Count must be at least 1" msgstr "Anzahl muss mindestens 1 sein" #, elixir-autogen, elixir-format -#: lib/cannery/activity_log/shot_group.ex:61 -#: lib/cannery/activity_log/shot_group.ex:98 +#: lib/cannery/activity_log/shot_group.ex:74 +#: lib/cannery/activity_log/shot_group.ex:111 msgid "Count must be less than %{count}" msgstr "Anzahl muss weniger als %{count} betragen" @@ -176,13 +176,28 @@ msgid "Tag could not be removed" msgstr "Tag konnte nicht gelöscht werden" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_group_live/form_component.ex:126 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:143 msgid "Could not parse number of copies" msgstr "Konnte die Anzahl der Kopien nicht verstehen" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_group_live/form_component.ex:111 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:128 msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}" msgstr "" "Ungültige Nummer an Kopien. Muss zwischen 1 and %{max} liegen. War " "%{multiplier}" + +#, elixir-autogen, elixir-format +#: lib/cannery/ammo.ex:407 +msgid "Invalid multiplier" +msgstr "" + +#, elixir-autogen, elixir-format +#: lib/cannery/ammo/ammo_group.ex:84 +msgid "Please select an ammo type and container" +msgstr "" + +#, elixir-autogen, elixir-format +#: lib/cannery/activity_log/shot_group.ex:69 +msgid "Please select a valid user and ammo group" +msgstr "" diff --git a/priv/gettext/de/LC_MESSAGES/prompts.po b/priv/gettext/de/LC_MESSAGES/prompts.po index e4ca75a..82cf45f 100644 --- a/priv/gettext/de/LC_MESSAGES/prompts.po +++ b/priv/gettext/de/LC_MESSAGES/prompts.po @@ -283,12 +283,12 @@ msgid "Ammo unstaged succesfully" msgstr "Munition erfolgreich demarkiert" #, elixir-autogen, elixir-format, fuzzy -#: lib/cannery_web/live/ammo_group_live/form_component.ex:88 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:105 msgid "Ammo updated successfully" msgstr "Munitionsgruppe erfolgreich aktualisiert" #, elixir-autogen, elixir-format, fuzzy -#: lib/cannery_web/live/ammo_group_live/form_component.ex:147 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:164 msgid "Ammo added successfully" msgid_plural "Ammo added successfully" msgstr[0] "Munitionsgruppe erfolgreich aktualisiert" diff --git a/priv/gettext/en/LC_MESSAGES/errors.po b/priv/gettext/en/LC_MESSAGES/errors.po index d303a0f..dfb3bf9 100644 --- a/priv/gettext/en/LC_MESSAGES/errors.po +++ b/priv/gettext/en/LC_MESSAGES/errors.po @@ -139,13 +139,13 @@ msgid "Tag could not be added" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery/activity_log/shot_group.ex:102 +#: lib/cannery/activity_log/shot_group.ex:115 msgid "Count must be at least 1" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery/activity_log/shot_group.ex:61 -#: lib/cannery/activity_log/shot_group.ex:98 +#: lib/cannery/activity_log/shot_group.ex:74 +#: lib/cannery/activity_log/shot_group.ex:111 msgid "Count must be less than %{count}" msgstr "" @@ -161,11 +161,26 @@ msgid "Tag could not be removed" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_group_live/form_component.ex:126 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:143 msgid "Could not parse number of copies" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_group_live/form_component.ex:111 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:128 msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}" msgstr "" + +#, elixir-autogen, elixir-format +#: lib/cannery/ammo.ex:407 +msgid "Invalid multiplier" +msgstr "" + +#, elixir-autogen, elixir-format +#: lib/cannery/ammo/ammo_group.ex:84 +msgid "Please select an ammo type and container" +msgstr "" + +#, elixir-autogen, elixir-format +#: lib/cannery/activity_log/shot_group.ex:69 +msgid "Please select a valid user and ammo group" +msgstr "" diff --git a/priv/gettext/en/LC_MESSAGES/prompts.po b/priv/gettext/en/LC_MESSAGES/prompts.po index ce081ea..19aac89 100644 --- a/priv/gettext/en/LC_MESSAGES/prompts.po +++ b/priv/gettext/en/LC_MESSAGES/prompts.po @@ -263,12 +263,12 @@ msgid "Ammo unstaged succesfully" msgstr "" #, elixir-autogen, elixir-format, fuzzy -#: lib/cannery_web/live/ammo_group_live/form_component.ex:88 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:105 msgid "Ammo updated successfully" msgstr "" #, elixir-autogen, elixir-format, fuzzy -#: lib/cannery_web/live/ammo_group_live/form_component.ex:147 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:164 msgid "Ammo added successfully" msgid_plural "Ammo added successfully" msgstr[0] "" diff --git a/priv/gettext/errors.pot b/priv/gettext/errors.pot index 6b3a922..e2d2ce8 100644 --- a/priv/gettext/errors.pot +++ b/priv/gettext/errors.pot @@ -138,13 +138,13 @@ msgid "Tag could not be added" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery/activity_log/shot_group.ex:102 +#: lib/cannery/activity_log/shot_group.ex:115 msgid "Count must be at least 1" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery/activity_log/shot_group.ex:61 -#: lib/cannery/activity_log/shot_group.ex:98 +#: lib/cannery/activity_log/shot_group.ex:74 +#: lib/cannery/activity_log/shot_group.ex:111 msgid "Count must be less than %{count}" msgstr "" @@ -160,11 +160,26 @@ msgid "Tag could not be removed" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_group_live/form_component.ex:126 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:143 msgid "Could not parse number of copies" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_group_live/form_component.ex:111 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:128 msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}" msgstr "" + +#, elixir-autogen, elixir-format +#: lib/cannery/ammo.ex:407 +msgid "Invalid multiplier" +msgstr "" + +#, elixir-autogen, elixir-format +#: lib/cannery/ammo/ammo_group.ex:84 +msgid "Please select an ammo type and container" +msgstr "" + +#, elixir-autogen, elixir-format +#: lib/cannery/activity_log/shot_group.ex:69 +msgid "Please select a valid user and ammo group" +msgstr "" diff --git a/priv/gettext/es/LC_MESSAGES/errors.po b/priv/gettext/es/LC_MESSAGES/errors.po index aac6461..d1a1f11 100644 --- a/priv/gettext/es/LC_MESSAGES/errors.po +++ b/priv/gettext/es/LC_MESSAGES/errors.po @@ -149,13 +149,13 @@ msgid "Tag could not be added" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery/activity_log/shot_group.ex:102 +#: lib/cannery/activity_log/shot_group.ex:115 msgid "Count must be at least 1" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery/activity_log/shot_group.ex:61 -#: lib/cannery/activity_log/shot_group.ex:98 +#: lib/cannery/activity_log/shot_group.ex:74 +#: lib/cannery/activity_log/shot_group.ex:111 msgid "Count must be less than %{count}" msgstr "" @@ -171,11 +171,26 @@ msgid "Tag could not be removed" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_group_live/form_component.ex:126 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:143 msgid "Could not parse number of copies" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_group_live/form_component.ex:111 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:128 msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}" msgstr "" + +#, elixir-autogen, elixir-format +#: lib/cannery/ammo.ex:407 +msgid "Invalid multiplier" +msgstr "" + +#, elixir-autogen, elixir-format +#: lib/cannery/ammo/ammo_group.ex:84 +msgid "Please select an ammo type and container" +msgstr "" + +#, elixir-autogen, elixir-format +#: lib/cannery/activity_log/shot_group.ex:69 +msgid "Please select a valid user and ammo group" +msgstr "" diff --git a/priv/gettext/es/LC_MESSAGES/prompts.po b/priv/gettext/es/LC_MESSAGES/prompts.po index 095eda8..1504ad3 100644 --- a/priv/gettext/es/LC_MESSAGES/prompts.po +++ b/priv/gettext/es/LC_MESSAGES/prompts.po @@ -273,12 +273,12 @@ msgid "Ammo unstaged succesfully" msgstr "" #, elixir-autogen, elixir-format, fuzzy -#: lib/cannery_web/live/ammo_group_live/form_component.ex:88 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:105 msgid "Ammo updated successfully" msgstr "" #, elixir-autogen, elixir-format, fuzzy -#: lib/cannery_web/live/ammo_group_live/form_component.ex:147 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:164 msgid "Ammo added successfully" msgid_plural "Ammo added successfully" msgstr[0] "" diff --git a/priv/gettext/fr/LC_MESSAGES/errors.po b/priv/gettext/fr/LC_MESSAGES/errors.po index 013a47f..d391eff 100644 --- a/priv/gettext/fr/LC_MESSAGES/errors.po +++ b/priv/gettext/fr/LC_MESSAGES/errors.po @@ -153,13 +153,13 @@ msgid "Tag could not be added" msgstr "Le tag n’a pas pu être ajouté" #, elixir-autogen, elixir-format -#: lib/cannery/activity_log/shot_group.ex:102 +#: lib/cannery/activity_log/shot_group.ex:115 msgid "Count must be at least 1" msgstr "Le nombre doit être au moins égal à 1" #, elixir-autogen, elixir-format -#: lib/cannery/activity_log/shot_group.ex:61 -#: lib/cannery/activity_log/shot_group.ex:98 +#: lib/cannery/activity_log/shot_group.ex:74 +#: lib/cannery/activity_log/shot_group.ex:111 msgid "Count must be less than %{count}" msgstr "La quantité doit être inférieur à %{count}" @@ -177,11 +177,26 @@ msgid "Tag could not be removed" msgstr "Le tag n’a pas pu être retiré" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_group_live/form_component.ex:126 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:143 msgid "Could not parse number of copies" msgstr "Impossible d'analyser le nombre de copies" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_group_live/form_component.ex:111 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:128 msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}" msgstr "Nombre de copies invalide, doit être 1 et %{max}. Été %{multiplier}" + +#, elixir-autogen, elixir-format +#: lib/cannery/ammo.ex:407 +msgid "Invalid multiplier" +msgstr "" + +#, elixir-autogen, elixir-format +#: lib/cannery/ammo/ammo_group.ex:84 +msgid "Please select an ammo type and container" +msgstr "" + +#, elixir-autogen, elixir-format +#: lib/cannery/activity_log/shot_group.ex:69 +msgid "Please select a valid user and ammo group" +msgstr "" diff --git a/priv/gettext/fr/LC_MESSAGES/prompts.po b/priv/gettext/fr/LC_MESSAGES/prompts.po index 0328cab..3f5aae1 100644 --- a/priv/gettext/fr/LC_MESSAGES/prompts.po +++ b/priv/gettext/fr/LC_MESSAGES/prompts.po @@ -284,12 +284,12 @@ msgid "Ammo unstaged succesfully" msgstr "Groupe de munition désélectionner avec succès" #, elixir-autogen, elixir-format, fuzzy -#: lib/cannery_web/live/ammo_group_live/form_component.ex:88 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:105 msgid "Ammo updated successfully" msgstr "Groupe de munition mis à jour avec succès" #, elixir-autogen, elixir-format, fuzzy -#: lib/cannery_web/live/ammo_group_live/form_component.ex:147 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:164 msgid "Ammo added successfully" msgid_plural "Ammo added successfully" msgstr[0] "Groupe de munition mis à jour avec succès" diff --git a/priv/gettext/prompts.pot b/priv/gettext/prompts.pot index 364f8e4..7981b0a 100644 --- a/priv/gettext/prompts.pot +++ b/priv/gettext/prompts.pot @@ -262,12 +262,12 @@ msgid "Ammo unstaged succesfully" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_group_live/form_component.ex:88 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:105 msgid "Ammo updated successfully" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_group_live/form_component.ex:147 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:164 msgid "Ammo added successfully" msgid_plural "Ammo added successfully" msgstr[0] "" diff --git a/test/cannery/ammo_test.exs b/test/cannery/ammo_test.exs index 2ae4e6e..317c5a5 100644 --- a/test/cannery/ammo_test.exs +++ b/test/cannery/ammo_test.exs @@ -180,9 +180,5 @@ defmodule Cannery.AmmoTest do Ammo.get_ammo_group!(ammo_group.id, current_user) end end - - test "change_ammo_group/1 returns a ammo_group changeset", %{ammo_group: ammo_group} do - assert %Changeset{} = Ammo.change_ammo_group(ammo_group) - end end end