improve Ammo.list_types
This commit is contained in:
		@@ -15,31 +15,31 @@ defmodule Cannery.Ammo do
 | 
				
			|||||||
  @pack_preloads [:type]
 | 
					  @pack_preloads [:type]
 | 
				
			||||||
  @type_preloads [:packs]
 | 
					  @type_preloads [:packs]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @type list_types_option :: {:search, String.t() | nil} | {:class, Type.class() | :all}
 | 
				
			||||||
 | 
					  @type list_types_options :: [list_types_option()]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @doc """
 | 
					  @doc """
 | 
				
			||||||
  Returns the list of types.
 | 
					  Returns the list of types.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  ## Examples
 | 
					  ## Examples
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      iex> list_types(%User{id: 123}, :all)
 | 
					      iex> list_types(%User{id: 123})
 | 
				
			||||||
      [%Type{}, ...]
 | 
					      [%Type{}, ...]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      iex> list_types("cool", %User{id: 123}, :shotgun)
 | 
					      iex> list_types(%User{id: 123}, search: "cool", class: :shotgun)
 | 
				
			||||||
      [%Type{name: "My cool type", class: :shotgun}, ...]
 | 
					      [%Type{name: "My cool type", class: :shotgun}, ...]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  """
 | 
					  """
 | 
				
			||||||
  @spec list_types(User.t(), Type.class() | :all) :: [Type.t()]
 | 
					  @spec list_types(User.t()) :: [Type.t()]
 | 
				
			||||||
  @spec list_types(search :: nil | String.t(), User.t(), Type.class() | :all) ::
 | 
					  @spec list_types(User.t(), list_types_options()) :: [Type.t()]
 | 
				
			||||||
          [Type.t()]
 | 
					  def list_types(%User{id: user_id}, opts \\ []) do
 | 
				
			||||||
  def list_types(search \\ nil, user, type)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  def list_types(search, %User{id: user_id}, type) do
 | 
					 | 
				
			||||||
    from(t in Type,
 | 
					    from(t in Type,
 | 
				
			||||||
      as: :t,
 | 
					      as: :t,
 | 
				
			||||||
      where: t.user_id == ^user_id,
 | 
					      where: t.user_id == ^user_id,
 | 
				
			||||||
      preload: ^@type_preloads
 | 
					      preload: ^@type_preloads
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
    |> list_types_filter_type(type)
 | 
					    |> list_types_filter_class(Keyword.get(opts, :class, :all))
 | 
				
			||||||
    |> list_types_filter_search(search)
 | 
					    |> list_types_filter_search(Keyword.get(opts, :search))
 | 
				
			||||||
    |> Repo.all()
 | 
					    |> Repo.all()
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -72,17 +72,17 @@ defmodule Cannery.Ammo do
 | 
				
			|||||||
    )
 | 
					    )
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @spec list_types_filter_type(Queryable.t(), Type.class() | :all) :: Queryable.t()
 | 
					  @spec list_types_filter_class(Queryable.t(), Type.class() | :all) :: Queryable.t()
 | 
				
			||||||
  defp list_types_filter_type(query, :rifle),
 | 
					  defp list_types_filter_class(query, :rifle),
 | 
				
			||||||
    do: query |> where([t: t], t.class == :rifle)
 | 
					    do: query |> where([t: t], t.class == :rifle)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  defp list_types_filter_type(query, :pistol),
 | 
					  defp list_types_filter_class(query, :pistol),
 | 
				
			||||||
    do: query |> where([t: t], t.class == :pistol)
 | 
					    do: query |> where([t: t], t.class == :pistol)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  defp list_types_filter_type(query, :shotgun),
 | 
					  defp list_types_filter_class(query, :shotgun),
 | 
				
			||||||
    do: query |> where([t: t], t.class == :shotgun)
 | 
					    do: query |> where([t: t], t.class == :shotgun)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  defp list_types_filter_type(query, _all), do: query
 | 
					  defp list_types_filter_class(query, _all), do: query
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @doc """
 | 
					  @doc """
 | 
				
			||||||
  Returns a count of types.
 | 
					  Returns a count of types.
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,7 +3,7 @@ defmodule CanneryWeb.ExportController do
 | 
				
			|||||||
  alias Cannery.{ActivityLog, Ammo, Containers}
 | 
					  alias Cannery.{ActivityLog, Ammo, Containers}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def export(%{assigns: %{current_user: current_user}} = conn, %{"mode" => "json"}) do
 | 
					  def export(%{assigns: %{current_user: current_user}} = conn, %{"mode" => "json"}) do
 | 
				
			||||||
    types = Ammo.list_types(current_user, :all)
 | 
					    types = Ammo.list_types(current_user)
 | 
				
			||||||
    used_counts = types |> ActivityLog.get_used_count_for_types(current_user)
 | 
					    used_counts = types |> ActivityLog.get_used_count_for_types(current_user)
 | 
				
			||||||
    round_counts = types |> Ammo.get_round_count_for_types(current_user)
 | 
					    round_counts = types |> Ammo.get_round_count_for_types(current_user)
 | 
				
			||||||
    pack_counts = types |> Ammo.get_packs_count_for_types(current_user)
 | 
					    pack_counts = types |> Ammo.get_packs_count_for_types(current_user)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -26,7 +26,7 @@ defmodule CanneryWeb.PackLive.FormComponent do
 | 
				
			|||||||
      socket =
 | 
					      socket =
 | 
				
			||||||
      socket
 | 
					      socket
 | 
				
			||||||
      |> assign(:pack_create_limit, @pack_create_limit)
 | 
					      |> assign(:pack_create_limit, @pack_create_limit)
 | 
				
			||||||
      |> assign(:types, Ammo.list_types(current_user, :all))
 | 
					      |> assign(:types, Ammo.list_types(current_user))
 | 
				
			||||||
      |> assign_new(:containers, fn -> Containers.list_containers(current_user) end)
 | 
					      |> assign_new(:containers, fn -> Containers.list_containers(current_user) end)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    params =
 | 
					    params =
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -106,7 +106,7 @@ defmodule CanneryWeb.TypeLive.Index do
 | 
				
			|||||||
       ) do
 | 
					       ) do
 | 
				
			||||||
    socket
 | 
					    socket
 | 
				
			||||||
    |> assign(
 | 
					    |> assign(
 | 
				
			||||||
      types: Ammo.list_types(search, current_user, class),
 | 
					      types: Ammo.list_types(current_user, class: class, search: search),
 | 
				
			||||||
      types_count: Ammo.get_types_count!(current_user)
 | 
					      types_count: Ammo.get_types_count!(current_user)
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -85,7 +85,7 @@ defmodule Cannery.AmmoTest do
 | 
				
			|||||||
      pistol_type: pistol_type,
 | 
					      pistol_type: pistol_type,
 | 
				
			||||||
      current_user: current_user
 | 
					      current_user: current_user
 | 
				
			||||||
    } do
 | 
					    } do
 | 
				
			||||||
      results = Ammo.list_types(current_user, :all)
 | 
					      results = Ammo.list_types(current_user, class: :all)
 | 
				
			||||||
      assert results |> Enum.count() == 3
 | 
					      assert results |> Enum.count() == 3
 | 
				
			||||||
      assert rifle_type in results
 | 
					      assert rifle_type in results
 | 
				
			||||||
      assert shotgun_type in results
 | 
					      assert shotgun_type in results
 | 
				
			||||||
