fix shotgroup changesets

This commit is contained in:
shibao 2022-07-01 21:39:08 -04:00
parent 67010640f0
commit 3cbd62e84c
15 changed files with 157 additions and 138 deletions

View File

@ -4,8 +4,7 @@ defmodule Cannery.ActivityLog do
""" """
import Ecto.Query, warn: false import Ecto.Query, warn: false
import CanneryWeb.Gettext alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Ammo.AmmoGroup, Repo}
alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Ammo, Ammo.AmmoGroup, Repo}
alias Ecto.{Changeset, Multi} alias Ecto.{Changeset, Multi}
@doc """ @doc """
@ -60,32 +59,30 @@ defmodule Cannery.ActivityLog do
""" """
@spec create_shot_group(attrs :: map(), User.t(), AmmoGroup.t()) :: @spec create_shot_group(attrs :: map(), User.t(), AmmoGroup.t()) ::
{:ok, ShotGroup.t()} | {:error, Changeset.t(ShotGroup.t()) | nil} {:ok, ShotGroup.t()} | {:error, Changeset.t(ShotGroup.t()) | nil}
def create_shot_group( def create_shot_group(attrs, user, ammo_group) do
attrs, Multi.new()
%User{id: user_id}, |> Multi.insert(
%AmmoGroup{id: ammo_group_id, count: ammo_group_count, user_id: user_id} = ammo_group :create_shot_group,
) do %ShotGroup{} |> ShotGroup.create_changeset(user, ammo_group, attrs)
attrs = attrs |> Map.merge(%{"user_id" => user_id, "ammo_group_id" => ammo_group_id}) )
changeset = %ShotGroup{} |> ShotGroup.create_changeset(attrs) |> Multi.run(
shot_group_count = changeset |> Changeset.get_field(:count) :ammo_group,
fn repo, %{create_shot_group: %{ammo_group_id: ammo_group_id, user_id: user_id}} ->
if shot_group_count > ammo_group_count do {:ok,
error = dgettext("errors", "Count must be less than %{count}", count: ammo_group_count) repo.one(from ag in AmmoGroup, where: ag.id == ^ammo_group_id and ag.user_id == ^user_id)}
changeset = changeset |> Changeset.add_error(:count, error)
{:error, changeset}
else
Multi.new()
|> Multi.insert(:create_shot_group, changeset)
|> Multi.update(
:update_ammo_group,
ammo_group |> AmmoGroup.range_changeset(%{"count" => ammo_group_count - shot_group_count})
)
|> Repo.transaction()
|> case do
{:ok, %{create_shot_group: shot_group}} -> {:ok, shot_group}
{:error, :create_shot_group, changeset, _changes_so_far} -> {:error, changeset}
{:error, _other_transaction, _value, _changes_so_far} -> {:error, nil}
end end
)
|> Multi.update(
:update_ammo_group,
fn %{create_shot_group: %{count: shot_group_count}, ammo_group: %{count: ammo_group_count}} ->
ammo_group |> AmmoGroup.range_changeset(%{"count" => ammo_group_count - shot_group_count})
end
)
|> Repo.transaction()
|> case do
{:ok, %{create_shot_group: shot_group}} -> {:ok, shot_group}
{:error, :create_shot_group, changeset, _changes_so_far} -> {:error, changeset}
{:error, _other_transaction, _value, _changes_so_far} -> {:error, nil}
end end
end end
@ -104,42 +101,38 @@ defmodule Cannery.ActivityLog do
@spec update_shot_group(ShotGroup.t(), attrs :: map(), User.t()) :: @spec update_shot_group(ShotGroup.t(), attrs :: map(), User.t()) ::
{:ok, ShotGroup.t()} | {:error, Changeset.t(ShotGroup.t()) | nil} {:ok, ShotGroup.t()} | {:error, Changeset.t(ShotGroup.t()) | nil}
def update_shot_group( def update_shot_group(
%ShotGroup{count: count, user_id: user_id, ammo_group_id: ammo_group_id} = shot_group, %ShotGroup{count: count, user_id: user_id} = shot_group,
attrs, attrs,
%User{id: user_id} = user %User{id: user_id} = user
) do ) do
%{count: ammo_group_count, user_id: ^user_id} = Multi.new()
ammo_group = ammo_group_id |> Ammo.get_ammo_group!(user) |> Multi.update(
:update_shot_group,
changeset = shot_group |> ShotGroup.update_changeset(attrs) shot_group |> ShotGroup.update_changeset(user, attrs)
new_shot_group_count = changeset |> Changeset.get_field(:count) )
shot_diff_to_add = new_shot_group_count - count |> Multi.run(
:ammo_group,
cond do fn repo, %{update_shot_group: %{ammo_group_id: ammo_group_id, user_id: user_id}} ->
shot_diff_to_add > ammo_group_count -> {:ok,
error = dgettext("errors", "Count must be less than %{count}", count: ammo_group_count) repo.one(from ag in AmmoGroup, where: ag.id == ^ammo_group_id and ag.user_id == ^user_id)}
changeset = changeset |> Changeset.add_error(:count, error) end
{:error, changeset} )
|> Multi.update(
new_shot_group_count <= 0 -> :update_ammo_group,
error = dgettext("errors", "Count must be at least 1") fn %{
changeset = changeset |> Changeset.add_error(:count, error) update_shot_group: %{count: new_count},
{:error, changeset} ammo_group: %{count: ammo_group_count} = ammo_group
} ->
true -> shot_diff_to_add = new_count - count
Multi.new() new_ammo_group_count = ammo_group_count - shot_diff_to_add
|> Multi.update(:update_shot_group, changeset) ammo_group |> AmmoGroup.range_changeset(%{"count" => new_ammo_group_count})
|> Multi.update( end
:update_ammo_group, )
ammo_group |> Repo.transaction()
|> AmmoGroup.range_changeset(%{"count" => ammo_group_count - shot_diff_to_add}) |> case do
) {:ok, %{update_shot_group: shot_group}} -> {:ok, shot_group}
|> Repo.transaction() {:error, :update_shot_group, changeset, _changes_so_far} -> {:error, changeset}
|> case do {:error, _other_transaction, _value, _changes_so_far} -> {:error, nil}
{:ok, %{update_shot_group: shot_group}} -> {:ok, shot_group}
{:error, :update_shot_group, changeset, _changes_so_far} -> {:error, changeset}
{:error, _other_transaction, _value, _changes_so_far} -> {:error, nil}
end
end end
end end
@ -158,18 +151,27 @@ defmodule Cannery.ActivityLog do
@spec delete_shot_group(ShotGroup.t(), User.t()) :: @spec delete_shot_group(ShotGroup.t(), User.t()) ::
{:ok, ShotGroup.t()} | {:error, Changeset.t(ShotGroup.t())} {:ok, ShotGroup.t()} | {:error, Changeset.t(ShotGroup.t())}
def delete_shot_group( def delete_shot_group(
%ShotGroup{count: count, user_id: user_id, ammo_group_id: ammo_group_id} = shot_group, %ShotGroup{user_id: user_id} = shot_group,
%User{id: user_id} = user %User{id: user_id}
) do ) do
%{count: ammo_group_count, user_id: ^user_id} =
ammo_group = ammo_group_id |> Ammo.get_ammo_group!(user)
Multi.new() Multi.new()
|> Multi.delete(:delete_shot_group, shot_group) |> Multi.delete(:delete_shot_group, shot_group)
|> Multi.run(
:ammo_group,
fn repo, %{delete_shot_group: %{ammo_group_id: ammo_group_id, user_id: user_id}} ->
{:ok,
repo.one(from ag in AmmoGroup, where: ag.id == ^ammo_group_id and ag.user_id == ^user_id)}
end
)
|> Multi.update( |> Multi.update(
:update_ammo_group, :update_ammo_group,
ammo_group fn %{
|> AmmoGroup.range_changeset(%{"count" => ammo_group_count + count}) delete_shot_group: %{count: count},
ammo_group: %{count: ammo_group_count} = ammo_group
} ->
new_ammo_group_count = ammo_group_count + count
ammo_group |> AmmoGroup.range_changeset(%{"count" => new_ammo_group_count})
end
) )
|> Repo.transaction() |> Repo.transaction()
|> case do |> case do
@ -178,21 +180,4 @@ defmodule Cannery.ActivityLog do
{:error, _other_transaction, _value, _changes_so_far} -> {:error, nil} {:error, _other_transaction, _value, _changes_so_far} -> {:error, nil}
end end
end end
@doc """
Returns an `%Ecto.Changeset{}` for tracking shot_group changes.
## Examples
iex> change_shot_group(shot_group)
%Ecto.Changeset{data: %ShotGroup{}}
"""
@spec change_shot_group(ShotGroup.t() | ShotGroup.new_shot_group()) ::
Changeset.t(ShotGroup.t() | ShotGroup.new_shot_group())
@spec change_shot_group(ShotGroup.t() | ShotGroup.new_shot_group(), attrs :: map()) ::
Changeset.t(ShotGroup.t() | ShotGroup.new_shot_group())
def change_shot_group(%ShotGroup{} = shot_group, attrs \\ %{}) do
shot_group |> ShotGroup.update_changeset(attrs)
end
end end

