improve containers context
This commit is contained in:
parent
84f36db1d1
commit
c27c162562
@ -44,17 +44,18 @@ defmodule Cannery.Containers do
|
|||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
iex> create_container(%{field: value})
|
iex> create_container(%{field: value}, user)
|
||||||
{:ok, %Container{}}
|
{:ok, %Container{}}
|
||||||
|
|
||||||
iex> create_container(%{field: bad_value})
|
iex> create_container(%{field: bad_value}, user)
|
||||||
{:error, %Changeset{}}
|
{:error, %Changeset{}}
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@spec create_container(attrs :: map()) ::
|
@spec create_container(attrs :: map(), User.t()) ::
|
||||||
{:ok, Container.t()} | {:error, Changeset.t(Container.new_container())}
|
{:ok, Container.t()} | {:error, Changeset.t(Container.new_container())}
|
||||||
def create_container(attrs) do
|
def create_container(attrs, %User{id: user_id}) do
|
||||||
%Container{} |> Container.changeset(attrs) |> Repo.insert()
|
attrs = attrs |> Map.put("user_id", user_id)
|
||||||
|
%Container{} |> Container.create_changeset(attrs) |> Repo.insert()
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
@ -62,17 +63,17 @@ defmodule Cannery.Containers do
|
|||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
iex> update_container(container, %{field: new_value})
|
iex> update_container(container, user, %{field: new_value})
|
||||||
{:ok, %Container{}}
|
{:ok, %Container{}}
|
||||||
|
|
||||||
iex> update_container(container, %{field: bad_value})
|
iex> update_container(container, user, %{field: bad_value})
|
||||||
{:error, %Changeset{}}
|
{:error, %Changeset{}}
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@spec update_container(Container.t(), attrs :: map()) ::
|
@spec update_container(Container.t(), User.t(), attrs :: map()) ::
|
||||||
{:ok, Container.t()} | {:error, Changeset.t(Container.t())}
|
{:ok, Container.t()} | {:error, Changeset.t(Container.t())}
|
||||||
def update_container(container, attrs) do
|
def update_container(%Container{user_id: user_id} = container, %User{id: user_id}, attrs) do
|
||||||
container |> Container.changeset(attrs) |> Repo.update()
|
container |> Container.update_changeset(attrs) |> Repo.update()
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
@ -80,16 +81,16 @@ defmodule Cannery.Containers do
|
|||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
iex> delete_container(container)
|
iex> delete_container(container, user)
|
||||||
{:ok, %Container{}}
|
{:ok, %Container{}}
|
||||||
|
|
||||||
iex> delete_container(container)
|
iex> delete_container(container, user)
|
||||||
{:error, %Changeset{}}
|
{:error, %Changeset{}}
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@spec delete_container(Container.t()) ::
|
@spec delete_container(Container.t(), User.t()) ::
|
||||||
{:ok, Container.t()} | {:error, Changeset.t(Container.t())}
|
{:ok, Container.t()} | {:error, Changeset.t(Container.t())}
|
||||||
def delete_container(container) do
|
def delete_container(%Container{user_id: user_id} = container, %User{id: user_id}) do
|
||||||
Repo.one(
|
Repo.one(
|
||||||
from ag in AmmoGroup,
|
from ag in AmmoGroup,
|
||||||
where: ag.container_id == ^container.id,
|
where: ag.container_id == ^container.id,
|
||||||
@ -125,13 +126,13 @@ defmodule Cannery.Containers do
|
|||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
iex> delete_container(container)
|
iex> delete_container(container, user)
|
||||||
%Container{}
|
%Container{}
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@spec delete_container!(Container.t()) :: Container.t()
|
@spec delete_container!(Container.t(), User.t()) :: Container.t()
|
||||||
def delete_container!(container) do
|
def delete_container!(container, user) do
|
||||||
{:ok, container} = container |> delete_container()
|
{:ok, container} = container |> delete_container(user)
|
||||||
container
|
container
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -151,7 +152,8 @@ defmodule Cannery.Containers do
|
|||||||
Changeset.t(Container.t() | Container.new_container())
|
Changeset.t(Container.t() | Container.new_container())
|
||||||
@spec change_container(Container.t() | Container.new_container(), attrs :: map()) ::
|
@spec change_container(Container.t() | Container.new_container(), attrs :: map()) ::
|
||||||
Changeset.t(Container.t() | Container.new_container())
|
Changeset.t(Container.t() | Container.new_container())
|
||||||
def change_container(container, attrs \\ %{}), do: container |> Container.changeset(attrs)
|
def change_container(container, attrs \\ %{}),
|
||||||
|
do: container |> Container.update_changeset(attrs)
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Adds a tag to a container
|
Adds a tag to a container
|
||||||
|
@ -39,10 +39,20 @@ defmodule Cannery.Containers.Container do
|
|||||||
@type id :: UUID.t()
|
@type id :: UUID.t()
|
||||||
|
|
||||||
@doc false
|
@doc false
|
||||||
@spec changeset(t() | new_container(), attrs :: map()) :: Changeset.t(t() | new_container())
|
@spec create_changeset(t() | new_container(), attrs :: map()) ::
|
||||||
def changeset(container, attrs) do
|
Changeset.t(t() | new_container())
|
||||||
|
def create_changeset(container, attrs) do
|
||||||
container
|
container
|
||||||
|> cast(attrs, [:name, :desc, :type, :location, :user_id])
|
|> cast(attrs, [:name, :desc, :type, :location, :user_id])
|
||||||
|> validate_required([:name, :type, :user_id])
|
|> validate_required([:name, :type, :user_id])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc false
|
||||||
|
@spec update_changeset(t() | new_container(), attrs :: map()) ::
|
||||||
|
Changeset.t(t() | new_container())
|
||||||
|
def update_changeset(container, attrs) do
|
||||||
|
container
|
||||||
|
|> cast(attrs, [:name, :desc, :type, :location])
|
||||||
|
|> validate_required([:name, :type])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -22,7 +22,6 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
|
|||||||
end
|
end
|
||||||
|
|
||||||
def handle_event("save", %{"container" => container_params}, socket) do
|
def handle_event("save", %{"container" => container_params}, socket) do
|
||||||
container_params = container_params |> Map.put("user_id", socket.assigns.current_user.id)
|
|
||||||
save_container(socket, socket.assigns.action, container_params)
|
save_container(socket, socket.assigns.action, container_params)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -88,7 +87,12 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
|
|||||||
end
|
end
|
||||||
|
|
||||||
defp save_container(socket, :edit, container_params) do
|
defp save_container(socket, :edit, container_params) do
|
||||||
case Containers.update_container(socket.assigns.container, container_params) do
|
Containers.update_container(
|
||||||
|
socket.assigns.container,
|
||||||
|
socket.assigns.current_user,
|
||||||
|
container_params
|
||||||
|
)
|
||||||
|
|> case do
|
||||||
{:ok, _container} ->
|
{:ok, _container} ->
|
||||||
{:noreply,
|
{:noreply,
|
||||||
socket
|
socket
|
||||||
@ -101,7 +105,9 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
|
|||||||
end
|
end
|
||||||
|
|
||||||
defp save_container(socket, :new, container_params) do
|
defp save_container(socket, :new, container_params) do
|
||||||
case Containers.create_container(container_params) do
|
container_params
|
||||||
|
|> Containers.create_container(socket.assigns.current_user)
|
||||||
|
|> case do
|
||||||
{:ok, _container} ->
|
{:ok, _container} ->
|
||||||
{:noreply,
|
{:noreply,
|
||||||
socket
|
socket
|
||||||
|
@ -46,7 +46,7 @@ defmodule CanneryWeb.ContainerLive.Index do
|
|||||||
|
|
||||||
container ->
|
container ->
|
||||||
container
|
container
|
||||||
|> Containers.delete_container()
|
|> Containers.delete_container(socket.assigns.current_user)
|
||||||
|> case do
|
|> case do
|
||||||
{:ok, container} ->
|
{:ok, container} ->
|
||||||
socket
|
socket
|
||||||
|
@ -28,7 +28,7 @@ defmodule CanneryWeb.ContainerLive.Show do
|
|||||||
def handle_event("delete", _, socket) do
|
def handle_event("delete", _, socket) do
|
||||||
socket =
|
socket =
|
||||||
socket.assigns.container
|
socket.assigns.container
|
||||||
|> Containers.delete_container()
|
|> Containers.delete_container(socket.assigns.current_user)
|
||||||
|> case do
|
|> case do
|
||||||
{:ok, container} ->
|
{:ok, container} ->
|
||||||
socket
|
socket
|
||||||
|
@ -94,7 +94,7 @@ msgid "must be equal to %{number}"
|
|||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#, elixir-format, ex-autogen
|
#, elixir-format, ex-autogen
|
||||||
#: lib/cannery/containers.ex:104
|
#: lib/cannery/containers.ex:105
|
||||||
msgid "There is still %{amount} ammo group in this container!"
|
msgid "There is still %{amount} ammo group in this container!"
|
||||||
msgid_plural "There are still %{amount} ammo groups in this container!"
|
msgid_plural "There are still %{amount} ammo groups in this container!"
|
||||||
msgstr[0] ""
|
msgstr[0] ""
|
||||||
|
@ -1,12 +1,17 @@
|
|||||||
defmodule Cannery.ContainersTest do
|
defmodule Cannery.ContainersTest do
|
||||||
|
@moduledoc """
|
||||||
|
Tests for the Containers context
|
||||||
|
"""
|
||||||
|
|
||||||
use Cannery.DataCase
|
use Cannery.DataCase
|
||||||
|
|
||||||
alias Cannery.Containers
|
alias Cannery.Containers
|
||||||
|
alias Cannery.{Accounts.User, Containers.Container}
|
||||||
alias Ecto.Changeset
|
alias Ecto.Changeset
|
||||||
|
|
||||||
describe "containers" do
|
@moduletag :containers
|
||||||
alias Cannery.Containers.Container
|
|
||||||
|
|
||||||
|
describe "containers" do
|
||||||
@valid_attrs %{
|
@valid_attrs %{
|
||||||
"desc" => "some desc",
|
"desc" => "some desc",
|
||||||
"location" => "some location",
|
"location" => "some location",
|
||||||
@ -19,44 +24,47 @@ defmodule Cannery.ContainersTest do
|
|||||||
"name" => "some updated name",
|
"name" => "some updated name",
|
||||||
"type" => "some updated type"
|
"type" => "some updated type"
|
||||||
}
|
}
|
||||||
@invalid_attrs %{desc: nil, location: nil, name: nil, type: nil}
|
@invalid_attrs %{"desc" => nil, "location" => nil, "name" => nil, "type" => nil}
|
||||||
|
|
||||||
def container_fixture(attrs \\ %{}) do
|
@spec container_fixture(User.t(), map()) :: Container.t()
|
||||||
{:ok, container} =
|
def container_fixture(user, attrs \\ %{}) do
|
||||||
attrs
|
{:ok, container} = @valid_attrs |> Map.merge(attrs) |> Containers.create_container(user)
|
||||||
|> Enum.into(@valid_attrs)
|
|
||||||
|> Containers.create_container()
|
|
||||||
|
|
||||||
container
|
container
|
||||||
end
|
end
|
||||||
|
|
||||||
test "list_containers/0 returns all containers" do
|
test "list_containers/1 returns all containers" do
|
||||||
container = container_fixture()
|
user = user_fixture()
|
||||||
assert Containers.list_containers() == [container]
|
container = user |> container_fixture()
|
||||||
|
assert Containers.list_containers(user) == [container]
|
||||||
end
|
end
|
||||||
|
|
||||||
test "get_container!/1 returns the container with given id" do
|
test "get_container!/1 returns the container with given id" do
|
||||||
container = container_fixture()
|
container = user_fixture() |> container_fixture()
|
||||||
assert Containers.get_container!(container.id) == container
|
assert Containers.get_container!(container.id) == container
|
||||||
end
|
end
|
||||||
|
|
||||||
test "create_container/1 with valid data creates a container" do
|
test "create_container/1 with valid data creates a container" do
|
||||||
assert {:ok, %Container{} = container} = Containers.create_container(@valid_attrs)
|
user = user_fixture()
|
||||||
|
assert {:ok, %Container{} = container} = @valid_attrs |> Containers.create_container(user)
|
||||||
assert container.desc == "some desc"
|
assert container.desc == "some desc"
|
||||||
assert container.location == "some location"
|
assert container.location == "some location"
|
||||||
assert container.name == "some name"
|
assert container.name == "some name"
|
||||||
assert container.type == "some type"
|
assert container.type == "some type"
|
||||||
|
assert container.user_id == user.id
|
||||||
end
|
end
|
||||||
|
|
||||||
test "create_container/1 with invalid data returns error changeset" do
|
test "create_container/1 with invalid data returns error changeset" do
|
||||||
assert {:error, %Changeset{}} = Containers.create_container(@invalid_attrs)
|
assert {:error, %Changeset{}} =
|
||||||
|
@invalid_attrs |> Containers.create_container(user_fixture())
|
||||||
end
|
end
|
||||||
|
|
||||||
test "update_container/2 with valid data updates the container" do
|
test "update_container/2 with valid data updates the container" do
|
||||||
container = container_fixture()
|
user = user_fixture()
|
||||||
|
container = user |> container_fixture()
|
||||||
|
|
||||||
assert {:ok, %Container{} = container} =
|
assert {:ok, %Container{} = container} =
|
||||||
Containers.update_container(container, @update_attrs)
|
Containers.update_container(container, user, @update_attrs)
|
||||||
|
|
||||||
assert container.desc == "some updated desc"
|
assert container.desc == "some updated desc"
|
||||||
assert container.location == "some updated location"
|
assert container.location == "some updated location"
|
||||||
@ -65,19 +73,21 @@ defmodule Cannery.ContainersTest do
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "update_container/2 with invalid data returns error changeset" do
|
test "update_container/2 with invalid data returns error changeset" do
|
||||||
container = container_fixture()
|
user = user_fixture()
|
||||||
assert {:error, %Changeset{}} = Containers.update_container(container, @invalid_attrs)
|
container = user |> container_fixture()
|
||||||
|
assert {:error, %Changeset{}} = Containers.update_container(container, user, @invalid_attrs)
|
||||||
assert container == Containers.get_container!(container.id)
|
assert container == Containers.get_container!(container.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "delete_container/1 deletes the container" do
|
test "delete_container/1 deletes the container" do
|
||||||
container = container_fixture()
|
user = user_fixture()
|
||||||
assert {:ok, %Container{}} = Containers.delete_container(container)
|
container = user |> container_fixture()
|
||||||
|
assert {:ok, %Container{}} = Containers.delete_container(container, user)
|
||||||
assert_raise Ecto.NoResultsError, fn -> Containers.get_container!(container.id) end
|
assert_raise Ecto.NoResultsError, fn -> Containers.get_container!(container.id) end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "change_container/1 returns a container changeset" do
|
test "change_container/1 returns a container changeset" do
|
||||||
container = container_fixture()
|
container = user_fixture() |> container_fixture()
|
||||||
assert %Changeset{} = Containers.change_container(container)
|
assert %Changeset{} = Containers.change_container(container)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
defmodule CanneryWeb.ContainerLiveTest do
|
defmodule CanneryWeb.ContainerLiveTest do
|
||||||
use CanneryWeb.ConnCase
|
use CanneryWeb.ConnCase
|
||||||
|
|
||||||
import Phoenix.LiveViewTest
|
import Phoenix.LiveViewTest
|
||||||
|
import CanneryWeb.Gettext
|
||||||
alias Cannery.Containers
|
alias Cannery.Containers
|
||||||
|
alias Cannery.{Accounts.User, Containers.Container}
|
||||||
|
|
||||||
|
@moduletag :containers_live
|
||||||
|
|
||||||
@create_attrs %{
|
@create_attrs %{
|
||||||
"desc" => "some desc",
|
"desc" => "some desc",
|
||||||
@ -19,13 +21,14 @@ defmodule CanneryWeb.ContainerLiveTest do
|
|||||||
}
|
}
|
||||||
@invalid_attrs %{desc: nil, location: nil, name: nil, type: nil}
|
@invalid_attrs %{desc: nil, location: nil, name: nil, type: nil}
|
||||||
|
|
||||||
defp fixture(:container) do
|
@spec fixture(:container, User.t()) :: Container.t()
|
||||||
{:ok, container} = Containers.create_container(@create_attrs)
|
defp fixture(:container, user) do
|
||||||
|
{:ok, container} = Containers.create_container(@create_attrs, user)
|
||||||
container
|
container
|
||||||
end
|
end
|
||||||
|
|
||||||
defp create_container(_) do
|
defp create_container(%{user: user}) do
|
||||||
container = fixture(:container)
|
container = fixture(:container, user)
|
||||||
%{container: container}
|
%{container: container}
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -35,15 +38,15 @@ defmodule CanneryWeb.ContainerLiveTest do
|
|||||||
test "lists all containers", %{conn: conn, container: container} do
|
test "lists all containers", %{conn: conn, container: container} do
|
||||||
{:ok, _index_live, html} = live(conn, Routes.container_index_path(conn, :index))
|
{:ok, _index_live, html} = live(conn, Routes.container_index_path(conn, :index))
|
||||||
|
|
||||||
assert html =~ "Listing Containers"
|
assert html =~ gettext("Listing Containers")
|
||||||
assert html =~ container.desc
|
assert html =~ container.desc
|
||||||
end
|
end
|
||||||
|
|
||||||
test "saves new container", %{conn: conn} do
|
test "saves new container", %{conn: conn} do
|
||||||
{:ok, index_live, _html} = live(conn, Routes.container_index_path(conn, :index))
|
{:ok, index_live, _html} = live(conn, Routes.container_index_path(conn, :index))
|
||||||
|
|
||||||
assert index_live |> element("a", "New Container") |> render_click() =~
|
assert index_live |> element("a", gettext("New Container")) |> render_click() =~
|
||||||
"New Container"
|
gettext("New Container")
|
||||||
|
|
||||||
assert_patch(index_live, Routes.container_index_path(conn, :new))
|
assert_patch(index_live, Routes.container_index_path(conn, :new))
|
||||||
|
|
||||||
@ -57,7 +60,7 @@ defmodule CanneryWeb.ContainerLiveTest do
|
|||||||
|> render_submit()
|
|> render_submit()
|
||||||
|> follow_redirect(conn, Routes.container_index_path(conn, :index))
|
|> follow_redirect(conn, Routes.container_index_path(conn, :index))
|
||||||
|
|
||||||
assert html =~ "Container created successfully"
|
assert html =~ gettext("Container created successfully")
|
||||||
assert html =~ "some desc"
|
assert html =~ "some desc"
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -65,7 +68,7 @@ defmodule CanneryWeb.ContainerLiveTest do
|
|||||||
{:ok, index_live, _html} = live(conn, Routes.container_index_path(conn, :index))
|
{:ok, index_live, _html} = live(conn, Routes.container_index_path(conn, :index))
|
||||||
|
|
||||||
assert index_live |> element("#container-#{container.id} a", "Edit") |> render_click() =~
|
assert index_live |> element("#container-#{container.id} a", "Edit") |> render_click() =~
|
||||||
"Edit Container"
|
gettext("Edit Container")
|
||||||
|
|
||||||
assert_patch(index_live, Routes.container_index_path(conn, :edit, container))
|
assert_patch(index_live, Routes.container_index_path(conn, :edit, container))
|
||||||
|
|
||||||
@ -79,7 +82,7 @@ defmodule CanneryWeb.ContainerLiveTest do
|
|||||||
|> render_submit()
|
|> render_submit()
|
||||||
|> follow_redirect(conn, Routes.container_index_path(conn, :index))
|
|> follow_redirect(conn, Routes.container_index_path(conn, :index))
|
||||||
|
|
||||||
assert html =~ "Container updated successfully"
|
assert html =~ gettext("Container updated successfully")
|
||||||
assert html =~ "some updated desc"
|
assert html =~ "some updated desc"
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -97,7 +100,7 @@ defmodule CanneryWeb.ContainerLiveTest do
|
|||||||
test "displays container", %{conn: conn, container: container} do
|
test "displays container", %{conn: conn, container: container} do
|
||||||
{:ok, _show_live, html} = live(conn, Routes.container_show_path(conn, :show, container))
|
{:ok, _show_live, html} = live(conn, Routes.container_show_path(conn, :show, container))
|
||||||
|
|
||||||
assert html =~ "Show Container"
|
assert html =~ gettext("Show Container")
|
||||||
assert html =~ container.desc
|
assert html =~ container.desc
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -105,7 +108,7 @@ defmodule CanneryWeb.ContainerLiveTest do
|
|||||||
{:ok, show_live, _html} = live(conn, Routes.container_show_path(conn, :show, container))
|
{:ok, show_live, _html} = live(conn, Routes.container_show_path(conn, :show, container))
|
||||||
|
|
||||||
assert show_live |> element("a", "Edit") |> render_click() =~
|
assert show_live |> element("a", "Edit") |> render_click() =~
|
||||||
"Edit Container"
|
gettext("Edit Container")
|
||||||
|
|
||||||
assert_patch(show_live, Routes.container_show_path(conn, :edit, container))
|
assert_patch(show_live, Routes.container_show_path(conn, :edit, container))
|
||||||
|
|
||||||
@ -119,7 +122,7 @@ defmodule CanneryWeb.ContainerLiveTest do
|
|||||||
|> render_submit()
|
|> render_submit()
|
||||||
|> follow_redirect(conn, Routes.container_show_path(conn, :show, container))
|
|> follow_redirect(conn, Routes.container_show_path(conn, :show, container))
|
||||||
|
|
||||||
assert html =~ "Container updated successfully"
|
assert html =~ gettext("Container updated successfully")
|
||||||
assert html =~ "some updated desc"
|
assert html =~ "some updated desc"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -25,6 +25,7 @@ defmodule Cannery.DataCase do
|
|||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
import Cannery.DataCase
|
import Cannery.DataCase
|
||||||
|
import Cannery.AccountsFixtures
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -15,8 +15,8 @@ defmodule Cannery.AccountsFixtures do
|
|||||||
{:ok, user} =
|
{:ok, user} =
|
||||||
attrs
|
attrs
|
||||||
|> Enum.into(%{
|
|> Enum.into(%{
|
||||||
email: unique_user_email(),
|
"email" => unique_user_email(),
|
||||||
password: valid_user_password()
|
"password" => valid_user_password()
|
||||||
})
|
})
|
||||||
|> Accounts.register_user()
|
|> Accounts.register_user()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user