diff --git a/lib/idea_inbox/ideas/idea.ex b/lib/idea_inbox/ideas/idea.ex new file mode 100644 index 0000000..4f89014 --- /dev/null +++ b/lib/idea_inbox/ideas/idea.ex @@ -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 diff --git a/lib/idea_inbox/ideas/ideas.ex b/lib/idea_inbox/ideas/ideas.ex new file mode 100644 index 0000000..41d5c37 --- /dev/null +++ b/lib/idea_inbox/ideas/ideas.ex @@ -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 diff --git a/priv/repo/migrations/20181007014738_create_ideas.exs b/priv/repo/migrations/20181007014738_create_ideas.exs new file mode 100644 index 0000000..f53442b --- /dev/null +++ b/priv/repo/migrations/20181007014738_create_ideas.exs @@ -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 diff --git a/test/idea_inbox/ideas/ideas_test.exs b/test/idea_inbox/ideas/ideas_test.exs new file mode 100644 index 0000000..869ecb5 --- /dev/null +++ b/test/idea_inbox/ideas/ideas_test.exs @@ -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