View File

@ -4,8 +4,9 @@ defmodule Cannery.ActivityLog.ShotGroup do
""" """
use Ecto.Schema use Ecto.Schema
import CanneryWeb.Gettext
import Ecto.Changeset import Ecto.Changeset
alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Ammo.AmmoGroup} alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Ammo.AmmoGroup, Repo}
alias Ecto.{Changeset, UUID} alias Ecto.{Changeset, UUID}
@primary_key {:id, :binary_id, autogenerate: true} @primary_key {:id, :binary_id, autogenerate: true}
@ -37,21 +38,71 @@ defmodule Cannery.ActivityLog.ShotGroup do
@type id :: UUID.t() @type id :: UUID.t()
@doc false @doc false
@spec create_changeset(new_shot_group(), attrs :: map()) :: Changeset.t(new_shot_group()) @spec create_changeset(new_shot_group(), User.t(), AmmoGroup.t(), attrs :: map()) ::
def create_changeset(shot_group, attrs) do Changeset.t(new_shot_group())
def create_changeset(
shot_group,
%User{id: user_id},
%AmmoGroup{id: ammo_group_id, user_id: user_id} = ammo_group,
attrs
)
when not (user_id |> is_nil()) and not (ammo_group_id |> is_nil()) do
shot_group shot_group
|> cast(attrs, [:count, :notes, :date, :ammo_group_id, :user_id]) |> change(user_id: user_id)
|> change(ammo_group_id: ammo_group_id)
|> cast(attrs, [:count, :notes, :date])
|> validate_number(:count, greater_than: 0) |> validate_number(:count, greater_than: 0)
|> validate_create_shot_group_count(ammo_group)
|> validate_required([:count, :ammo_group_id, :user_id]) |> validate_required([:count, :ammo_group_id, :user_id])
end end
defp validate_create_shot_group_count(changeset, %AmmoGroup{count: ammo_group_count}) do
if changeset |> Changeset.get_field(:count) > ammo_group_count do
error = dgettext("errors", "Count must be less than %{count}", count: ammo_group_count)
changeset |> Changeset.add_error(:count, error)
else
changeset
end
end
@doc false @doc false
@spec update_changeset(t() | new_shot_group(), attrs :: map()) :: @spec update_changeset(t() | new_shot_group(), User.t(), attrs :: map()) ::
Changeset.t(t() | new_shot_group()) Changeset.t(t() | new_shot_group())
def update_changeset(shot_group, attrs) do def update_changeset(
%ShotGroup{user_id: user_id} = shot_group,
%User{id: user_id} = user,
attrs
)
when not (user_id |> is_nil()) do
shot_group shot_group
|> cast(attrs, [:count, :notes, :date]) |> cast(attrs, [:count, :notes, :date])
|> validate_number(:count, greater_than: 0) |> validate_number(:count, greater_than: 0)
|> validate_required([:count]) |> validate_required([:count])
|> validate_update_shot_group_count(shot_group, user)
end
defp validate_update_shot_group_count(
changeset,
%ShotGroup{count: count} = shot_group,
%User{id: user_id}
)
when not (user_id |> is_nil()) do
%{ammo_group: %AmmoGroup{count: ammo_group_count, user_id: ^user_id}} =
shot_group |> Repo.preload(:ammo_group)
new_shot_group_count = changeset |> Changeset.get_field(:count)
shot_diff_to_add = new_shot_group_count - count
cond do
shot_diff_to_add > ammo_group_count ->
error = dgettext("errors", "Count must be less than %{count}", count: ammo_group_count)
changeset |> Changeset.add_error(:count, error)
new_shot_group_count <= 0 ->
changeset |> Changeset.add_error(:count, dgettext("errors", "Count must be at least 1"))
true ->
changeset
end
end end
end end

