-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
defmodule App.Item do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
import Ecto.Query, warn: false | ||
alias App.Repo | ||
alias __MODULE__ | ||
|
||
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 | ||
|
||
@doc """ | ||
Returns the list of items. | ||
## Examples | ||
iex> list_items() | ||
[%Item{}, ...] | ||
""" | ||
def list_items do | ||
Repo.all(Item) | ||
end | ||
|
||
@doc """ | ||
Gets a single item. | ||
Raises `Ecto.NoResultsError` if the Item does not exist. | ||
## Examples | ||
iex> get_item!(123) | ||
%Item{} | ||
iex> get_item!(456) | ||
** (Ecto.NoResultsError) | ||
""" | ||
def get_item!(id), do: Repo.get!(Item, id) | ||
|
||
@doc """ | ||
Creates a item. | ||
## Examples | ||
iex> create_item(%{field: value}) | ||
{:ok, %Item{}} | ||
iex> create_item(%{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def create_item(attrs \\ %{}) do | ||
%Item{} | ||
|> Item.changeset(attrs) | ||
|> Repo.insert() | ||
end | ||
|
||
@doc """ | ||
Updates a item. | ||
## Examples | ||
iex> update_item(item, %{field: new_value}) | ||
{:ok, %Item{}} | ||
iex> update_item(item, %{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def update_item(%Item{} = item, attrs) do | ||
item | ||
|> Item.changeset(attrs) | ||
|> Repo.update() | ||
end | ||
|
||
@doc """ | ||
Deletes a Item. | ||
## Examples | ||
iex> delete_item(item) | ||
{:ok, %Item{}} | ||
iex> delete_item(item) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def delete_item(%Item{} = item) do | ||
Repo.delete(item) | ||
end | ||
|
||
@doc """ | ||
Returns an `%Ecto.Changeset{}` for tracking item changes. | ||
## Examples | ||
iex> change_item(item) | ||
%Ecto.Changeset{source: %Item{}} | ||
""" | ||
def change_item(%Item{} = item) do | ||
Item.changeset(item, %{}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
defmodule App.ItemTest do | ||
use App.DataCase | ||
|
||
alias App.Item | ||
|
||
describe "items" do | ||
@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) | ||
|> Item.create_item() | ||
|
||
item | ||
end | ||
|
||
test "get_item!/1 returns the item with given id" do | ||
item = item_fixture(@valid_attrs) | ||
assert Item.get_item!(item.id) == item | ||
end | ||
|
||
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" | ||
end | ||
|
||
test "create_item/1 with invalid data returns error changeset" do | ||
assert {:error, %Ecto.Changeset{}} = Item.create_item(@invalid_attrs) | ||
end | ||
|
||
test "update_item/2 with valid data updates the item" do | ||
item = item_fixture() | ||
assert {:ok, %Item{} = item} = Item.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{} = Item.change_item(item) | ||
end | ||
end | ||
end |