From 0c883cea64751e639c65e4a42e2ee57b1a10c040 Mon Sep 17 00:00:00 2001 From: nelsonic Date: Fri, 2 Oct 2020 09:40:06 +0100 Subject: [PATCH] Let's try that again ... #65 --- lib/app/ctx/item.ex | 20 --------- lib/app/item.ex | 26 ++++++------ .../controllers/captures_controller.ex | 8 ++-- .../controllers/categorise_controller.ex | 4 +- lib/app_web/controllers/item_controller.ex | 26 ++++++------ test/app/ctx_test.exs | 42 ------------------- test/app/item_test.exs | 3 ++ .../controllers/item_controller_test.exs | 4 +- test/support/setup_helpers.ex | 2 +- 9 files changed, 35 insertions(+), 100 deletions(-) delete mode 100644 lib/app/ctx/item.ex diff --git a/lib/app/ctx/item.ex b/lib/app/ctx/item.ex deleted file mode 100644 index 1312a782..00000000 --- a/lib/app/ctx/item.ex +++ /dev/null @@ -1,20 +0,0 @@ -defmodule App.Ctx.Item do - use Ecto.Schema - import Ecto.Changeset - - schema "items" do - field :text, :string - # an item always belongs to a person - field :person_id, :id - field :status, :id - many_to_many :tags, App.Ctx.Tag, join_through: "item_tags" - timestamps() - end - - @doc false - def changeset(item, attrs) do - item - |> cast(attrs, [:text]) - |> validate_required([:text]) - end -end diff --git a/lib/app/item.ex b/lib/app/item.ex index 57d9229c..bb87d17c 100644 --- a/lib/app/item.ex +++ b/lib/app/item.ex @@ -87,31 +87,31 @@ defmodule App.Item do end @doc """ - Deletes a Item. + Returns an `%Ecto.Changeset{}` for tracking item changes. ## Examples - iex> delete_item(item) - {:ok, %Item{}} - - iex> delete_item(item) - {:error, %Ecto.Changeset{}} + iex> change_item(item) + %Ecto.Changeset{source: %Item{}} """ - def delete_item(%Item{} = item) do - Repo.delete(item) + def change_item(%Item{} = item) do + Item.changeset(item, %{}) end @doc """ - Returns an `%Ecto.Changeset{}` for tracking item changes. + Deletes a Item. ## Examples - iex> change_item(item) - %Ecto.Changeset{source: %Item{}} + iex> delete_item(item) + {:ok, %Item{}} + + iex> delete_item(item) + {:error, %Ecto.Changeset{}} """ - def change_item(%Item{} = item) do - Item.changeset(item, %{}) + def delete_item(%Item{} = item) do + Repo.delete(item) end end diff --git a/lib/app_web/controllers/captures_controller.ex b/lib/app_web/controllers/captures_controller.ex index 22a3f937..a4cacf09 100644 --- a/lib/app_web/controllers/captures_controller.ex +++ b/lib/app_web/controllers/captures_controller.ex @@ -1,16 +1,14 @@ defmodule AppWeb.CaptureController do use AppWeb, :controller - - alias App.Ctx - alias App.Ctx.Item + alias App.Item def new(conn, _params) do - changeset = Ctx.change_item(%Item{}) + changeset = Item.change_item(%Item{}) render(conn, "new.html", changeset: changeset) end def create(conn, %{"item" => capture_params}) do - case Ctx.create_item(capture_params) do + case Item.create_item(capture_params) do {:ok, _item} -> conn |> put_flash(:info, "Item created successfully.") diff --git a/lib/app_web/controllers/categorise_controller.ex b/lib/app_web/controllers/categorise_controller.ex index ba3d4249..3458e89f 100644 --- a/lib/app_web/controllers/categorise_controller.ex +++ b/lib/app_web/controllers/categorise_controller.ex @@ -1,10 +1,8 @@ defmodule AppWeb.CategoriseController do use AppWeb, :controller - alias App.Ctx - def index(conn, _) do - captures = Ctx.list_items() + captures = App.Item.list_items() render(conn, "index.html", captures: captures) end end diff --git a/lib/app_web/controllers/item_controller.ex b/lib/app_web/controllers/item_controller.ex index 918cba13..2a7a1cb7 100644 --- a/lib/app_web/controllers/item_controller.ex +++ b/lib/app_web/controllers/item_controller.ex @@ -1,26 +1,24 @@ defmodule AppWeb.ItemController do use AppWeb, :controller - - alias App.Ctx - alias App.Ctx.Item + alias App.Item def index(conn, _params) do - items = Ctx.list_items() + items = Item.list_items() render(conn, "index.html", items: items) end def api_index(conn, _params) do - items = Ctx.list_items() + items = Item.list_items() render(conn, "index.json", items: items) end def new(conn, _params) do - changeset = Ctx.change_item(%Item{}) + changeset = Item.change_item(%Item{}) render(conn, "new.html", changeset: changeset) end def create(conn, %{"item" => item_params}) do - case Ctx.create_item(item_params) do + case Item.create_item(item_params) do {:ok, item} -> conn |> put_flash(:info, "Item created successfully.") @@ -32,20 +30,20 @@ defmodule AppWeb.ItemController do end def show(conn, %{"id" => id}) do - item = Ctx.get_item!(id) + item = Item.get_item!(id) render(conn, "show.html", item: item) end def edit(conn, %{"id" => id}) do - item = Ctx.get_item!(id) - changeset = Ctx.change_item(item) + item = Item.get_item!(id) + changeset = Item.change_item(item) render(conn, "edit.html", item: item, changeset: changeset) end def update(conn, %{"id" => id, "item" => item_params}) do - item = Ctx.get_item!(id) + item = Item.get_item!(id) - case Ctx.update_item(item, item_params) do + case Item.update_item(item, item_params) do {:ok, item} -> conn |> put_flash(:info, "Item updated successfully.") @@ -57,8 +55,8 @@ defmodule AppWeb.ItemController do end def delete(conn, %{"id" => id}) do - item = Ctx.get_item!(id) - {:ok, _item} = Ctx.delete_item(item) + item = Item.get_item!(id) + {:ok, _item} = Item.delete_item(item) conn |> put_flash(:info, "Item deleted successfully.") diff --git a/test/app/ctx_test.exs b/test/app/ctx_test.exs index 4f1260b9..562395b8 100644 --- a/test/app/ctx_test.exs +++ b/test/app/ctx_test.exs @@ -127,48 +127,6 @@ defmodule App.CtxTest do end end - describe "items" do - alias App.Ctx.{Item, List} - - @valid_attrs %{text: "some text"} - @update_attrs %{text: "some updated text"} - @invalid_attrs %{text: nil} - - def item_fixture(attrs \\ %{}) do - {:ok, item} = - attrs - |> Enum.into(@valid_attrs) - |> Ctx.create_item() - - item - end - - test "get_item!/1 returns the item with given id" do - item = item_fixture(@valid_attrs) - assert Ctx.get_item!(item.id) == item - end - - test "create_item/1 with valid data creates a item" do - assert {:ok, %Item{} = item} = Ctx.create_item(@valid_attrs) - assert item.text == "some text" - end - - test "create_item/1 with invalid data returns error changeset" do - assert {:error, %Ecto.Changeset{}} = Ctx.create_item(@invalid_attrs) - end - - test "update_item/2 with valid data updates the item" do - item = item_fixture() - assert {:ok, %Item{} = item} = Ctx.update_item(item, @update_attrs) - assert item.text == "some updated text" - end - - test "change_item/1 returns a item changeset" do - item = item_fixture() - assert %Ecto.Changeset{} = Ctx.change_item(item) - end - end - describe "lists" do alias App.Ctx.List diff --git a/test/app/item_test.exs b/test/app/item_test.exs index 3277cfb2..96328e78 100644 --- a/test/app/item_test.exs +++ b/test/app/item_test.exs @@ -25,6 +25,9 @@ defmodule App.ItemTest do test "create_item/1 with valid data creates a item" do assert {:ok, %Item{} = item} = Item.create_item(@valid_attrs) assert item.text == "some text" + + inserted_item = List.first(Item.list_items()) + assert inserted_item.text == @valid_attrs.text end test "create_item/1 with invalid data returns error changeset" do diff --git a/test/app_web/controllers/item_controller_test.exs b/test/app_web/controllers/item_controller_test.exs index 21c87cac..f21fc572 100644 --- a/test/app_web/controllers/item_controller_test.exs +++ b/test/app_web/controllers/item_controller_test.exs @@ -1,14 +1,14 @@ defmodule AppWeb.ItemControllerTest do use AppWeb.ConnCase import App.SetupHelpers - alias App.Ctx + alias App.Item @create_attrs %{text: "some text"} @update_attrs %{text: "some updated text"} @invalid_attrs %{text: nil} def fixture(:item) do - {:ok, item} = Ctx.create_item(@create_attrs) + {:ok, item} = Item.create_item(@create_attrs) item end diff --git a/test/support/setup_helpers.ex b/test/support/setup_helpers.ex index ece2b2ef..e85378e1 100644 --- a/test/support/setup_helpers.ex +++ b/test/support/setup_helpers.ex @@ -1,5 +1,5 @@ defmodule App.SetupHelpers do - import Phoenix.ConnTest + # import Phoenix.ConnTest def person_login(_) do AppTest.person_login()