From cddb4d3ae3578477b5b8f7a0b74dab290f856679 Mon Sep 17 00:00:00 2001 From: "Liam P. White" Date: Wed, 28 Aug 2019 18:33:13 -0400 Subject: [PATCH] context: user_links --- lib/philomena/users.ex | 96 +++++++++++++++++++++++++++++++++++ lib/philomena/users/link.ex | 29 +++++++++++ test/philomena/users_test.exs | 57 +++++++++++++++++++++ 3 files changed, 182 insertions(+) create mode 100644 lib/philomena/users/link.ex diff --git a/lib/philomena/users.ex b/lib/philomena/users.ex index a0410fb8..0eeea855 100644 --- a/lib/philomena/users.ex +++ b/lib/philomena/users.ex @@ -293,4 +293,100 @@ defmodule Philomena.Users do def change_fingerprints(%Fingerprint{} = fingerprints) do Fingerprint.changeset(fingerprints, %{}) end + + alias Philomena.Users.Link + + @doc """ + Returns the list of user_links. + + ## Examples + + iex> list_user_links() + [%Link{}, ...] + + """ + def list_user_links do + Repo.all(Link) + end + + @doc """ + Gets a single link. + + Raises `Ecto.NoResultsError` if the Link does not exist. + + ## Examples + + iex> get_link!(123) + %Link{} + + iex> get_link!(456) + ** (Ecto.NoResultsError) + + """ + def get_link!(id), do: Repo.get!(Link, id) + + @doc """ + Creates a link. + + ## Examples + + iex> create_link(%{field: value}) + {:ok, %Link{}} + + iex> create_link(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_link(attrs \\ %{}) do + %Link{} + |> Link.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a link. + + ## Examples + + iex> update_link(link, %{field: new_value}) + {:ok, %Link{}} + + iex> update_link(link, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_link(%Link{} = link, attrs) do + link + |> Link.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a Link. + + ## Examples + + iex> delete_link(link) + {:ok, %Link{}} + + iex> delete_link(link) + {:error, %Ecto.Changeset{}} + + """ + def delete_link(%Link{} = link) do + Repo.delete(link) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking link changes. + + ## Examples + + iex> change_link(link) + %Ecto.Changeset{source: %Link{}} + + """ + def change_link(%Link{} = link) do + Link.changeset(link, %{}) + end end diff --git a/lib/philomena/users/link.ex b/lib/philomena/users/link.ex new file mode 100644 index 00000000..f5b5c6f3 --- /dev/null +++ b/lib/philomena/users/link.ex @@ -0,0 +1,29 @@ +defmodule Philomena.Users.Link do + use Ecto.Schema + import Ecto.Changeset + + schema "user_links" do + belongs_to :user, Philomena.Users.User + belongs_to :verified_by_user, Philomena.Users.User + belongs_to :contacted_by_user, Philomena.Users.User + belongs_to :tag, Philomena.Tags.Tag + + field :aasm_state, :string + field :uri, :string + field :hostname, :string + field :path, :string + field :verification_code, :string + field :public, :boolean, default: true + field :next_check_at, :naive_datetime + field :contacted_at, :naive_datetime + + timestamps(inserted_at: :created_at) + end + + @doc false + def changeset(link, attrs) do + link + |> cast(attrs, []) + |> validate_required([]) + end +end diff --git a/test/philomena/users_test.exs b/test/philomena/users_test.exs index b5cbcfa4..15dbe95b 100644 --- a/test/philomena/users_test.exs +++ b/test/philomena/users_test.exs @@ -116,4 +116,61 @@ defmodule Philomena.UsersTest do assert %Ecto.Changeset{} = Users.change_fingerprints(fingerprints) end end + + describe "user_links" do + alias Philomena.Users.Link + + @valid_attrs %{} + @update_attrs %{} + @invalid_attrs %{} + + def link_fixture(attrs \\ %{}) do + {:ok, link} = + attrs + |> Enum.into(@valid_attrs) + |> Users.create_link() + + link + end + + test "list_user_links/0 returns all user_links" do + link = link_fixture() + assert Users.list_user_links() == [link] + end + + test "get_link!/1 returns the link with given id" do + link = link_fixture() + assert Users.get_link!(link.id) == link + end + + test "create_link/1 with valid data creates a link" do + assert {:ok, %Link{} = link} = Users.create_link(@valid_attrs) + end + + test "create_link/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Users.create_link(@invalid_attrs) + end + + test "update_link/2 with valid data updates the link" do + link = link_fixture() + assert {:ok, %Link{} = link} = Users.update_link(link, @update_attrs) + end + + test "update_link/2 with invalid data returns error changeset" do + link = link_fixture() + assert {:error, %Ecto.Changeset{}} = Users.update_link(link, @invalid_attrs) + assert link == Users.get_link!(link.id) + end + + test "delete_link/1 deletes the link" do + link = link_fixture() + assert {:ok, %Link{}} = Users.delete_link(link) + assert_raise Ecto.NoResultsError, fn -> Users.get_link!(link.id) end + end + + test "change_link/1 returns a link changeset" do + link = link_fixture() + assert %Ecto.Changeset{} = Users.change_link(link) + end + end end