-
Notifications
You must be signed in to change notification settings - Fork 0
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
4 changed files
with
209 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,20 @@ | ||
defmodule IdeaInbox.Ideas.Idea do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
|
||
schema "ideas" do | ||
field :title, :string | ||
field :up_votes, :integer, default: 0 | ||
field :down_votes, :integer, default: 0 | ||
|
||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(idea, attrs) do | ||
idea | ||
|> cast(attrs, [:title]) | ||
|> validate_required([:title]) | ||
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,104 @@ | ||
defmodule IdeaInbox.Ideas do | ||
@moduledoc """ | ||
The Ideas context. | ||
""" | ||
|
||
import Ecto.Query, warn: false | ||
alias IdeaInbox.Repo | ||
|
||
alias IdeaInbox.Ideas.Idea | ||
|
||
@doc """ | ||
Returns the list of ideas. | ||
## Examples | ||
iex> list_ideas() | ||
[%Idea{}, ...] | ||
""" | ||
def list_ideas do | ||
Repo.all(Idea) | ||
end | ||
|
||
@doc """ | ||
Gets a single idea. | ||
Raises `Ecto.NoResultsError` if the Idea does not exist. | ||
## Examples | ||
iex> get_idea!(123) | ||
%Idea{} | ||
iex> get_idea!(456) | ||
** (Ecto.NoResultsError) | ||
""" | ||
def get_idea!(id), do: Repo.get!(Idea, id) | ||
|
||
@doc """ | ||
Creates a idea. | ||
## Examples | ||
iex> create_idea(%{field: value}) | ||
{:ok, %Idea{}} | ||
iex> create_idea(%{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def create_idea(attrs \\ %{}) do | ||
%Idea{} | ||
|> Idea.changeset(attrs) | ||
|> Repo.insert() | ||
end | ||
|
||
@doc """ | ||
Updates a idea. | ||
## Examples | ||
iex> update_idea(idea, %{field: new_value}) | ||
{:ok, %Idea{}} | ||
iex> update_idea(idea, %{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def update_idea(%Idea{} = idea, attrs) do | ||
idea | ||
|> Idea.changeset(attrs) | ||
|> Repo.update() | ||
end | ||
|
||
@doc """ | ||
Deletes a Idea. | ||
## Examples | ||
iex> delete_idea(idea) | ||
{:ok, %Idea{}} | ||
iex> delete_idea(idea) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def delete_idea(%Idea{} = idea) do | ||
Repo.delete(idea) | ||
end | ||
|
||
@doc """ | ||
Returns an `%Ecto.Changeset{}` for tracking idea changes. | ||
## Examples | ||
iex> change_idea(idea) | ||
%Ecto.Changeset{source: %Idea{}} | ||
""" | ||
def change_idea(%Idea{} = idea) do | ||
Idea.changeset(idea, %{}) | ||
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,14 @@ | ||
defmodule IdeaInbox.Repo.Migrations.CreateIdeas do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:ideas) do | ||
add :title, :string | ||
add :up_votes, :integer | ||
add :down_votes, :integer | ||
|
||
timestamps() | ||
end | ||
|
||
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,71 @@ | ||
defmodule IdeaInbox.IdeasTest do | ||
use IdeaInbox.DataCase | ||
|
||
alias IdeaInbox.Ideas | ||
|
||
describe "ideas" do | ||
alias IdeaInbox.Ideas.Idea | ||
|
||
@valid_attrs %{down_votes: 42, timestamps: "some timestamps", title: "some title", up_votes: 42} | ||
@update_attrs %{down_votes: 43, timestamps: "some updated timestamps", title: "some updated title", up_votes: 43} | ||
@invalid_attrs %{down_votes: nil, timestamps: nil, title: nil, up_votes: nil} | ||
|
||
def idea_fixture(attrs \\ %{}) do | ||
{:ok, idea} = | ||
attrs | ||
|> Enum.into(@valid_attrs) | ||
|> Ideas.create_idea() | ||
|
||
idea | ||
end | ||
|
||
test "list_ideas/0 returns all ideas" do | ||
idea = idea_fixture() | ||
assert Ideas.list_ideas() == [idea] | ||
end | ||
|
||
test "get_idea!/1 returns the idea with given id" do | ||
idea = idea_fixture() | ||
assert Ideas.get_idea!(idea.id) == idea | ||
end | ||
|
||
test "create_idea/1 with valid data creates a idea" do | ||
assert {:ok, %Idea{} = idea} = Ideas.create_idea(@valid_attrs) | ||
assert idea.down_votes == 42 | ||
assert idea.timestamps == "some timestamps" | ||
assert idea.title == "some title" | ||
assert idea.up_votes == 42 | ||
end | ||
|
||
test "create_idea/1 with invalid data returns error changeset" do | ||
assert {:error, %Ecto.Changeset{}} = Ideas.create_idea(@invalid_attrs) | ||
end | ||
|
||
test "update_idea/2 with valid data updates the idea" do | ||
idea = idea_fixture() | ||
assert {:ok, idea} = Ideas.update_idea(idea, @update_attrs) | ||
assert %Idea{} = idea | ||
assert idea.down_votes == 43 | ||
assert idea.timestamps == "some updated timestamps" | ||
assert idea.title == "some updated title" | ||
assert idea.up_votes == 43 | ||
end | ||
|
||
test "update_idea/2 with invalid data returns error changeset" do | ||
idea = idea_fixture() | ||
assert {:error, %Ecto.Changeset{}} = Ideas.update_idea(idea, @invalid_attrs) | ||
assert idea == Ideas.get_idea!(idea.id) | ||
end | ||
|
||
test "delete_idea/1 deletes the idea" do | ||
idea = idea_fixture() | ||
assert {:ok, %Idea{}} = Ideas.delete_idea(idea) | ||
assert_raise Ecto.NoResultsError, fn -> Ideas.get_idea!(idea.id) end | ||
end | ||
|
||
test "change_idea/1 returns a idea changeset" do | ||
idea = idea_fixture() | ||
assert %Ecto.Changeset{} = Ideas.change_idea(idea) | ||
end | ||
end | ||
end |