View File

@ -16,9 +16,10 @@ defmodule CanneryWeb.Components.AddShotGroupComponent do
}, },
Socket.t() Socket.t()
) :: {:ok, Socket.t()} ) :: {:ok, Socket.t()}
def update(%{ammo_group: _ammo_group, current_user: _current_user} = assigns, socket) do def update(%{ammo_group: ammo_group, current_user: current_user} = assigns, socket) do
changeset = changeset =
%ShotGroup{date: NaiveDateTime.utc_now(), count: 1} |> ActivityLog.change_shot_group() %ShotGroup{date: NaiveDateTime.utc_now(), count: 1}
|> ShotGroup.create_changeset(current_user, ammo_group, %{})
{:ok, socket |> assign(assigns) |> assign(:changeset, changeset)} {:ok, socket |> assign(assigns) |> assign(:changeset, changeset)}
end end
@ -27,21 +28,13 @@ defmodule CanneryWeb.Components.AddShotGroupComponent do
def handle_event( def handle_event(
"validate", "validate",
%{"shot_group" => shot_group_params}, %{"shot_group" => shot_group_params},
%{ %{assigns: %{ammo_group: ammo_group, current_user: current_user}} = socket
assigns: %{
ammo_group: %AmmoGroup{id: ammo_group_id} = ammo_group,
current_user: %User{id: user_id}
}
} = socket
) do ) do
shot_group_params = params = shot_group_params |> process_params(ammo_group)
shot_group_params
|> process_params(ammo_group)
|> Map.merge(%{"ammo_group_id" => ammo_group_id, "user_id" => user_id})
changeset = changeset =
%ShotGroup{} %ShotGroup{}
|> ActivityLog.change_shot_group(shot_group_params) |> ShotGroup.create_changeset(current_user, ammo_group, params)
|> Map.put(:action, :validate) |> Map.put(:action, :validate)
{:noreply, socket |> assign(:changeset, changeset)} {:noreply, socket |> assign(:changeset, changeset)}
@ -51,17 +44,12 @@ defmodule CanneryWeb.Components.AddShotGroupComponent do
"save", "save",
%{"shot_group" => shot_group_params}, %{"shot_group" => shot_group_params},
%{ %{
assigns: %{ assigns: %{ammo_group: ammo_group, current_user: current_user, return_to: return_to}
ammo_group: %{id: ammo_group_id} = ammo_group,
current_user: %{id: user_id} = current_user,
return_to: return_to
}
} = socket } = socket
) do ) do
socket = socket =
shot_group_params shot_group_params
|> process_params(ammo_group) |> process_params(ammo_group)
|> Map.merge(%{"ammo_group_id" => ammo_group_id, "user_id" => user_id})
|> ActivityLog.create_shot_group(current_user, ammo_group) |> ActivityLog.create_shot_group(current_user, ammo_group)
|> case do |> case do
{:ok, _shot_group} -> {:ok, _shot_group} ->

