From 9ca69ae3e49341d94e367a56679a845e70481028 Mon Sep 17 00:00:00 2001 From: shibao Date: Mon, 31 Jan 2022 23:20:57 -0500 Subject: [PATCH] style ammo type show page --- lib/cannery/ammo.ex | 109 +++++++++++------- lib/cannery_web/live/ammo_type_live/show.ex | 6 + .../live/ammo_type_live/show.html.heex | 54 +++++++++ .../live/ammo_type_live/show.html.leex | 47 -------- 4 files changed, 126 insertions(+), 90 deletions(-) create mode 100644 lib/cannery_web/live/ammo_type_live/show.html.heex delete mode 100644 lib/cannery_web/live/ammo_type_live/show.html.leex diff --git a/lib/cannery/ammo.ex b/lib/cannery/ammo.ex index 52ebae09..40b9391d 100644 --- a/lib/cannery/ammo.ex +++ b/lib/cannery/ammo.ex @@ -4,9 +4,9 @@ defmodule Cannery.Ammo do """ import Ecto.Query, warn: false - alias Cannery.Repo - - alias Cannery.Ammo.AmmoType + alias Cannery.{Accounts.User, Repo} + alias Cannery.Ammo.{AmmoGroup, AmmoType} + alias Ecto.Changeset @doc """ Returns the list of ammo_types. @@ -17,9 +17,8 @@ defmodule Cannery.Ammo do [%AmmoType{}, ...] """ - def list_ammo_types do - Repo.all(AmmoType) - end + @spec list_ammo_types() :: [AmmoType.t()] + def list_ammo_types, do: Repo.all(AmmoType) @doc """ Gets a single ammo_type. @@ -35,6 +34,7 @@ defmodule Cannery.Ammo do ** (Ecto.NoResultsError) """ + @spec get_ammo_type!(AmmoType.id()) :: AmmoType.t() def get_ammo_type!(id), do: Repo.get!(AmmoType, id) @doc """ @@ -49,11 +49,9 @@ defmodule Cannery.Ammo do {:error, %Ecto.Changeset{}} """ - def create_ammo_type(attrs \\ %{}) do - %AmmoType{} - |> AmmoType.changeset(attrs) - |> Repo.insert() - end + @spec create_ammo_type(attrs :: map()) :: {:ok, AmmoType.t()} | {:error, Changeset.t()} + def create_ammo_type(attrs \\ %{}), + do: %AmmoType{} |> AmmoType.changeset(attrs) |> Repo.insert() @doc """ Updates a ammo_type. @@ -67,11 +65,10 @@ defmodule Cannery.Ammo do {:error, %Ecto.Changeset{}} """ - def update_ammo_type(%AmmoType{} = ammo_type, attrs) do - ammo_type - |> AmmoType.changeset(attrs) - |> Repo.update() - end + @spec update_ammo_type(AmmoType.t(), attrs :: map()) :: + {:ok, AmmoType.t()} | {:error, Changeset.t()} + def update_ammo_type(%AmmoType{} = ammo_type, attrs), + do: ammo_type |> AmmoType.changeset(attrs) |> Repo.update() @doc """ Deletes a ammo_type. @@ -85,9 +82,20 @@ defmodule Cannery.Ammo do {:error, %Ecto.Changeset{}} """ - def delete_ammo_type(%AmmoType{} = ammo_type) do - Repo.delete(ammo_type) - end + @spec delete_ammo_type(AmmoType.t()) :: {:ok, AmmoType.t()} | {:error, Changeset.t()} + def delete_ammo_type(%AmmoType{} = ammo_type), do: ammo_type |> Repo.delete() + + @doc """ + Deletes a ammo_type. + + ## Examples + + iex> delete_ammo_type(ammo_type) + %AmmoType{} + + """ + @spec delete_ammo_type!(AmmoType.t()) :: AmmoType.t() + def delete_ammo_type!(%AmmoType{} = ammo_type), do: ammo_type |> Repo.delete!() @doc """ Returns an `%Ecto.Changeset{}` for tracking ammo_type changes. @@ -98,23 +106,28 @@ defmodule Cannery.Ammo do %Ecto.Changeset{data: %AmmoType{}} """ - def change_ammo_type(%AmmoType{} = ammo_type, attrs \\ %{}) do - AmmoType.changeset(ammo_type, attrs) - end - - alias Cannery.Ammo.AmmoGroup + @spec change_ammo_type(AmmoType.t() | AmmoType.new_ammo_type()) :: Changeset.t() + @spec change_ammo_type(AmmoType.t() | AmmoType.new_ammo_type(), attrs :: map()) :: Changeset.t() + def change_ammo_type(%AmmoType{} = ammo_type, attrs \\ %{}), + do: AmmoType.changeset(ammo_type, attrs) @doc """ Returns the list of ammo_groups. ## Examples - iex> list_ammo_groups() + iex> list_ammo_groups(%User{id: 123}) + [%AmmoGroup{}, ...] + + iex> list_ammo_groups(123) [%AmmoGroup{}, ...] """ - def list_ammo_groups do - Repo.all(AmmoGroup) + @spec list_ammo_groups(User.t() | User.id()) :: [AmmoGroup.t()] + def list_ammo_groups(%{id: user_id}), do: list_ammo_groups(user_id) + + def list_ammo_groups(user_id) do + Repo.all(from am in AmmoGroup, where: am.user_id == ^user_id) end @doc """ @@ -131,6 +144,7 @@ defmodule Cannery.Ammo do ** (Ecto.NoResultsError) """ + @spec get_ammo_group!(AmmoGroup.id()) :: AmmoGroup.t() def get_ammo_group!(id), do: Repo.get!(AmmoGroup, id) @doc """ @@ -145,11 +159,9 @@ defmodule Cannery.Ammo do {:error, %Ecto.Changeset{}} """ - def create_ammo_group(attrs \\ %{}) do - %AmmoGroup{} - |> AmmoGroup.changeset(attrs) - |> Repo.insert() - end + @spec create_ammo_group(attrs :: map()) :: {:ok, AmmoGroup.t()} | {:error, Changeset.t()} + def create_ammo_group(attrs \\ %{}), + do: %AmmoGroup{} |> AmmoGroup.changeset(attrs) |> Repo.insert() @doc """ Updates a ammo_group. @@ -163,11 +175,10 @@ defmodule Cannery.Ammo do {:error, %Ecto.Changeset{}} """ - def update_ammo_group(%AmmoGroup{} = ammo_group, attrs) do - ammo_group - |> AmmoGroup.changeset(attrs) - |> Repo.update() - end + @spec update_ammo_group(AmmoGroup.t(), attrs :: map()) :: + {:ok, AmmoGroup.t()} | {:error, Changeset.t()} + def update_ammo_group(%AmmoGroup{} = ammo_group, attrs), + do: ammo_group |> AmmoGroup.changeset(attrs) |> Repo.update() @doc """ Deletes a ammo_group. @@ -181,9 +192,20 @@ defmodule Cannery.Ammo do {:error, %Ecto.Changeset{}} """ - def delete_ammo_group(%AmmoGroup{} = ammo_group) do - Repo.delete(ammo_group) - end + @spec delete_ammo_group(AmmoGroup.t()) :: {:ok, AmmoGroup.t()} | {:error, Changeset.t()} + def delete_ammo_group(%AmmoGroup{} = ammo_group), do: ammo_group |> Repo.delete() + + @doc """ + Deletes a ammo_group. + + ## Examples + + iex> delete_ammo_group!(ammo_group) + %AmmoGroup{} + + """ + @spec delete_ammo_group!(AmmoGroup.t()) :: AmmoGroup.t() + def delete_ammo_group!(%AmmoGroup{} = ammo_group), do: ammo_group |> Repo.delete!() @doc """ Returns an `%Ecto.Changeset{}` for tracking ammo_group changes. @@ -194,7 +216,8 @@ defmodule Cannery.Ammo do %Ecto.Changeset{data: %AmmoGroup{}} """ - def change_ammo_group(%AmmoGroup{} = ammo_group, attrs \\ %{}) do - AmmoGroup.changeset(ammo_group, attrs) - end + @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) end diff --git a/lib/cannery_web/live/ammo_type_live/show.ex b/lib/cannery_web/live/ammo_type_live/show.ex index 1303522c..f9e511c7 100644 --- a/lib/cannery_web/live/ammo_type_live/show.ex +++ b/lib/cannery_web/live/ammo_type_live/show.ex @@ -20,6 +20,12 @@ defmodule CanneryWeb.AmmoTypeLive.Show do |> assign(:ammo_type, Ammo.get_ammo_type!(id))} 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))} + end + defp page_title(:show), do: "Show Ammo type" defp page_title(:edit), do: "Edit Ammo type" end diff --git a/lib/cannery_web/live/ammo_type_live/show.html.heex b/lib/cannery_web/live/ammo_type_live/show.html.heex new file mode 100644 index 00000000..f7166cc1 --- /dev/null +++ b/lib/cannery_web/live/ammo_type_live/show.html.heex @@ -0,0 +1,54 @@ +
+

