- harden ammo context
- add user_id to ammo types
This commit is contained in:
@ -13,12 +13,13 @@ defmodule Cannery.Ammo do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list_ammo_types()
|
||||
iex> list_ammo_types(%User{id: 123})
|
||||
[%AmmoType{}, ...]
|
||||
|
||||
"""
|
||||
@spec list_ammo_types() :: [AmmoType.t()]
|
||||
def list_ammo_types, do: Repo.all(AmmoType)
|
||||
@spec list_ammo_types(User.t()) :: [AmmoType.t()]
|
||||
def list_ammo_types(%User{id: user_id}),
|
||||
do: Repo.all(from at in AmmoType, where: at.user_id == ^user_id)
|
||||
|
||||
@doc """
|
||||
Gets a single ammo_type.
|
||||
@ -27,15 +28,16 @@ defmodule Cannery.Ammo do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_ammo_type!(123)
|
||||
iex> get_ammo_type!(123, %User{id: 123})
|
||||
%AmmoType{}
|
||||
|
||||
iex> get_ammo_type!(456)
|
||||
iex> get_ammo_type!(456, %User{id: 123})
|
||||
** (Ecto.NoResultsError)
|
||||
|
||||
"""
|
||||
@spec get_ammo_type!(AmmoType.id()) :: AmmoType.t()
|
||||
def get_ammo_type!(id), do: Repo.get!(AmmoType, id)
|
||||
@spec get_ammo_type!(AmmoType.id(), User.t()) :: AmmoType.t()
|
||||
def get_ammo_type!(id, %User{id: user_id}),
|
||||
do: Repo.one!(from at in AmmoType, where: at.id == ^id and at.user_id == ^user_id)
|
||||
|
||||
@doc """
|
||||
Gets the average cost of a single ammo type
|
||||
@ -44,15 +46,18 @@ defmodule Cannery.Ammo do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_ammo_type!(123)
|
||||
iex> get_ammo_type!(123, %User{id: 123})
|
||||
%AmmoType{}
|
||||
|
||||
iex> get_ammo_type!(456)
|
||||
iex> get_ammo_type!(456, %User{id: 123})
|
||||
** (Ecto.NoResultsError)
|
||||
|
||||
"""
|
||||
@spec get_average_cost_for_ammo_type!(AmmoType.t()) :: float()
|
||||
def get_average_cost_for_ammo_type!(%AmmoType{id: ammo_type_id}) do
|
||||
@spec get_average_cost_for_ammo_type!(AmmoType.t(), User.t()) :: float()
|
||||
def get_average_cost_for_ammo_type!(
|
||||
%AmmoType{id: ammo_type_id, user_id: user_id},
|
||||
%User{id: user_id}
|
||||
) do
|
||||
Repo.one!(
|
||||
from ag in AmmoGroup,
|
||||
where: ag.ammo_type_id == ^ammo_type_id,
|
||||
@ -66,62 +71,67 @@ defmodule Cannery.Ammo do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> create_ammo_type(%{field: value})
|
||||
iex> create_ammo_type(%{field: value}, %User{id: 123})
|
||||
{:ok, %AmmoType{}}
|
||||
|
||||
iex> create_ammo_type(%{field: bad_value})
|
||||
iex> create_ammo_type(%{field: bad_value}, %User{id: 123})
|
||||
{:error, %Changeset{}}
|
||||
|
||||
"""
|
||||
@spec create_ammo_type(attrs :: map()) ::
|
||||
@spec create_ammo_type(attrs :: map(), User.t()) ::
|
||||
{:ok, AmmoType.t()} | {:error, Changeset.t(AmmoType.new_ammo_type())}
|
||||
def create_ammo_type(attrs \\ %{}),
|
||||
do: %AmmoType{} |> AmmoType.changeset(attrs) |> Repo.insert()
|
||||
def create_ammo_type(attrs \\ %{}, %User{id: user_id}) do
|
||||
%AmmoType{}
|
||||
|> AmmoType.create_changeset(attrs |> Map.put("user_id", user_id))
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates a ammo_type.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> update_ammo_type(ammo_type, %{field: new_value})
|
||||
iex> update_ammo_type(ammo_type, %{field: new_value}, %User{id: 123})
|
||||
{:ok, %AmmoType{}}
|
||||
|
||||
iex> update_ammo_type(ammo_type, %{field: bad_value})
|
||||
iex> update_ammo_type(ammo_type, %{field: bad_value}, %User{id: 123})
|
||||
{:error, %Changeset{}}
|
||||
|
||||
"""
|
||||
@spec update_ammo_type(AmmoType.t(), attrs :: map()) ::
|
||||
@spec update_ammo_type(AmmoType.t(), attrs :: map(), User.t()) ::
|
||||
{:ok, AmmoType.t()} | {:error, Changeset.t(AmmoType.t())}
|
||||
def update_ammo_type(%AmmoType{} = ammo_type, attrs),
|
||||
do: ammo_type |> AmmoType.changeset(attrs) |> Repo.update()
|
||||
def update_ammo_type(%AmmoType{user_id: user_id} = ammo_type, attrs, %User{id: user_id}),
|
||||
do: ammo_type |> AmmoType.update_changeset(attrs) |> Repo.update()
|
||||
|
||||
@doc """
|
||||
Deletes a ammo_type.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> delete_ammo_type(ammo_type)
|
||||
iex> delete_ammo_type(ammo_type, %User{id: 123})
|
||||
{:ok, %AmmoType{}}
|
||||
|
||||
iex> delete_ammo_type(ammo_type)
|
||||
iex> delete_ammo_type(ammo_type, %User{id: 123})
|
||||
{:error, %Changeset{}}
|
||||
|
||||
"""
|
||||
@spec delete_ammo_type(AmmoType.t()) ::
|
||||
@spec delete_ammo_type(AmmoType.t(), User.t()) ::
|
||||
{:ok, AmmoType.t()} | {:error, Changeset.t(AmmoType.t())}
|
||||
def delete_ammo_type(%AmmoType{} = ammo_type), do: ammo_type |> Repo.delete()
|
||||
def delete_ammo_type(%AmmoType{user_id: user_id} = ammo_type, %User{id: user_id}),
|
||||
do: ammo_type |> Repo.delete()
|
||||
|
||||
@doc """
|
||||
Deletes a ammo_type.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> delete_ammo_type(ammo_type)
|
||||
iex> delete_ammo_type(ammo_type, %User{id: 123})
|
||||
%AmmoType{}
|
||||
|
||||
"""
|
||||
@spec delete_ammo_type!(AmmoType.t()) :: AmmoType.t()
|
||||
def delete_ammo_type!(%AmmoType{} = ammo_type), do: ammo_type |> Repo.delete!()
|
||||
@spec delete_ammo_type!(AmmoType.t(), User.t()) :: AmmoType.t()
|
||||
def delete_ammo_type!(%AmmoType{user_id: user_id} = ammo_type, %User{id: user_id}),
|
||||
do: ammo_type |> Repo.delete!()
|
||||
|
||||
@doc """
|
||||
Returns an `%Changeset{}` for tracking ammo_type changes.
|
||||
@ -137,7 +147,7 @@ defmodule Cannery.Ammo do
|
||||
@spec change_ammo_type(AmmoType.t() | AmmoType.new_ammo_type(), attrs :: map()) ::
|
||||
Changeset.t(AmmoType.t() | AmmoType.new_ammo_type())
|
||||
def change_ammo_type(%AmmoType{} = ammo_type, attrs \\ %{}),
|
||||
do: AmmoType.changeset(ammo_type, attrs)
|
||||
do: AmmoType.update_changeset(ammo_type, attrs)
|
||||
|
||||
@doc """
|
||||
Returns the list of ammo_groups for a user and type.
|
||||
@ -149,7 +159,7 @@ defmodule Cannery.Ammo do
|
||||
|
||||
"""
|
||||
@spec list_ammo_groups_for_type(AmmoType.t(), User.t()) :: [AmmoGroup.t()]
|
||||
def list_ammo_groups_for_type(%AmmoType{id: ammo_type_id}, %User{id: user_id}) do
|
||||
def list_ammo_groups_for_type(%AmmoType{id: ammo_type_id, user_id: user_id}, %User{id: user_id}) do
|
||||
Repo.all(
|
||||
from am in AmmoGroup,
|
||||
where: am.ammo_type_id == ^ammo_type_id,
|
||||
@ -178,77 +188,83 @@ defmodule Cannery.Ammo do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_ammo_group!(123)
|
||||
iex> get_ammo_group!(123, %User{id: 123})
|
||||
%AmmoGroup{}
|
||||
|
||||
iex> get_ammo_group!(456)
|
||||
iex> get_ammo_group!(456, %User{id: 123})
|
||||
** (Ecto.NoResultsError)
|
||||
|
||||
"""
|
||||
@spec get_ammo_group!(AmmoGroup.id()) :: AmmoGroup.t()
|
||||
def get_ammo_group!(id), do: Repo.get!(AmmoGroup, id)
|
||||
@spec get_ammo_group!(AmmoGroup.id(), User.t()) :: AmmoGroup.t()
|
||||
def get_ammo_group!(id, %User{id: user_id}),
|
||||
do: Repo.one!(from am in AmmoGroup, where: am.id == ^id and am.user_id == ^user_id)
|
||||
|
||||
@doc """
|
||||
Creates a ammo_group.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> create_ammo_group(%{field: value})
|
||||
iex> create_ammo_group(%{field: value}, %User{id: 123})
|
||||
{:ok, %AmmoGroup{}}
|
||||
|
||||
iex> create_ammo_group(%{field: bad_value})
|
||||
iex> create_ammo_group(%{field: bad_value}, %User{id: 123})
|
||||
{:error, %Changeset{}}
|
||||
|
||||
"""
|
||||
@spec create_ammo_group(attrs :: map()) ::
|
||||
@spec create_ammo_group(attrs :: map(), User.t()) ::
|
||||
{:ok, AmmoGroup.t()} | {:error, Changeset.t(AmmoGroup.new_ammo_group())}
|
||||
def create_ammo_group(attrs \\ %{}),
|
||||
do: %AmmoGroup{} |> AmmoGroup.changeset(attrs) |> Repo.insert()
|
||||
def create_ammo_group(attrs \\ %{}, %User{id: user_id}) do
|
||||
%AmmoGroup{}
|
||||
|> AmmoGroup.create_changeset(attrs |> Map.put("user_id", user_id))
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates a ammo_group.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> update_ammo_group(ammo_group, %{field: new_value})
|
||||
iex> update_ammo_group(ammo_group, %{field: new_value}, %User{id: 123})
|
||||
{:ok, %AmmoGroup{}}
|
||||
|
||||
iex> update_ammo_group(ammo_group, %{field: bad_value})
|
||||
iex> update_ammo_group(ammo_group, %{field: bad_value}, %User{id: 123})
|
||||
{:error, %Changeset{}}
|
||||
|
||||
"""
|
||||
@spec update_ammo_group(AmmoGroup.t(), attrs :: map()) ::
|
||||
@spec update_ammo_group(AmmoGroup.t(), attrs :: map(), User.t()) ::
|
||||
{:ok, AmmoGroup.t()} | {:error, Changeset.t(AmmoGroup.t())}
|
||||
def update_ammo_group(%AmmoGroup{} = ammo_group, attrs),
|
||||
do: ammo_group |> AmmoGroup.changeset(attrs) |> Repo.update()
|
||||
def update_ammo_group(%AmmoGroup{user_id: user_id} = ammo_group, attrs, %User{id: user_id}),
|
||||
do: ammo_group |> AmmoGroup.update_changeset(attrs) |> Repo.update()
|
||||
|
||||
@doc """
|
||||
Deletes a ammo_group.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> delete_ammo_group(ammo_group)
|
||||
iex> delete_ammo_group(ammo_group, %User{id: 123})
|
||||
{:ok, %AmmoGroup{}}
|
||||
|
||||
iex> delete_ammo_group(ammo_group)
|
||||
iex> delete_ammo_group(ammo_group, %User{id: 123})
|
||||
{:error, %Changeset{}}
|
||||
|
||||
"""
|
||||
@spec delete_ammo_group(AmmoGroup.t()) ::
|
||||
@spec delete_ammo_group(AmmoGroup.t(), User.t()) ::
|
||||
{:ok, AmmoGroup.t()} | {:error, Changeset.t(AmmoGroup.t())}
|
||||
def delete_ammo_group(%AmmoGroup{} = ammo_group), do: ammo_group |> Repo.delete()
|
||||
def delete_ammo_group(%AmmoGroup{user_id: user_id} = ammo_group, %User{id: user_id}),
|
||||
do: ammo_group |> Repo.delete()
|
||||
|
||||
@doc """
|
||||
Deletes a ammo_group.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> delete_ammo_group!(ammo_group)
|
||||
iex> delete_ammo_group!(ammo_group, %User{id: 123})
|
||||
%AmmoGroup{}
|
||||
|
||||
"""
|
||||
@spec delete_ammo_group!(AmmoGroup.t()) :: AmmoGroup.t()
|
||||
def delete_ammo_group!(%AmmoGroup{} = ammo_group), do: ammo_group |> Repo.delete!()
|
||||
@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.
|
||||
@ -262,5 +278,5 @@ defmodule Cannery.Ammo do
|
||||
@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.changeset(ammo_group, attrs)
|
||||
do: AmmoGroup.update_changeset(ammo_group, attrs)
|
||||
end
|
||||
|
@ -44,10 +44,11 @@ defmodule Cannery.Ammo.AmmoGroup do
|
||||
@type id :: UUID.t()
|
||||
|
||||
@doc false
|
||||
@spec changeset(t() | new_ammo_group(), attrs :: map()) :: Changeset.t(t() | new_ammo_group())
|
||||
def changeset(ammo_group, attrs) do
|
||||
@spec create_changeset(t() | new_ammo_group(), attrs :: map()) ::
|
||||
Changeset.t(t() | new_ammo_group())
|
||||
def create_changeset(ammo_group, attrs) do
|
||||
ammo_group
|
||||
|> cast(attrs, [:count, :price_paid, :notes, :tag_id, :ammo_type_id, :container_id, :user_id])
|
||||
|> cast(attrs, [:count, :price_paid, :notes, :ammo_type_id, :container_id, :user_id])
|
||||
|> validate_required([
|
||||
:count,
|
||||
:ammo_type_id,
|
||||
@ -55,4 +56,17 @@ defmodule Cannery.Ammo.AmmoGroup do
|
||||
:user_id
|
||||
])
|
||||
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, :ammo_type_id, :container_id, :user_id])
|
||||
|> validate_required([
|
||||
:count,
|
||||
:ammo_type_id,
|
||||
:container_id
|
||||
])
|
||||
end
|
||||
end
|
||||
|
@ -7,6 +7,7 @@ defmodule Cannery.Ammo.AmmoType do
|
||||
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
alias Cannery.Accounts.User
|
||||
alias Cannery.Ammo.{AmmoGroup, AmmoType}
|
||||
alias Ecto.{Changeset, UUID}
|
||||
|
||||
@ -34,6 +35,8 @@ defmodule Cannery.Ammo.AmmoType do
|
||||
field :manufacturer, :string
|
||||
field :sku, :string
|
||||
|
||||
belongs_to :user, User
|
||||
|
||||
has_many :ammo_groups, AmmoGroup
|
||||
|
||||
timestamps()
|
||||
@ -58,6 +61,8 @@ defmodule Cannery.Ammo.AmmoType do
|
||||
corrosive: boolean(),
|
||||
manufacturer: String.t() | nil,
|
||||
sku: String.t() | nil,
|
||||
user_id: User.id(),
|
||||
user: User.t() | nil,
|
||||
ammo_groups: [AmmoGroup.t()] | nil,
|
||||
inserted_at: NaiveDateTime.t(),
|
||||
updated_at: NaiveDateTime.t()
|
||||
@ -65,11 +70,9 @@ defmodule Cannery.Ammo.AmmoType do
|
||||
@type new_ammo_type :: %AmmoType{}
|
||||
@type id :: UUID.t()
|
||||
|
||||
@doc false
|
||||
@spec changeset(t() | new_ammo_type(), attrs :: map()) :: Changeset.t(t() | new_ammo_type())
|
||||
def changeset(ammo_type, attrs) do
|
||||
ammo_type
|
||||
|> cast(attrs, [
|
||||
@spec changeset_fields() :: [atom()]
|
||||
defp changeset_fields,
|
||||
do: [
|
||||
:name,
|
||||
:desc,
|
||||
:bullet_type,
|
||||
@ -87,7 +90,23 @@ defmodule Cannery.Ammo.AmmoType do
|
||||
:corrosive,
|
||||
:manufacturer,
|
||||
:sku
|
||||
])
|
||||
|> validate_required([:name])
|
||||
]
|
||||
|
||||
@doc false
|
||||
@spec create_changeset(t() | new_ammo_type(), attrs :: map()) ::
|
||||
Changeset.t(t() | new_ammo_type())
|
||||
def create_changeset(ammo_type, attrs) do
|
||||
ammo_type
|
||||
|> cast(attrs, [:user_id | changeset_fields()])
|
||||
|> validate_required([:name, :user_id])
|
||||
end
|
||||
|
||||
@doc false
|
||||
@spec update_changeset(t() | new_ammo_type(), attrs :: map()) ::
|
||||
Changeset.t(t() | new_ammo_type())
|
||||
def update_changeset(ammo_type, attrs) do
|
||||
ammo_type
|
||||
|> cast(attrs, changeset_fields())
|
||||
|> validate_required([:name, :user_id])
|
||||
end
|
||||
end
|
||||
|
@ -4,32 +4,38 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
|
||||
"""
|
||||
|
||||
use CanneryWeb, :live_component
|
||||
|
||||
alias Cannery.{Ammo, Containers}
|
||||
alias Cannery.{Ammo.AmmoType, Containers.Container}
|
||||
alias Ecto.Changeset
|
||||
|
||||
@impl true
|
||||
def update(%{ammo_group: ammo_group} = assigns, socket) do
|
||||
socket = socket |> assign(assigns)
|
||||
def update(%{ammo_group: _ammo_group} = assigns, socket) do
|
||||
socket |> assign(assigns) |> update()
|
||||
end
|
||||
|
||||
def update(%{assigns: %{ammo_group: ammo_group, current_user: current_user}} = socket) do
|
||||
changeset = Ammo.change_ammo_group(ammo_group)
|
||||
containers = Containers.list_containers(socket.assigns.current_user)
|
||||
ammo_types = Ammo.list_ammo_types()
|
||||
|
||||
containers = Containers.list_containers(current_user)
|
||||
ammo_types = Ammo.list_ammo_types(current_user)
|
||||
{:ok, socket |> assign(changeset: changeset, containers: containers, ammo_types: ammo_types)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("validate", %{"ammo_group" => ammo_group_params}, socket) do
|
||||
ammo_group_params = ammo_group_params |> Map.put("user_id", socket.assigns.current_user.id)
|
||||
changeset = socket.assigns.ammo_group |> Ammo.change_ammo_group(ammo_group_params)
|
||||
{:noreply, socket |> assign(:changeset, changeset)}
|
||||
def handle_event(
|
||||
"validate",
|
||||
%{"ammo_group" => ammo_group_params},
|
||||
%{assigns: %{ammo_group: ammo_group}} = socket
|
||||
) do
|
||||
socket = socket |> assign(:changeset, ammo_group |> Ammo.change_ammo_group(ammo_group_params))
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
def handle_event("save", %{"ammo_group" => ammo_group_params}, socket) do
|
||||
ammo_group_params = ammo_group_params |> Map.put("user_id", socket.assigns.current_user.id)
|
||||
save_ammo_group(socket, socket.assigns.action, ammo_group_params)
|
||||
def handle_event(
|
||||
"save",
|
||||
%{"ammo_group" => ammo_group_params},
|
||||
%{assigns: %{action: action}} = socket
|
||||
) do
|
||||
save_ammo_group(socket, action, ammo_group_params)
|
||||
end
|
||||
|
||||
@impl true
|
||||
@ -99,41 +105,50 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
|
||||
# HTML Helpers
|
||||
@spec container_options([Container.t()]) :: [{String.t(), Container.id()}]
|
||||
defp container_options(containers) do
|
||||
containers |> Enum.map(fn container -> {container.name, container.id} end)
|
||||
containers |> Enum.map(fn %{id: id, name: name} -> {name, id} end)
|
||||
end
|
||||
|
||||
@spec ammo_type_options([AmmoType.t()]) :: [{String.t(), AmmoType.id()}]
|
||||
defp ammo_type_options(ammo_types) do
|
||||
ammo_types |> Enum.map(fn ammo_type -> {ammo_type.name, ammo_type.id} end)
|
||||
ammo_types |> Enum.map(fn %{id: id, name: name} -> {name, id} end)
|
||||
end
|
||||
|
||||
# Save Helpers
|
||||
|
||||
defp save_ammo_group(socket, :edit, ammo_group_params) do
|
||||
socket.assigns.ammo_group
|
||||
|> Ammo.update_ammo_group(ammo_group_params)
|
||||
|> case do
|
||||
{:ok, _ammo_group} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, dgettext("prompts", "Ammo group updated successfully"))
|
||||
|> push_redirect(to: socket.assigns.return_to)}
|
||||
defp save_ammo_group(
|
||||
%{assigns: %{ammo_group: ammo_group, current_user: current_user, return_to: return_to}} =
|
||||
socket,
|
||||
:edit,
|
||||
ammo_group_params
|
||||
) do
|
||||
socket =
|
||||
case Ammo.update_ammo_group(ammo_group, ammo_group_params, current_user) do
|
||||
{:ok, _ammo_group} ->
|
||||
prompt = dgettext("prompts", "Ammo group updated successfully")
|
||||
socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
|
||||
|
||||
{:error, %Changeset{} = changeset} ->
|
||||
{:noreply, socket |> assign(:changeset, changeset)}
|
||||
end
|
||||
{:error, %Changeset{} = changeset} ->
|
||||
socket |> assign(:changeset, changeset)
|
||||
end
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
defp save_ammo_group(socket, :new, ammo_group_params) do
|
||||
case Ammo.create_ammo_group(ammo_group_params) do
|
||||
{:ok, _ammo_group} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, dgettext("prompts", "Ammo group created successfully"))
|
||||
|> push_redirect(to: socket.assigns.return_to)}
|
||||
defp save_ammo_group(
|
||||
%{assigns: %{current_user: current_user, return_to: return_to}} = socket,
|
||||
:new,
|
||||
ammo_group_params
|
||||
) do
|
||||
socket =
|
||||
case Ammo.create_ammo_group(ammo_group_params, current_user) do
|
||||
{:ok, _ammo_group} ->
|
||||
prompt = dgettext("prompts", "Ammo group created successfully")
|
||||
socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
|
||||
|
||||
{:error, %Changeset{} = changeset} ->
|
||||
{:noreply, socket |> assign(changeset: changeset)}
|
||||
end
|
||||
{:error, %Changeset{} = changeset} ->
|
||||
socket |> assign(changeset: changeset)
|
||||
end
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
end
|
||||
|
@ -4,7 +4,6 @@ defmodule CanneryWeb.AmmoGroupLive.Index do
|
||||
"""
|
||||
|
||||
use CanneryWeb, :live_view
|
||||
|
||||
alias Cannery.Ammo
|
||||
alias Cannery.Ammo.AmmoGroup
|
||||
|
||||
@ -14,14 +13,14 @@ defmodule CanneryWeb.AmmoGroupLive.Index do
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_params(params, _url, socket) do
|
||||
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
|
||||
def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
|
||||
{:noreply, apply_action(socket, live_action, params)}
|
||||
end
|
||||
|
||||
defp apply_action(socket, :edit, %{"id" => id}) do
|
||||
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
|
||||
socket
|
||||
|> assign(:page_title, gettext("Edit Ammo group"))
|
||||
|> assign(:ammo_group, Ammo.get_ammo_group!(id))
|
||||
|> assign(:ammo_group, Ammo.get_ammo_group!(id, current_user))
|
||||
end
|
||||
|
||||
defp apply_action(socket, :new, _params) do
|
||||
@ -31,19 +30,19 @@ defmodule CanneryWeb.AmmoGroupLive.Index do
|
||||
end
|
||||
|
||||
defp apply_action(socket, :index, _params) do
|
||||
socket
|
||||
|> assign(:page_title, gettext("Listing Ammo groups"))
|
||||
|> assign(:ammo_group, nil)
|
||||
socket |> assign(:page_title, gettext("Listing Ammo groups")) |> assign(:ammo_group, nil)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", %{"id" => id}, socket) do
|
||||
Ammo.get_ammo_group!(id) |> Ammo.delete_ammo_group!()
|
||||
{:noreply, socket |> display_ammo_groups()}
|
||||
def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
|
||||
Ammo.get_ammo_group!(id, current_user) |> Ammo.delete_ammo_group!(current_user)
|
||||
|
||||
prompt = dgettext("prompts", "Ammo group deleted succesfully")
|
||||
|
||||
{:noreply, socket |> put_flash(:info, prompt) |> display_ammo_groups()}
|
||||
end
|
||||
|
||||
defp display_ammo_groups(%{assigns: %{current_user: current_user}} = socket) do
|
||||
ammo_groups = Ammo.list_ammo_groups(current_user)
|
||||
socket |> assign(:ammo_groups, ammo_groups)
|
||||
socket |> assign(:ammo_groups, Ammo.list_ammo_groups(current_user))
|
||||
end
|
||||
end
|
||||
|
@ -9,27 +9,31 @@ defmodule CanneryWeb.AmmoGroupLive.Show do
|
||||
|
||||
@impl true
|
||||
def mount(_params, session, socket) do
|
||||
socket = socket |> assign_defaults(session)
|
||||
|
||||
{:ok, socket}
|
||||
{:ok, socket |> assign_defaults(session)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_params(%{"id" => id}, _, socket) do
|
||||
socket =
|
||||
socket
|
||||
|> assign(
|
||||
page_title: page_title(socket.assigns.live_action),
|
||||
ammo_group: Ammo.get_ammo_group!(id) |> Repo.preload([:container, :ammo_type])
|
||||
)
|
||||
|
||||
{:noreply, socket}
|
||||
def handle_params(
|
||||
%{"id" => id},
|
||||
_,
|
||||
%{assigns: %{live_action: live_action, current_user: current_user}} = socket
|
||||
) do
|
||||
ammo_group = Ammo.get_ammo_group!(id, current_user) |> Repo.preload([:container, :ammo_type])
|
||||
{:noreply, socket |> assign(page_title: page_title(live_action), ammo_group: ammo_group)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", _, socket) do
|
||||
socket.assigns.ammo_group |> Ammo.delete_ammo_group!()
|
||||
{:noreply, socket |> push_redirect(to: Routes.ammo_group_index_path(socket, :index))}
|
||||
def handle_event(
|
||||
"delete",
|
||||
_,
|
||||
%{assigns: %{ammo_group: ammo_group, current_user: current_user}} = socket
|
||||
) do
|
||||
ammo_group |> Ammo.delete_ammo_group!(current_user)
|
||||
|
||||
prompt = dgettext("prompts", "Ammo group deleted succesfully")
|
||||
redirect_to = Routes.ammo_group_index_path(socket, :index)
|
||||
|
||||
{:noreply, socket |> put_flash(:info, prompt) |> push_redirect(to: redirect_to)}
|
||||
end
|
||||
|
||||
defp page_title(:show), do: gettext("Show Ammo group")
|
||||
|
@ -4,28 +4,29 @@ defmodule CanneryWeb.AmmoTypeLive.FormComponent do
|
||||
"""
|
||||
|
||||
use CanneryWeb, :live_component
|
||||
|
||||
alias Cannery.Ammo
|
||||
alias Ecto.Changeset
|
||||
|
||||
@impl true
|
||||
def update(%{ammo_type: ammo_type} = assigns, socket) do
|
||||
changeset = Ammo.change_ammo_type(ammo_type)
|
||||
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(assigns)
|
||||
|> assign(:changeset, changeset)}
|
||||
{:ok, socket |> assign(assigns) |> assign(:changeset, Ammo.change_ammo_type(ammo_type))}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("validate", %{"ammo_type" => ammo_type_params}, socket) do
|
||||
changeset = socket.assigns.ammo_type |> Ammo.change_ammo_type(ammo_type_params)
|
||||
{:noreply, socket |> assign(:changeset, changeset)}
|
||||
def handle_event(
|
||||
"validate",
|
||||
%{"ammo_type" => ammo_type_params},
|
||||
%{assigns: %{ammo_type: ammo_type}} = socket
|
||||
) do
|
||||
{:noreply, socket |> assign(:changeset, ammo_type |> Ammo.change_ammo_type(ammo_type_params))}
|
||||
end
|
||||
|
||||
def handle_event("save", %{"ammo_type" => ammo_type_params}, socket) do
|
||||
save_ammo_type(socket, socket.assigns.action, ammo_type_params)
|
||||
def handle_event(
|
||||
"save",
|
||||
%{"ammo_type" => ammo_type_params},
|
||||
%{assigns: %{action: action}} = socket
|
||||
) do
|
||||
save_ammo_type(socket, action, ammo_type_params)
|
||||
end
|
||||
|
||||
@impl true
|
||||
@ -161,29 +162,36 @@ defmodule CanneryWeb.AmmoTypeLive.FormComponent do
|
||||
"""
|
||||
end
|
||||
|
||||
defp save_ammo_type(socket, :edit, ammo_type_params) do
|
||||
case Ammo.update_ammo_type(socket.assigns.ammo_type, ammo_type_params) do
|
||||
{:ok, _ammo_type} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, dgettext("prompts", "Ammo type updated successfully"))
|
||||
|> push_redirect(to: socket.assigns.return_to)}
|
||||
defp save_ammo_type(
|
||||
%{assigns: %{ammo_type: ammo_type, current_user: current_user, return_to: return_to}} =
|
||||
socket,
|
||||
:edit,
|
||||
ammo_type_params
|
||||
) do
|
||||
socket =
|
||||
case Ammo.update_ammo_type(ammo_type, ammo_type_params, current_user) do
|
||||
{:ok, %{name: ammo_type_name}} ->
|
||||
prompt = dgettext("prompts", "%{name} updated successfully", name: ammo_type_name)
|
||||
socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
|
||||
|
||||
{:error, %Changeset{} = changeset} ->
|
||||
{:noreply, socket |> assign(:changeset, changeset)}
|
||||
end
|
||||
{:error, %Changeset{} = changeset} ->
|
||||
socket |> assign(:changeset, changeset)
|
||||
end
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
defp save_ammo_type(socket, :new, ammo_type_params) do
|
||||
case Ammo.create_ammo_type(ammo_type_params) do
|
||||
{:ok, _ammo_type} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, dgettext("prompts", "Ammo type created successfully"))
|
||||
|> push_redirect(to: socket.assigns.return_to)}
|
||||
defp save_ammo_type(%{assigns: %{return_to: return_to}} = socket, :new, ammo_type_params) do
|
||||
socket =
|
||||
case Ammo.create_ammo_type(ammo_type_params) do
|
||||
{:ok, %{name: ammo_type_name}} ->
|
||||
prompt = dgettext("prompts", "%{name} created successfully", name: ammo_type_name)
|
||||
socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
|
||||
|
||||
{:error, %Changeset{} = changeset} ->
|
||||
{:noreply, socket |> assign(changeset: changeset)}
|
||||
end
|
||||
{:error, %Changeset{} = changeset} ->
|
||||
socket |> assign(changeset: changeset)
|
||||
end
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
end
|
||||
|
@ -10,18 +10,18 @@ defmodule CanneryWeb.AmmoTypeLive.Index do
|
||||
|
||||
@impl true
|
||||
def mount(_params, session, socket) do
|
||||
{:ok, socket |> assign_defaults(session) |> assign(:ammo_types, list_ammo_types())}
|
||||
{:ok, socket |> assign_defaults(session) |> list_ammo_types()}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_params(params, _url, socket) do
|
||||
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
|
||||
def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
|
||||
{:noreply, apply_action(socket, live_action, params)}
|
||||
end
|
||||
|
||||
defp apply_action(socket, :edit, %{"id" => id}) do
|
||||
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
|
||||
socket
|
||||
|> assign(:page_title, gettext("Edit Ammo type"))
|
||||
|> assign(:ammo_type, Ammo.get_ammo_type!(id))
|
||||
|> assign(:ammo_type, Ammo.get_ammo_type!(id, current_user))
|
||||
end
|
||||
|
||||
defp apply_action(socket, :new, _params) do
|
||||
@ -31,20 +31,19 @@ defmodule CanneryWeb.AmmoTypeLive.Index do
|
||||
end
|
||||
|
||||
defp apply_action(socket, :index, _params) do
|
||||
socket
|
||||
|> assign(:page_title, gettext("Listing Ammo types"))
|
||||
|> assign(:ammo_type, nil)
|
||||
socket |> assign(:page_title, gettext("Listing Ammo types")) |> assign(:ammo_type, nil)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", %{"id" => id}, socket) do
|
||||
ammo_type = Ammo.get_ammo_type!(id)
|
||||
{:ok, _} = Ammo.delete_ammo_type(ammo_type)
|
||||
def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
|
||||
%{name: name} = Ammo.get_ammo_type!(id, current_user) |> Ammo.delete_ammo_type!(current_user)
|
||||
|
||||
{:noreply, socket |> assign(:ammo_types, list_ammo_types())}
|
||||
prompt = dgettext("prompts", "%{name} deleted succesfully", name: name)
|
||||
|
||||
{:noreply, socket |> put_flash(:info, prompt) |> list_ammo_types()}
|
||||
end
|
||||
|
||||
defp list_ammo_types do
|
||||
Ammo.list_ammo_types()
|
||||
defp list_ammo_types(%{assigns: %{current_user: current_user}} = socket) do
|
||||
socket |> assign(:ammo_types, Ammo.list_ammo_types(current_user))
|
||||
end
|
||||
end
|
||||
|
@ -14,25 +14,32 @@ defmodule CanneryWeb.AmmoTypeLive.Show do
|
||||
|
||||
@impl true
|
||||
def handle_params(%{"id" => id}, _, %{assigns: %{current_user: current_user}} = socket) do
|
||||
ammo_type = Ammo.get_ammo_type!(id)
|
||||
ammo_groups = ammo_type |> Ammo.list_ammo_groups_for_type(current_user)
|
||||
ammo_type = Ammo.get_ammo_type!(id, current_user)
|
||||
|
||||
socket =
|
||||
socket
|
||||
|> assign(
|
||||
page_title: page_title(socket.assigns.live_action),
|
||||
ammo_type: ammo_type,
|
||||
ammo_groups: ammo_groups,
|
||||
avg_cost_per_round: ammo_type |> Ammo.get_average_cost_for_ammo_type!()
|
||||
ammo_groups: ammo_type |> Ammo.list_ammo_groups_for_type(current_user),
|
||||
avg_cost_per_round: ammo_type |> Ammo.get_average_cost_for_ammo_type!(current_user)
|
||||
)
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", _, socket) do
|
||||
socket.assigns.ammo_type |> Ammo.delete_ammo_type!()
|
||||
{:noreply, socket |> push_redirect(to: Routes.ammo_type_index_path(socket, :index))}
|
||||
def handle_event(
|
||||
"delete",
|
||||
_,
|
||||
%{assigns: %{ammo_type: ammo_type, current_user: current_user}} = socket
|
||||
) do
|
||||
%{name: ammo_type_name} = ammo_type |> Ammo.delete_ammo_type!(current_user)
|
||||
|
||||
prompt = dgettext("prompts", "%{name} deleted succesfully", name: ammo_type_name)
|
||||
redirect_to = Routes.ammo_type_index_path(socket, :index)
|
||||
|
||||
{:noreply, socket |> put_flash(:info, prompt) |> push_redirect(to: redirect_to)}
|
||||
end
|
||||
|
||||
defp page_title(:show), do: gettext("Show Ammo type")
|
||||
|
Reference in New Issue
Block a user