View File

@ -1,6 +1,6 @@
defmodule CanneryWeb.RangeLive.FormComponent do defmodule CanneryWeb.RangeLive.FormComponent do
@moduledoc """ @moduledoc """
Livecomponent that can update or create a ShotGroup Livecomponent that can update a ShotGroup
""" """
use CanneryWeb, :live_component use CanneryWeb, :live_component
@ -24,7 +24,7 @@ defmodule CanneryWeb.RangeLive.FormComponent do
} = assigns, } = assigns,
socket socket
) do ) do
changeset = shot_group |> ActivityLog.change_shot_group() changeset = shot_group |> ShotGroup.update_changeset(current_user, %{})
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user) ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
{:ok, socket |> assign(assigns) |> assign(ammo_group: ammo_group, changeset: changeset)} {:ok, socket |> assign(assigns) |> assign(ammo_group: ammo_group, changeset: changeset)}
end end
@ -33,11 +33,11 @@ defmodule CanneryWeb.RangeLive.FormComponent do
def handle_event( def handle_event(
"validate", "validate",
%{"shot_group" => shot_group_params}, %{"shot_group" => shot_group_params},
%{assigns: %{shot_group: shot_group}} = socket %{assigns: %{current_user: current_user, shot_group: shot_group}} = socket
) do ) do
changeset = changeset =
shot_group shot_group
|> ActivityLog.change_shot_group(shot_group_params) |> ShotGroup.update_changeset(current_user, shot_group_params)
|> Map.put(:action, :validate) |> Map.put(:action, :validate)
{:noreply, assign(socket, :changeset, changeset)} {:noreply, assign(socket, :changeset, changeset)}