@@ -96,21 +96,21 @@ defmodule Cannery.AmmoTest do
 | 
				
			|||||||
      rifle_type: rifle_type,
 | 
					      rifle_type: rifle_type,
 | 
				
			||||||
      current_user: current_user
 | 
					      current_user: current_user
 | 
				
			||||||
    } do
 | 
					    } do
 | 
				
			||||||
      assert [^rifle_type] = Ammo.list_types(current_user, :rifle)
 | 
					      assert [^rifle_type] = Ammo.list_types(current_user, class: :rifle)
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    test "list_types/2 returns shotgun types", %{
 | 
					    test "list_types/2 returns shotgun types", %{
 | 
				
			||||||
      shotgun_type: shotgun_type,
 | 
					      shotgun_type: shotgun_type,
 | 
				
			||||||
      current_user: current_user
 | 
					      current_user: current_user
 | 
				
			||||||
    } do
 | 
					    } do
 | 
				
			||||||
      assert [^shotgun_type] = Ammo.list_types(current_user, :shotgun)
 | 
					      assert [^shotgun_type] = Ammo.list_types(current_user, class: :shotgun)
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    test "list_types/2 returns pistol types", %{
 | 
					    test "list_types/2 returns pistol types", %{
 | 
				
			||||||
      pistol_type: pistol_type,
 | 
					      pistol_type: pistol_type,
 | 
				
			||||||
      current_user: current_user
 | 
					      current_user: current_user
 | 
				
			||||||
    } do
 | 
					    } do
 | 
				
			||||||
      assert [^pistol_type] = Ammo.list_types(current_user, :pistol)
 | 
					      assert [^pistol_type] = Ammo.list_types(current_user, class: :pistol)
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    test "list_types/2 returns relevant types for a user", %{
 | 
					    test "list_types/2 returns relevant types for a user", %{
 | 
				
			||||||
@@ -120,22 +120,22 @@ defmodule Cannery.AmmoTest do
 | 
				
			|||||||
      current_user: current_user
 | 
					      current_user: current_user
 | 
				
			||||||
    } do
 | 
					    } do
 | 
				
			||||||
      # name
 | 
					      # name
 | 
				
			||||||
      assert Ammo.list_types("bullet", current_user, :all) == [rifle_type]
 | 
					      assert Ammo.list_types(current_user, search: "bullet") == [rifle_type]
 | 
				
			||||||
      assert Ammo.list_types("bullets", current_user, :all) == [rifle_type]
 | 
					      assert Ammo.list_types(current_user, search: "bullets") == [rifle_type]
 | 
				
			||||||
      assert Ammo.list_types("hollow", current_user, :all) == [shotgun_type]
 | 
					      assert Ammo.list_types(current_user, search: "hollow") == [shotgun_type]
 | 
				
			||||||
      assert Ammo.list_types("jacket", current_user, :all) == [pistol_type]
 | 
					      assert Ammo.list_types(current_user, search: "jacket") == [pistol_type]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      # desc
 | 
					      # desc
 | 
				
			||||||
      assert Ammo.list_types("pew", current_user, :all) == [rifle_type]
 | 
					      assert Ammo.list_types(current_user, search: "pew") == [rifle_type]
 | 
				
			||||||
      assert Ammo.list_types("brass", current_user, :all) == [pistol_type]
 | 
					      assert Ammo.list_types(current_user, search: "brass") == [pistol_type]
 | 
				
			||||||
      assert Ammo.list_types("shell", current_user, :all) == [pistol_type]
 | 
					      assert Ammo.list_types(current_user, search: "shell") == [pistol_type]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      # grains (integer)
 | 
					      # grains (integer)
 | 
				
			||||||
      assert Ammo.list_types("53453", current_user, :all) == [rifle_type]
 | 
					      assert Ammo.list_types(current_user, search: "53453") == [rifle_type]
 | 
				
			||||||
      assert Ammo.list_types("3234234", current_user, :all) == [shotgun_type]
 | 
					      assert Ammo.list_types(current_user, search: "3234234") == [shotgun_type]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      # tracer (boolean)
 | 
					      # tracer (boolean)
 | 
				
			||||||
      assert Ammo.list_types("tracer", current_user, :all) == [pistol_type]
 | 
					      assert Ammo.list_types(current_user, search: "tracer") == [pistol_type]
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user