improve containers
This commit is contained in:
parent
845ebc3bc6
commit
76d594ca0f
@ -14,13 +14,13 @@ defmodule Cannery.Containers do
|
|||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
iex> list_containers()
|
iex> list_containers(%User{id: 123})
|
||||||
[%Container{}, ...]
|
[%Container{}, ...]
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@spec list_containers(user_or_user_id :: User.t() | User.id()) :: [Container.t()]
|
@spec list_containers(User.t()) :: [Container.t()]
|
||||||
def list_containers(%{id: user_id}), do: list_containers(user_id)
|
def list_containers(%User{id: user_id}),
|
||||||
def list_containers(user_id), do: Repo.all(from c in Container, where: c.user_id == ^user_id)
|
do: Repo.all(from c in Container, where: c.user_id == ^user_id)
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Gets a single container.
|
Gets a single container.
|
||||||
@ -29,25 +29,26 @@ defmodule Cannery.Containers do
|
|||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
iex> get_container!(123)
|
iex> get_container!(123, %User{id: 123})
|
||||||
%Container{}
|
%Container{}
|
||||||
|
|
||||||
iex> get_container!(456)
|
iex> get_container!(456, %User{id: 123})
|
||||||
** (Ecto.NoResultsError)
|
** (Ecto.NoResultsError)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@spec get_container!(Container.id()) :: Container.t()
|
@spec get_container!(Container.id(), User.t()) :: Container.t()
|
||||||
def get_container!(id), do: Repo.get!(Container, id)
|
def get_container!(id, %User{id: user_id}),
|
||||||
|
do: Repo.one!(from c in Container, where: c.id == ^id and c.user_id == ^user_id)
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Creates a container.
|
Creates a container.
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
iex> create_container(%{field: value}, user)
|
iex> create_container(%{field: value}, %User{id: 123})
|
||||||
{:ok, %Container{}}
|
{:ok, %Container{}}
|
||||||
|
|
||||||
iex> create_container(%{field: bad_value}, user)
|
iex> create_container(%{field: bad_value}, %User{id: 123})
|
||||||
{:error, %Changeset{}}
|
{:error, %Changeset{}}
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -63,10 +64,10 @@ defmodule Cannery.Containers do
|
|||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
iex> update_container(container, user, %{field: new_value})
|
iex> update_container(container, %User{id: 123}, %{field: new_value})
|
||||||
{:ok, %Container{}}
|
{:ok, %Container{}}
|
||||||
|
|
||||||
iex> update_container(container, user, %{field: bad_value})
|
iex> update_container(container, %User{id: 123}, %{field: bad_value})
|
||||||
{:error, %Changeset{}}
|
{:error, %Changeset{}}
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -81,10 +82,10 @@ defmodule Cannery.Containers do
|
|||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
iex> delete_container(container, user)
|
iex> delete_container(container, %User{id: 123})
|
||||||
{:ok, %Container{}}
|
{:ok, %Container{}}
|
||||||
|
|
||||||
iex> delete_container(container, user)
|
iex> delete_container(container, %User{id: 123})
|
||||||
{:error, %Changeset{}}
|
{:error, %Changeset{}}
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -100,23 +101,12 @@ defmodule Cannery.Containers do
|
|||||||
0 ->
|
0 ->
|
||||||
container |> Repo.delete()
|
container |> Repo.delete()
|
||||||
|
|
||||||
amount ->
|
_amount ->
|
||||||
error_string =
|
error = dgettext("errors", "Container must be empty before deleting")
|
||||||
dngettext(
|
|
||||||
"errors",
|
|
||||||
"There is still %{amount} ammo group in this container",
|
|
||||||
"There are still %{amount} ammo groups in this container",
|
|
||||||
amount
|
|
||||||
)
|
|
||||||
|
|
||||||
container
|
container
|
||||||
|> change_container()
|
|> change_container()
|
||||||
|> Changeset.add_error(
|
|> Changeset.add_error(:ammo_groups, error)
|
||||||
:ammo_groups,
|
|
||||||
error_string,
|
|
||||||
amount: amount,
|
|
||||||
count: amount
|
|
||||||
)
|
|
||||||
|> Changeset.apply_action(:delete)
|
|> Changeset.apply_action(:delete)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -126,7 +116,7 @@ defmodule Cannery.Containers do
|
|||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
iex> delete_container(container, user)
|
iex> delete_container(container, %User{id: 123})
|
||||||
%Container{}
|
%Container{}
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -160,18 +150,16 @@ defmodule Cannery.Containers do
|
|||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
iex> add_tag!(container, tag)
|
iex> add_tag!(container, tag, %User{id: 123})
|
||||||
%Container{}
|
%Container{}
|
||||||
|
|
||||||
iex> add_tag!(container_id, tag_id)
|
|
||||||
%Container{}
|
|
||||||
"""
|
"""
|
||||||
@spec add_tag!(Container.t(), Tag.t()) :: Container.t()
|
@spec add_tag!(Container.t(), Tag.t(), User.t()) :: ContainerTag.t()
|
||||||
def add_tag!(%{id: container_id}, %{id: tag_id}), do: add_tag!(container_id, tag_id)
|
def add_tag!(
|
||||||
|
%Container{id: container_id, user_id: user_id},
|
||||||
@spec add_tag!(Container.id(), Tag.id()) :: Container.t()
|
%Tag{id: tag_id, user_id: user_id},
|
||||||
def add_tag!(container_id, tag_id)
|
%User{id: user_id}
|
||||||
when not (container_id |> is_nil()) and not (tag_id |> is_nil()) do
|
) do
|
||||||
%ContainerTag{}
|
%ContainerTag{}
|
||||||
|> ContainerTag.changeset(%{"container_id" => container_id, "tag_id" => tag_id})
|
|> ContainerTag.changeset(%{"container_id" => container_id, "tag_id" => tag_id})
|
||||||
|> Repo.insert!()
|
|> Repo.insert!()
|
||||||
@ -182,22 +170,24 @@ defmodule Cannery.Containers do
|
|||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
iex> remove_tag!(container, tag)
|
iex> remove_tag!(container, tag, %User{id: 123})
|
||||||
%Container{}
|
%Container{}
|
||||||
|
|
||||||
iex> remove_tag!(container_id, tag_id)
|
|
||||||
%Container{}
|
|
||||||
"""
|
"""
|
||||||
@spec remove_tag!(Container.t(), Tag.t()) :: Container.t()
|
@spec remove_tag!(Container.t(), Tag.t(), User.t()) :: non_neg_integer()
|
||||||
def remove_tag!(%{id: container_id}, %{id: tag_id}), do: remove_tag!(container_id, tag_id)
|
def remove_tag!(
|
||||||
|
%Container{id: container_id, user_id: user_id},
|
||||||
@spec remove_tag!(Container.id(), Tag.id()) :: Container.t()
|
%Tag{id: tag_id, user_id: user_id},
|
||||||
def remove_tag!(container_id, tag_id)
|
%User{id: user_id}
|
||||||
when not (container_id |> is_nil()) and not (tag_id |> is_nil()) do
|
) do
|
||||||
|
{count, _} =
|
||||||
Repo.delete_all(
|
Repo.delete_all(
|
||||||
from ct in ContainerTag,
|
from ct in ContainerTag,
|
||||||
where: ct.container_id == ^container_id,
|
where: ct.container_id == ^container_id,
|
||||||
where: ct.tag_id == ^tag_id
|
where: ct.tag_id == ^tag_id,
|
||||||
|
where: ct.user_id == ^user_id
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if count == 0, do: raise("could not delete container tag"), else: count
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -14,18 +14,25 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
|
|||||||
Socket.t()
|
Socket.t()
|
||||||
) :: {:ok, Socket.t()}
|
) :: {:ok, Socket.t()}
|
||||||
def update(%{container: container} = assigns, socket) do
|
def update(%{container: container} = assigns, socket) do
|
||||||
assigns = assigns |> Map.put(:changeset, container |> Containers.change_container())
|
{:ok, socket |> assign(assigns) |> assign(:changeset, Containers.change_container(container))}
|
||||||
{:ok, socket |> assign(assigns)}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_event("validate", %{"container" => container_params}, socket) do
|
def handle_event(
|
||||||
changeset = socket.assigns.container |> Containers.change_container(container_params)
|
"validate",
|
||||||
|
%{"container" => container_params},
|
||||||
|
%{assigns: %{container: container}} = socket
|
||||||
|
) do
|
||||||
|
changeset = container |> Containers.change_container(container_params)
|
||||||
{:noreply, socket |> assign(:changeset, changeset)}
|
{:noreply, socket |> assign(:changeset, changeset)}
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_event("save", %{"container" => container_params}, socket) do
|
def handle_event(
|
||||||
save_container(socket, socket.assigns.action, container_params)
|
"save",
|
||||||
|
%{"container" => container_params},
|
||||||
|
%{assigns: %{action: action}} = socket
|
||||||
|
) do
|
||||||
|
save_container(socket, action, container_params)
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
@ -89,36 +96,40 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
|
|||||||
"""
|
"""
|
||||||
end
|
end
|
||||||
|
|
||||||
defp save_container(socket, :edit, container_params) do
|
defp save_container(
|
||||||
Containers.update_container(
|
%{assigns: %{container: container, current_user: current_user, return_to: return_to}} =
|
||||||
socket.assigns.container,
|
socket,
|
||||||
socket.assigns.current_user,
|
:edit,
|
||||||
container_params
|
container_params
|
||||||
)
|
) do
|
||||||
|> case do
|
socket =
|
||||||
{:ok, _container} ->
|
case Containers.update_container(container, current_user, container_params) do
|
||||||
{:noreply,
|
{:ok, %{name: container_name}} ->
|
||||||
socket
|
prompt = dgettext("prompts", "%{name} updated successfully", name: container_name)
|
||||||
|> put_flash(:info, dgettext("prompts", "Container updated successfully"))
|
socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
|
||||||
|> push_redirect(to: socket.assigns.return_to)}
|
|
||||||
|
|
||||||
{:error, %Changeset{} = changeset} ->
|
{:error, %Changeset{} = changeset} ->
|
||||||
{:noreply, socket |> assign(:changeset, changeset)}
|
socket |> assign(:changeset, changeset)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp save_container(socket, :new, container_params) do
|
{:noreply, socket}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp save_container(
|
||||||
|
%{assigns: %{current_user: current_user, return_to: return_to}} = socket,
|
||||||
|
:new,
|
||||||
container_params
|
container_params
|
||||||
|> Containers.create_container(socket.assigns.current_user)
|
) do
|
||||||
|> case do
|
socket =
|
||||||
{:ok, _container} ->
|
case Containers.create_container(container_params, current_user) do
|
||||||
{:noreply,
|
{:ok, %{name: container_name}} ->
|
||||||
socket
|
prompt = dgettext("prompts", "%{name} created successfully", name: container_name)
|
||||||
|> put_flash(:info, dgettext("prompts", "Container created successfully"))
|
socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
|
||||||
|> push_redirect(to: socket.assigns.return_to)}
|
|
||||||
|
|
||||||
{:error, %Changeset{} = changeset} ->
|
{:error, %Changeset{} = changeset} ->
|
||||||
{:noreply, socket |> assign(changeset: changeset)}
|
socket |> assign(changeset: changeset)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
{:noreply, socket}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -6,6 +6,7 @@ defmodule CanneryWeb.ContainerLive.Index do
|
|||||||
use CanneryWeb, :live_view
|
use CanneryWeb, :live_view
|
||||||
import CanneryWeb.ContainerLive.ContainerCard
|
import CanneryWeb.ContainerLive.ContainerCard
|
||||||
alias Cannery.{Containers, Containers.Container}
|
alias Cannery.{Containers, Containers.Container}
|
||||||
|
alias Ecto.Changeset
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def mount(_params, session, socket) do
|
def mount(_params, session, socket) do
|
||||||
@ -13,30 +14,26 @@ defmodule CanneryWeb.ContainerLive.Index do
|
|||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_params(params, _url, socket) do
|
def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
|
||||||
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
|
{:noreply, apply_action(socket, live_action, params)}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp apply_action(socket, :edit, %{"id" => id}) do
|
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
|
||||||
socket
|
socket
|
||||||
|> assign(:page_title, gettext("Edit Container"))
|
|> assign(:page_title, gettext("Edit Container"))
|
||||||
|> assign(:container, Containers.get_container!(id))
|
|> assign(:container, Containers.get_container!(id, current_user))
|
||||||
end
|
end
|
||||||
|
|
||||||
defp apply_action(socket, :new, _params) do
|
defp apply_action(socket, :new, _params) do
|
||||||
socket
|
socket |> assign(:page_title, gettext("New Container")) |> assign(:container, %Container{})
|
||||||
|> assign(:page_title, gettext("New Container"))
|
|
||||||
|> assign(:container, %Container{})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp apply_action(socket, :index, _params) do
|
defp apply_action(socket, :index, _params) do
|
||||||
socket
|
socket |> assign(:page_title, gettext("Listing Containers")) |> assign(:container, nil)
|
||||||
|> assign(:page_title, gettext("Listing Containers"))
|
|
||||||
|> assign(:container, nil)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_event("delete", %{"id" => id}, socket) do
|
def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
|
||||||
socket =
|
socket =
|
||||||
socket.assigns.containers
|
socket.assigns.containers
|
||||||
|> Enum.find(fn %{id: container_id} -> id == container_id end)
|
|> Enum.find(fn %{id: container_id} -> id == container_id end)
|
||||||
@ -45,27 +42,23 @@ defmodule CanneryWeb.ContainerLive.Index do
|
|||||||
socket |> put_flash(:error, dgettext("errors", "Could not find that container"))
|
socket |> put_flash(:error, dgettext("errors", "Could not find that container"))
|
||||||
|
|
||||||
container ->
|
container ->
|
||||||
container
|
case Containers.delete_container(container, current_user) do
|
||||||
|> Containers.delete_container(socket.assigns.current_user)
|
{:ok, %{name: container_name}} ->
|
||||||
|> case do
|
prompt = dgettext("prompts", "%{name} has been deleted", name: container_name)
|
||||||
{:ok, container} ->
|
socket |> put_flash(:info, prompt) |> display_containers()
|
||||||
socket
|
|
||||||
|> put_flash(
|
|
||||||
:info,
|
|
||||||
dgettext("prompts", "%{name} has been deleted", name: container.name)
|
|
||||||
)
|
|
||||||
|> display_containers()
|
|
||||||
|
|
||||||
{:error, %{action: :delete, errors: [ammo_groups: _error], valid?: false} = changeset} ->
|
{:error, %{action: :delete, errors: [ammo_groups: _error], valid?: false} = changeset} ->
|
||||||
ammo_groups_error = changeset |> changeset_errors(:ammo_groups) |> Enum.join(", ")
|
ammo_groups_error = changeset |> changeset_errors(:ammo_groups) |> Enum.join(", ")
|
||||||
|
|
||||||
socket
|
prompt =
|
||||||
|> put_flash(
|
dgettext(
|
||||||
:error,
|
"errors",
|
||||||
dgettext("errors", "Could not delete container: %{error}",
|
"Could not delete %{name}: %{error}",
|
||||||
|
name: changeset |> Changeset.get_field(:name, "container"),
|
||||||
error: ammo_groups_error
|
error: ammo_groups_error
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
socket |> put_flash(:error, prompt)
|
||||||
|
|
||||||
{:error, changeset} ->
|
{:error, changeset} ->
|
||||||
socket |> put_flash(:error, changeset |> changeset_errors())
|
socket |> put_flash(:error, changeset |> changeset_errors())
|
||||||
@ -76,7 +69,6 @@ defmodule CanneryWeb.ContainerLive.Index do
|
|||||||
end
|
end
|
||||||
|
|
||||||
defp display_containers(%{assigns: %{current_user: current_user}} = socket) do
|
defp display_containers(%{assigns: %{current_user: current_user}} = socket) do
|
||||||
containers = Containers.list_containers(current_user)
|
socket |> assign(containers: Containers.list_containers(current_user))
|
||||||
socket |> assign(containers: containers)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -6,6 +6,7 @@ defmodule CanneryWeb.ContainerLive.Show do
|
|||||||
use CanneryWeb, :live_view
|
use CanneryWeb, :live_view
|
||||||
import CanneryWeb.AmmoGroupLive.AmmoGroupCard
|
import CanneryWeb.AmmoGroupLive.AmmoGroupCard
|
||||||
alias Cannery.{Containers, Repo}
|
alias Cannery.{Containers, Repo}
|
||||||
|
alias Ecto.Changeset
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def mount(_params, session, socket) do
|
def mount(_params, session, socket) do
|
||||||
@ -13,40 +14,48 @@ defmodule CanneryWeb.ContainerLive.Show do
|
|||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_params(%{"id" => id}, _, socket) do
|
def handle_params(
|
||||||
|
%{"id" => id},
|
||||||
|
_,
|
||||||
|
%{assigns: %{current_user: current_user, live_action: live_action}} = socket
|
||||||
|
) do
|
||||||
socket =
|
socket =
|
||||||
socket
|
socket
|
||||||
|> assign(
|
|> assign(
|
||||||
page_title: page_title(socket.assigns.live_action),
|
page_title: page_title(live_action),
|
||||||
container: Containers.get_container!(id) |> Repo.preload(:ammo_groups)
|
container: Containers.get_container!(id, current_user) |> Repo.preload(:ammo_groups)
|
||||||
)
|
)
|
||||||
|
|
||||||
{:noreply, socket}
|
{:noreply, socket}
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_event("delete", _, socket) do
|
def handle_event(
|
||||||
|
"delete",
|
||||||
|
_,
|
||||||
|
%{assigns: %{container: container, current_user: current_user}} = socket
|
||||||
|
) do
|
||||||
socket =
|
socket =
|
||||||
socket.assigns.container
|
Containers.delete_container(container, current_user)
|
||||||
|> Containers.delete_container(socket.assigns.current_user)
|
|
||||||
|> case do
|
|> case do
|
||||||
{:ok, container} ->
|
{:ok, %{name: container_name}} ->
|
||||||
|
prompt = dgettext("prompts", "%{name} has been deleted", name: container_name)
|
||||||
|
|
||||||
socket
|
socket
|
||||||
|> put_flash(
|
|> put_flash(:info, prompt)
|
||||||
:info,
|
|
||||||
dgettext("prompts", "%{name} has been deleted", name: container.name)
|
|
||||||
)
|
|
||||||
|> push_redirect(to: Routes.container_index_path(socket, :index))
|
|> push_redirect(to: Routes.container_index_path(socket, :index))
|
||||||
|
|
||||||
{:error, %{action: :delete, errors: [ammo_groups: _error], valid?: false} = changeset} ->
|
{:error, %{action: :delete, errors: [ammo_groups: _error], valid?: false} = changeset} ->
|
||||||
ammo_groups_error = changeset |> changeset_errors(:ammo_groups) |> Enum.join(", ")
|
ammo_groups_error = changeset |> changeset_errors(:ammo_groups) |> Enum.join(", ")
|
||||||
|
|
||||||
socket
|
prompt =
|
||||||
|> put_flash(
|
dgettext("errors", "Could not delete %{name}: %{error}",
|
||||||
:error,
|
name: changeset |> Changeset.get_field(:name, "container"),
|
||||||
dgettext("errors", "Could not delete container: %{error}", error: ammo_groups_error)
|
error: ammo_groups_error
|
||||||
)
|
)
|
||||||
|
|
||||||
|
socket |> put_flash(:error, prompt)
|
||||||
|
|
||||||
{:error, changeset} ->
|
{:error, changeset} ->
|
||||||
socket |> put_flash(:error, changeset |> changeset_errors())
|
socket |> put_flash(:error, changeset |> changeset_errors())
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user