- harden ammo context

- add user_id to ammo types
This commit is contained in:
shibao 2022-02-10 00:57:56 -05:00
parent 3b0850852e
commit e9ef44eb53
13 changed files with 325 additions and 242 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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")

View File

@ -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

View File

@ -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

View File

@ -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")

View File

@ -76,7 +76,7 @@ msgid "Send instructions to reset password"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_group_live/index.ex:29
#: lib/cannery_web/live/ammo_group_live/index.ex:28
msgid "Add Ammo"
msgstr ""
@ -91,11 +91,11 @@ msgid "New Ammo group"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_group_live/form_component.ex:90
#: lib/cannery_web/live/ammo_type_live/form_component.ex:155
#: lib/cannery_web/live/ammo_group_live/form_component.ex:96
#: lib/cannery_web/live/ammo_type_live/form_component.ex:156
#: lib/cannery_web/live/container_live/form_component.ex:79
#: lib/cannery_web/live/invite_live/form_component.ex:61
#: lib/cannery_web/live/tag_live/form_component.ex:69
#: lib/cannery_web/live/tag_live/form_component.ex:62
msgid "Save"
msgstr ""

View File

@ -111,7 +111,7 @@ msgid "Settings"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_group_live/form_component.ex:63
#: lib/cannery_web/live/ammo_group_live/form_component.ex:69
#: lib/cannery_web/live/ammo_group_live/index.html.heex:26
msgid "Count"
msgstr ""
@ -123,8 +123,8 @@ msgid "Count:"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_group_live/index.ex:23
#: lib/cannery_web/live/ammo_group_live/show.ex:36
#: lib/cannery_web/live/ammo_group_live/index.ex:22
#: lib/cannery_web/live/ammo_group_live/show.ex:40
msgid "Edit Ammo group"
msgstr ""
@ -134,7 +134,7 @@ msgid "Listing Ammo"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_group_live/index.ex:35
#: lib/cannery_web/live/ammo_group_live/index.ex:33
msgid "Listing Ammo groups"
msgstr ""
@ -144,7 +144,7 @@ msgid "No Ammo"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_group_live/form_component.ex:77
#: lib/cannery_web/live/ammo_group_live/form_component.ex:83
#: lib/cannery_web/live/ammo_group_live/index.html.heex:32
msgid "Notes"
msgstr ""
@ -156,7 +156,7 @@ msgid "Notes:"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_group_live/form_component.ex:70
#: lib/cannery_web/live/ammo_group_live/form_component.ex:76
#: lib/cannery_web/live/ammo_group_live/index.html.heex:29
msgid "Price paid"
msgstr ""
@ -168,7 +168,7 @@ msgid "Price paid:"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_group_live/show.ex:35
#: lib/cannery_web/live/ammo_group_live/show.ex:39
msgid "Show Ammo group"
msgstr ""
@ -183,7 +183,7 @@ msgid "This ammo group is not in a container"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_group_live/form_component.ex:57
#: lib/cannery_web/live/ammo_group_live/form_component.ex:63
msgid "Ammo type"
msgstr ""
@ -193,93 +193,93 @@ msgid "Average Price paid"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_type_live/form_component.ex:139
#: lib/cannery_web/live/ammo_type_live/form_component.ex:140
#: lib/cannery_web/live/ammo_type_live/index.html.heex:38
msgid "Blank"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_type_live/form_component.ex:101
#: lib/cannery_web/live/ammo_type_live/form_component.ex:102
msgid "Brass"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_type_live/form_component.ex:77
#: lib/cannery_web/live/ammo_type_live/form_component.ex:78
#: lib/cannery_web/live/ammo_type_live/index.html.heex:28
#: lib/cannery_web/live/ammo_type_live/show.html.heex:37
msgid "Bullet core"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_type_live/form_component.ex:70
#: lib/cannery_web/live/ammo_type_live/form_component.ex:71
#: lib/cannery_web/live/ammo_type_live/index.html.heex:27
#: lib/cannery_web/live/ammo_type_live/show.html.heex:36
msgid "Bullet type"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_type_live/form_component.ex:91
#: lib/cannery_web/live/ammo_type_live/form_component.ex:92
#: lib/cannery_web/live/ammo_type_live/index.html.heex:30
#: lib/cannery_web/live/ammo_type_live/show.html.heex:39
msgid "Caliber"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_type_live/form_component.ex:84
#: lib/cannery_web/live/ammo_type_live/form_component.ex:85
#: lib/cannery_web/live/ammo_type_live/index.html.heex:29
#: lib/cannery_web/live/ammo_type_live/show.html.heex:38
msgid "Cartridge"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_type_live/form_component.ex:98
#: lib/cannery_web/live/ammo_type_live/form_component.ex:99
#: lib/cannery_web/live/ammo_type_live/index.html.heex:31
#: lib/cannery_web/live/ammo_type_live/show.html.heex:40
msgid "Case material"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_group_live/form_component.ex:84
#: lib/cannery_web/live/ammo_group_live/form_component.ex:90
msgid "Container"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_type_live/form_component.ex:143
#: lib/cannery_web/live/ammo_type_live/form_component.ex:144
#: lib/cannery_web/live/ammo_type_live/index.html.heex:39
msgid "Corrosive"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_type_live/form_component.ex:57
#: lib/cannery_web/live/ammo_type_live/form_component.ex:58
#: lib/cannery_web/live/container_live/form_component.ex:56
msgid "Description"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_type_live/index.ex:23
#: lib/cannery_web/live/ammo_type_live/show.ex:39
#: lib/cannery_web/live/ammo_type_live/show.ex:46
msgid "Edit Ammo type"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_type_live/form_component.ex:68
#: lib/cannery_web/live/ammo_type_live/form_component.ex:69
msgid "Example bullet type abbreviations"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_type_live/form_component.ex:73
#: lib/cannery_web/live/ammo_type_live/form_component.ex:74
msgid "FMJ"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_type_live/form_component.ex:105
#: lib/cannery_web/live/ammo_type_live/form_component.ex:106
#: lib/cannery_web/live/ammo_type_live/index.html.heex:32
#: lib/cannery_web/live/ammo_type_live/show.html.heex:41
msgid "Grains"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_type_live/form_component.ex:135
#: lib/cannery_web/live/ammo_type_live/form_component.ex:136
#: lib/cannery_web/live/ammo_type_live/index.html.heex:37
msgid "Incendiary"
msgstr ""
@ -290,22 +290,22 @@ msgid "Listing Ammo Types"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_type_live/index.ex:35
#: lib/cannery_web/live/ammo_type_live/index.ex:34
msgid "Listing Ammo types"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_type_live/form_component.ex:147
#: lib/cannery_web/live/ammo_type_live/form_component.ex:148
#: lib/cannery_web/live/ammo_type_live/index.html.heex:40
msgid "Manufacturer"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_type_live/form_component.ex:53
#: lib/cannery_web/live/ammo_type_live/form_component.ex:54
#: lib/cannery_web/live/ammo_type_live/index.html.heex:26
#: lib/cannery_web/live/container_live/form_component.ex:49
#: lib/cannery_web/live/invite_live/form_component.ex:53
#: lib/cannery_web/live/tag_live/form_component.ex:53
#: lib/cannery_web/live/tag_live/form_component.ex:46
msgid "Name"
msgstr ""
@ -325,32 +325,32 @@ msgid "No ammo for this type"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_type_live/form_component.ex:113
#: lib/cannery_web/live/ammo_type_live/form_component.ex:114
#: lib/cannery_web/live/ammo_type_live/index.html.heex:33
#: lib/cannery_web/live/ammo_type_live/show.html.heex:42
msgid "Pressure"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_type_live/form_component.ex:120
#: lib/cannery_web/live/ammo_type_live/form_component.ex:121
#: lib/cannery_web/live/ammo_type_live/index.html.heex:34
#: lib/cannery_web/live/ammo_type_live/show.html.heex:43
msgid "Primer type"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_type_live/form_component.ex:127
#: lib/cannery_web/live/ammo_type_live/form_component.ex:128
#: lib/cannery_web/live/ammo_type_live/index.html.heex:35
msgid "Rimfire"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_type_live/form_component.ex:151
#: lib/cannery_web/live/ammo_type_live/form_component.ex:152
msgid "SKU"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_type_live/show.ex:38
#: lib/cannery_web/live/ammo_type_live/show.ex:45
msgid "Show Ammo type"
msgstr ""
@ -360,12 +360,12 @@ msgid "Sku"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_type_live/form_component.ex:80
#: lib/cannery_web/live/ammo_type_live/form_component.ex:81
msgid "Steel"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_type_live/form_component.ex:131
#: lib/cannery_web/live/ammo_type_live/form_component.ex:132
#: lib/cannery_web/live/ammo_type_live/index.html.heex:36
msgid "Tracer"
msgstr ""
@ -451,7 +451,7 @@ msgid "Type:"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/tag_live/form_component.ex:57
#: lib/cannery_web/live/tag_live/form_component.ex:50
msgid "Background color"
msgstr ""
@ -487,7 +487,7 @@ msgid "Listing Invites"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/tag_live/index.ex:35
#: lib/cannery_web/live/tag_live/index.ex:34
#: lib/cannery_web/live/tag_live/index.html.heex:3
msgid "Listing Tags"
msgstr ""
@ -523,7 +523,7 @@ msgid "Tags can be added to your containers to help you organize"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/tag_live/form_component.ex:63
#: lib/cannery_web/live/tag_live/form_component.ex:56
msgid "Text color"
msgstr ""