+ <%= @ammo_type.name %> +

+ +
+ <%= live_redirect "Back", to: Routes.ammo_type_index_path(@socket, :index), class: "link" %> + <%= live_patch "Edit", to: Routes.ammo_type_show_path(@socket, :edit, @ammo_type), class: "button" %> + <%= link("Delete", + to: "#", + class: "link", + phx_click: "delete", + data: [confirm: "Are you sure you want to delete #{@ammo_type.name}?"] + ) %> +
+ +
+ + +
+ +<%= if @live_action in [:edit] do %> + <%= live_modal CanneryWeb.AmmoTypeLive.FormComponent, + id: @ammo_type.id, + title: @page_title, + action: @live_action, + ammo_type: @ammo_type, + return_to: Routes.ammo_type_show_path(@socket, :show, @ammo_type) %> +<% end %> diff --git a/lib/cannery_web/live/ammo_type_live/show.html.leex b/lib/cannery_web/live/ammo_type_live/show.html.leex deleted file mode 100644 index 93e323c7..00000000 --- a/lib/cannery_web/live/ammo_type_live/show.html.leex +++ /dev/null @@ -1,47 +0,0 @@ -

Show Ammo type

- -<%= if @live_action in [:edit] do %> - <%= live_modal CanneryWeb.AmmoTypeLive.FormComponent, - id: @ammo_type.id, - title: @page_title, - action: @live_action, - ammo_type: @ammo_type, - return_to: Routes.ammo_type_show_path(@socket, :show, @ammo_type) %> -<% end %> - - - -<%= live_patch "Edit", to: Routes.ammo_type_show_path(@socket, :edit, @ammo_type), class: "button" %> -<%= live_redirect "Back", to: Routes.ammo_type_index_path(@socket, :index) %>