From 020b6621fd506d9da9a34eb455fa943fbb334716 Mon Sep 17 00:00:00 2001 From: Bryan Paxton Date: Wed, 29 Nov 2017 23:08:27 -0600 Subject: [PATCH] Add projects context module - Updated "changesets"/"with valid attributes" to use Projects.change_project --- lib/code_corps/projects/projects.ex | 104 +++++++++++++++++++++ test/lib/code_corps/model/project_test.exs | 4 +- 2 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 lib/code_corps/projects/projects.ex diff --git a/lib/code_corps/projects/projects.ex b/lib/code_corps/projects/projects.ex new file mode 100644 index 000000000..5790eebdf --- /dev/null +++ b/lib/code_corps/projects/projects.ex @@ -0,0 +1,104 @@ +defmodule CodeCorps.Projects do + @moduledoc """ + The Projects context. + """ + + import Ecto.Query, warn: false + alias CodeCorps.Repo + + alias CodeCorps.Project + + @doc """ + Returns the list of projects. + + ## Examples + + iex> list_projects() + [%Project{}, ...] + + """ + def list_projects do + Repo.all(Project) + end + + @doc """ + Gets a single project. + + Raises `Ecto.NoResultsError` if the Project does not exist. + + ## Examples + + iex> get_project!(123) + %Project{} + + iex> get_project!(456) + ** (Ecto.NoResultsError) + + """ + def get_project!(id), do: Repo.get!(Project, id) + + @doc """ + Creates a project. + + ## Examples + + iex> create_project(%{field: value}) + {:ok, %Project{}} + + iex> create_project(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_project(attrs \\ %{}) do + %Project{} + |> Project.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a project. + + ## Examples + + iex> update_project(project, %{field: new_value}) + {:ok, %Project{}} + + iex> update_project(project, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_project(%Project{} = project, attrs) do + project + |> Project.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a Project. + + ## Examples + + iex> delete_project(project) + {:ok, %Project{}} + + iex> delete_project(project) + {:error, %Ecto.Changeset{}} + + """ + def delete_project(%Project{} = project) do + Repo.delete(project) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking project changes. + + ## Examples + + iex> change_project(project) + %Ecto.Changeset{source: %Project{}} + + """ + def change_project(%Project{} = project) do + Project.changeset(project, %{}) + end +end diff --git a/test/lib/code_corps/model/project_test.exs b/test/lib/code_corps/model/project_test.exs index d66871bce..b8c5c8c41 100644 --- a/test/lib/code_corps/model/project_test.exs +++ b/test/lib/code_corps/model/project_test.exs @@ -3,14 +3,14 @@ defmodule CodeCorps.ProjectTest do import CodeCorps.Project - alias CodeCorps.{Project, ProjectUser, Repo} + alias CodeCorps.{Projects, Project, ProjectUser, Repo} describe "changeset" do @valid_attrs %{title: "A title"} @invalid_attrs %{} test "with valid attributes" do - changeset = Project.changeset(%Project{}, @valid_attrs) + changeset = Projects.change_project(%Project{title: "A title"}) assert changeset.valid? end