View File

@ -152,13 +152,13 @@ msgid "Tag could not be added"
msgstr "Tag konnte nicht hinzugefügt werden" msgstr "Tag konnte nicht hinzugefügt werden"
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery/activity_log.ex:125 #: lib/cannery/activity_log/shot_group.ex:102
msgid "Count must be at least 1" msgid "Count must be at least 1"
msgstr "Anzahl muss mindestens 1 sein" msgstr "Anzahl muss mindestens 1 sein"
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery/activity_log.ex:73 #: lib/cannery/activity_log/shot_group.ex:61
#: lib/cannery/activity_log.ex:120 #: lib/cannery/activity_log/shot_group.ex:98
msgid "Count must be less than %{count}" msgid "Count must be less than %{count}"
msgstr "Anzahl muss weniger als %{count} betragen" msgstr "Anzahl muss weniger als %{count} betragen"

View File

@ -205,7 +205,7 @@ msgid "Adding..."
msgstr "Füge hinzu..." msgstr "Füge hinzu..."
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery_web/components/add_shot_group_component.ex:68 #: lib/cannery_web/components/add_shot_group_component.ex:56
msgid "Shots recorded successfully" msgid "Shots recorded successfully"
msgstr "Schüsse erfolgreich dokumentiert" msgstr "Schüsse erfolgreich dokumentiert"

View File

@ -139,13 +139,13 @@ msgid "Tag could not be added"
msgstr "" msgstr ""
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery/activity_log.ex:125 #: lib/cannery/activity_log/shot_group.ex:102
msgid "Count must be at least 1" msgid "Count must be at least 1"
msgstr "" msgstr ""
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery/activity_log.ex:73 #: lib/cannery/activity_log/shot_group.ex:61
#: lib/cannery/activity_log.ex:120 #: lib/cannery/activity_log/shot_group.ex:98
msgid "Count must be less than %{count}" msgid "Count must be less than %{count}"
msgstr "" msgstr ""

View File

