Skip to content

Commit

Permalink
Let's try that again ... #65
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Oct 2, 2020
1 parent 9495195 commit 0c883ce
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 100 deletions.
20 changes: 0 additions & 20 deletions lib/app/ctx/item.ex

This file was deleted.

26 changes: 13 additions & 13 deletions lib/app/item.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 3 additions & 5 deletions lib/app_web/controllers/captures_controller.ex
Original file line number Diff line number Diff line change
@@ -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.")
Expand Down
4 changes: 1 addition & 3 deletions lib/app_web/controllers/categorise_controller.ex
Original file line number Diff line number Diff line change
@@ -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
26 changes: 12 additions & 14 deletions lib/app_web/controllers/item_controller.ex
Original file line number Diff line number Diff line change
@@ -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.")
Expand All @@ -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.")
Expand All @@ -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.")
Expand Down
42 changes: 0 additions & 42 deletions test/app/ctx_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions test/app/item_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/app_web/controllers/item_controller_test.exs
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/support/setup_helpers.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule App.SetupHelpers do
import Phoenix.ConnTest
# import Phoenix.ConnTest

def person_login(_) do
AppTest.person_login()
Expand Down

0 comments on commit 0c883ce

Please sign in to comment.