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 CanneryWeb.Gettext
alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Ammo, Ammo.AmmoGroup, Repo}
alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Ammo.AmmoGroup, Repo}
alias Ecto.{Changeset, Multi}
@doc """
@ -60,25 +59,24 @@ defmodule Cannery.ActivityLog do
"""
@spec create_shot_group(attrs :: map(), User.t(), AmmoGroup.t()) ::
{:ok, ShotGroup.t()} | {:error, Changeset.t(ShotGroup.t()) | nil}
def create_shot_group(
attrs,
%User{id: user_id},
%AmmoGroup{id: ammo_group_id, count: ammo_group_count, user_id: user_id} = ammo_group
) do
attrs = attrs |> Map.merge(%{"user_id" => user_id, "ammo_group_id" => ammo_group_id})
changeset = %ShotGroup{} |> ShotGroup.create_changeset(attrs)
shot_group_count = changeset |> Changeset.get_field(:count)
if shot_group_count > ammo_group_count do
error = dgettext("errors", "Count must be less than %{count}", count: ammo_group_count)
changeset = changeset |> Changeset.add_error(:count, error)
{:error, changeset}
else
def create_shot_group(attrs, user, ammo_group) do
Multi.new()
|> Multi.insert(:create_shot_group, changeset)
|> Multi.insert(
:create_shot_group,
%ShotGroup{} |> ShotGroup.create_changeset(user, ammo_group, attrs)
)
|> Multi.run(
:ammo_group,
fn repo, %{create_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(
: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
@ -87,7 +85,6 @@ defmodule Cannery.ActivityLog do
{:error, _other_transaction, _value, _changes_so_far} -> {:error, nil}
end
end
end
@doc """
Updates a shot_group.
@ -104,35 +101,32 @@ defmodule Cannery.ActivityLog do
@spec update_shot_group(ShotGroup.t(), attrs :: map(), User.t()) ::
{:ok, ShotGroup.t()} | {:error, Changeset.t(ShotGroup.t()) | nil}
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,
%User{id: user_id} = user
) do
%{count: ammo_group_count, user_id: ^user_id} =
ammo_group = ammo_group_id |> Ammo.get_ammo_group!(user)
changeset = shot_group |> ShotGroup.update_changeset(attrs)
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 |> Changeset.add_error(:count, error)
{:error, changeset}
new_shot_group_count <= 0 ->
error = dgettext("errors", "Count must be at least 1")
changeset = changeset |> Changeset.add_error(:count, error)
{:error, changeset}
true ->
Multi.new()
|> Multi.update(:update_shot_group, changeset)
|> Multi.update(
:update_shot_group,
shot_group |> ShotGroup.update_changeset(user, attrs)
)
|> Multi.run(
:ammo_group,
fn repo, %{update_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(
:update_ammo_group,
ammo_group
|> AmmoGroup.range_changeset(%{"count" => ammo_group_count - shot_diff_to_add})
fn %{
update_shot_group: %{count: new_count},
ammo_group: %{count: ammo_group_count} = ammo_group
} ->
shot_diff_to_add = new_count - count
new_ammo_group_count = ammo_group_count - shot_diff_to_add
ammo_group |> AmmoGroup.range_changeset(%{"count" => new_ammo_group_count})
end
)
|> Repo.transaction()
|> case do
@ -141,7 +135,6 @@ defmodule Cannery.ActivityLog do
{:error, _other_transaction, _value, _changes_so_far} -> {:error, nil}
end
end
end
@doc """
Deletes a shot_group.
@ -158,18 +151,27 @@ defmodule Cannery.ActivityLog do
@spec delete_shot_group(ShotGroup.t(), User.t()) ::
{:ok, ShotGroup.t()} | {:error, Changeset.t(ShotGroup.t())}
def delete_shot_group(
%ShotGroup{count: count, user_id: user_id, ammo_group_id: ammo_group_id} = shot_group,
%User{id: user_id} = user
%ShotGroup{user_id: user_id} = shot_group,
%User{id: user_id}
) do
%{count: ammo_group_count, user_id: ^user_id} =
ammo_group = ammo_group_id |> Ammo.get_ammo_group!(user)
Multi.new()
|> 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(
:update_ammo_group,
ammo_group
|> AmmoGroup.range_changeset(%{"count" => ammo_group_count + count})
fn %{
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()
|> case do
@ -178,21 +180,4 @@ defmodule Cannery.ActivityLog do
{:error, _other_transaction, _value, _changes_so_far} -> {:error, nil}
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

View File

@ -4,8 +4,9 @@ defmodule Cannery.ActivityLog.ShotGroup do
"""
use Ecto.Schema
import CanneryWeb.Gettext
import Ecto.Changeset
alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Ammo.AmmoGroup}
alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Ammo.AmmoGroup, Repo}
alias Ecto.{Changeset, UUID}
@primary_key {:id, :binary_id, autogenerate: true}
@ -37,21 +38,71 @@ defmodule Cannery.ActivityLog.ShotGroup do
@type id :: UUID.t()
@doc false
@spec create_changeset(new_shot_group(), attrs :: map()) :: Changeset.t(new_shot_group())
def create_changeset(shot_group, attrs) do
@spec create_changeset(new_shot_group(), User.t(), AmmoGroup.t(), attrs :: map()) ::
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
|> 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_create_shot_group_count(ammo_group)
|> validate_required([:count, :ammo_group_id, :user_id])
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
@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())
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
|> cast(attrs, [:count, :notes, :date])
|> validate_number(:count, greater_than: 0)
|> 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

View File

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

View File

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

View File

@ -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.ex:125
#: lib/cannery/activity_log/shot_group.ex:102
msgid "Count must be at least 1"
msgstr "Anzahl muss mindestens 1 sein"
#, elixir-autogen, elixir-format
#: lib/cannery/activity_log.ex:73
#: lib/cannery/activity_log.ex:120
#: lib/cannery/activity_log/shot_group.ex:61
#: lib/cannery/activity_log/shot_group.ex:98
msgid "Count must be less than %{count}"
msgstr "Anzahl muss weniger als %{count} betragen"

View File

@ -205,7 +205,7 @@ msgid "Adding..."
msgstr "Füge hinzu..."
#, 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"
msgstr "Schüsse erfolgreich dokumentiert"

View File

@ -139,13 +139,13 @@ msgid "Tag could not be added"
msgstr ""
#, 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"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery/activity_log.ex:73
#: lib/cannery/activity_log.ex:120
#: lib/cannery/activity_log/shot_group.ex:61
#: lib/cannery/activity_log/shot_group.ex:98
msgid "Count must be less than %{count}"
msgstr ""

View File

@ -185,7 +185,7 @@ msgid "Adding..."
msgstr ""
#, 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"
msgstr ""

View File

@ -138,13 +138,13 @@ msgid "Tag could not be added"
msgstr ""
#, 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"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery/activity_log.ex:73
#: lib/cannery/activity_log.ex:120
#: lib/cannery/activity_log/shot_group.ex:61
#: lib/cannery/activity_log/shot_group.ex:98
msgid "Count must be less than %{count}"
msgstr ""

View File

@ -149,13 +149,13 @@ msgid "Tag could not be added"
msgstr ""
#, 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"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery/activity_log.ex:73
#: lib/cannery/activity_log.ex:120
#: lib/cannery/activity_log/shot_group.ex:61
#: lib/cannery/activity_log/shot_group.ex:98
msgid "Count must be less than %{count}"
msgstr ""

View File

@ -195,7 +195,7 @@ msgid "Adding..."
msgstr ""
#, 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"
msgstr ""

View File

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

View File

@ -206,7 +206,7 @@ msgid "Adding..."
msgstr "Ajout en cours…"
#, 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"
msgstr "Tirs enregistré avec succès"

View File

@ -184,7 +184,7 @@ msgid "Adding..."
msgstr ""
#, 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"
msgstr ""

View File

@ -178,10 +178,5 @@ defmodule Cannery.ActivityLogTest do
ActivityLog.get_shot_group!(shot_group.id, current_user)
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