View File

@ -77,12 +77,12 @@ msgid "Your account has been deleted"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_group_live/form_component.ex:132
#: lib/cannery_web/live/ammo_group_live/form_component.ex:145
msgid "Ammo group created successfully"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_group_live/form_component.ex:119
#: lib/cannery_web/live/ammo_group_live/form_component.ex:127
msgid "Ammo group updated successfully"
msgstr ""
@ -94,24 +94,14 @@ msgid "Are you sure you want to delete this ammo?"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_group_live/form_component.ex:91
#: lib/cannery_web/live/ammo_type_live/form_component.ex:156
#: lib/cannery_web/live/ammo_group_live/form_component.ex:97
#: lib/cannery_web/live/ammo_type_live/form_component.ex:157
#: lib/cannery_web/live/container_live/form_component.ex:81
#: lib/cannery_web/live/invite_live/form_component.ex:63
#: lib/cannery_web/live/tag_live/form_component.ex:71
#: lib/cannery_web/live/tag_live/form_component.ex:64
msgid "Saving..."
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_type_live/form_component.ex:182
msgid "Ammo type created successfully"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_type_live/form_component.ex:169
msgid "Ammo type updated successfully"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_type_live/show.html.heex:26
#: lib/cannery_web/live/container_live/index.html.heex:36
@ -152,16 +142,26 @@ msgid "Invite updated successfully"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/tag_live/form_component.ex:98
msgid "Tag created successfully"
#: lib/cannery_web/live/ammo_type_live/form_component.ex:188
#: lib/cannery_web/live/tag_live/form_component.ex:97
msgid "%{name} created successfully"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/tag_live/index.ex:45
msgid "Tag deleted succesfully"
#: lib/cannery_web/live/ammo_type_live/index.ex:41
#: lib/cannery_web/live/ammo_type_live/show.ex:39
#: lib/cannery_web/live/tag_live/index.ex:41
msgid "%{name} deleted succesfully"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/tag_live/form_component.ex:83
msgid "Tag updated successfully"
#: lib/cannery_web/live/ammo_type_live/form_component.ex:174
#: lib/cannery_web/live/tag_live/form_component.ex:79
msgid "%{name} updated successfully"
msgstr ""
#, elixir-format, ex-autogen
#: lib/cannery_web/live/ammo_group_live/index.ex:40
#: lib/cannery_web/live/ammo_group_live/show.ex:33
msgid "Ammo group deleted succesfully"
msgstr ""

View File

@ -25,6 +25,8 @@ defmodule Cannery.Repo.Migrations.CreateAmmoTypes do
add :manufacturer, :string
add :sku, :string
add :user_id, references(:users, on_delete: :delete_all, type: :binary_id)
timestamps()
end
end