@ -185,7 +185,7 @@ msgid "Adding..."
msgstr "" msgstr ""
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery_web/components/add_shot_group_component.ex:68 #: lib/cannery_web/components/add_shot_group_component.ex:56
msgid "Shots recorded successfully" msgid "Shots recorded successfully"
msgstr "" msgstr ""

View File

@ -138,13 +138,13 @@ msgid "Tag could not be added"
msgstr "" msgstr ""
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery/activity_log.ex:125 #: lib/cannery/activity_log/shot_group.ex:102
msgid "Count must be at least 1" msgid "Count must be at least 1"
msgstr "" msgstr ""
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery/activity_log.ex:73 #: lib/cannery/activity_log/shot_group.ex:61
#: lib/cannery/activity_log.ex:120 #: lib/cannery/activity_log/shot_group.ex:98
msgid "Count must be less than %{count}" msgid "Count must be less than %{count}"
msgstr "" msgstr ""

View File

@ -149,13 +149,13 @@ msgid "Tag could not be added"
msgstr "" msgstr ""
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery/activity_log.ex:125 #: lib/cannery/activity_log/shot_group.ex:102
msgid "Count must be at least 1" msgid "Count must be at least 1"
msgstr "" msgstr ""
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery/activity_log.ex:73 #: lib/cannery/activity_log/shot_group.ex:61
#: lib/cannery/activity_log.ex:120 #: lib/cannery/activity_log/shot_group.ex:98
msgid "Count must be less than %{count}" msgid "Count must be less than %{count}"
msgstr "" msgstr ""

View File

@ -195,7 +195,7 @@ msgid "Adding..."
msgstr "" msgstr ""
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery_web/components/add_shot_group_component.ex:68 #: lib/cannery_web/components/add_shot_group_component.ex:56
msgid "Shots recorded successfully" msgid "Shots recorded successfully"
msgstr "" msgstr ""

View File

@ -153,13 +153,13 @@ msgid "Tag could not be added"
msgstr "Le tag na pas pu être ajouté" msgstr "Le tag na pas pu être ajouté"
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery/activity_log.ex:125 #: lib/cannery/activity_log/shot_group.ex:102
msgid "Count must be at least 1" msgid "Count must be at least 1"
msgstr "Le nombre doit être au moins égal à 1" msgstr "Le nombre doit être au moins égal à 1"
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery/activity_log.ex:73 #: lib/cannery/activity_log/shot_group.ex:61
#: lib/cannery/activity_log.ex:120 #: lib/cannery/activity_log/shot_group.ex:98
msgid "Count must be less than %{count}" msgid "Count must be less than %{count}"
msgstr "La quantité doit être inférieur à %{count}" msgstr "La quantité doit être inférieur à %{count}"

View File

@ -206,7 +206,7 @@ msgid "Adding..."
msgstr "Ajout en cours…" msgstr "Ajout en cours…"
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery_web/components/add_shot_group_component.ex:68 #: lib/cannery_web/components/add_shot_group_component.ex:56
msgid "Shots recorded successfully" msgid "Shots recorded successfully"
msgstr "Tirs enregistré avec succès" msgstr "Tirs enregistré avec succès"

View File

@ -184,7 +184,7 @@ msgid "Adding..."
msgstr "" msgstr ""
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery_web/components/add_shot_group_component.ex:68 #: lib/cannery_web/components/add_shot_group_component.ex:56
msgid "Shots recorded successfully" msgid "Shots recorded successfully"
msgstr "" msgstr ""

View File

@ -178,10 +178,5 @@ defmodule Cannery.ActivityLogTest do
ActivityLog.get_shot_group!(shot_group.id, current_user) ActivityLog.get_shot_group!(shot_group.id, current_user)
end end
end end
test "change_shot_group/1 returns a shot_group changeset",
%{shot_group: shot_group} do
assert %Ecto.Changeset{} = ActivityLog.change_shot_group(shot_group)
end
end end
end end