From b7824c4abcf4401f235d7c69d5adb4ac3c0be7f9 Mon Sep 17 00:00:00 2001 From: "Liam P. White" Date: Wed, 28 Aug 2019 12:48:39 -0400 Subject: [PATCH] context: conversations --- lib/philomena/conversations.ex | 104 ++++++++++++++++++++ lib/philomena/conversations/conversation.ex | 26 +++++ test/philomena/conversations_test.exs | 62 ++++++++++++ 3 files changed, 192 insertions(+) create mode 100644 lib/philomena/conversations.ex create mode 100644 lib/philomena/conversations/conversation.ex create mode 100644 test/philomena/conversations_test.exs diff --git a/lib/philomena/conversations.ex b/lib/philomena/conversations.ex new file mode 100644 index 00000000..51ce0dc8 --- /dev/null +++ b/lib/philomena/conversations.ex @@ -0,0 +1,104 @@ +defmodule Philomena.Conversations do + @moduledoc """ + The Conversations context. + """ + + import Ecto.Query, warn: false + alias Philomena.Repo + + alias Philomena.Conversations.Conversation + + @doc """ + Returns the list of conversations. + + ## Examples + + iex> list_conversations() + [%Conversation{}, ...] + + """ + def list_conversations do + Repo.all(Conversation) + end + + @doc """ + Gets a single conversation. + + Raises `Ecto.NoResultsError` if the Conversation does not exist. + + ## Examples + + iex> get_conversation!(123) + %Conversation{} + + iex> get_conversation!(456) + ** (Ecto.NoResultsError) + + """ + def get_conversation!(id), do: Repo.get!(Conversation, id) + + @doc """ + Creates a conversation. + + ## Examples + + iex> create_conversation(%{field: value}) + {:ok, %Conversation{}} + + iex> create_conversation(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_conversation(attrs \\ %{}) do + %Conversation{} + |> Conversation.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a conversation. + + ## Examples + + iex> update_conversation(conversation, %{field: new_value}) + {:ok, %Conversation{}} + + iex> update_conversation(conversation, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_conversation(%Conversation{} = conversation, attrs) do + conversation + |> Conversation.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a Conversation. + + ## Examples + + iex> delete_conversation(conversation) + {:ok, %Conversation{}} + + iex> delete_conversation(conversation) + {:error, %Ecto.Changeset{}} + + """ + def delete_conversation(%Conversation{} = conversation) do + Repo.delete(conversation) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking conversation changes. + + ## Examples + + iex> change_conversation(conversation) + %Ecto.Changeset{source: %Conversation{}} + + """ + def change_conversation(%Conversation{} = conversation) do + Conversation.changeset(conversation, %{}) + end +end diff --git a/lib/philomena/conversations/conversation.ex b/lib/philomena/conversations/conversation.ex new file mode 100644 index 00000000..7db30650 --- /dev/null +++ b/lib/philomena/conversations/conversation.ex @@ -0,0 +1,26 @@ +defmodule Philomena.Conversations.Conversation do + use Ecto.Schema + import Ecto.Changeset + + schema "conversations" do + belongs_to :from, Philomena.Users.User + belongs_to :to, Philomena.Users.User + + field :title, :string + field :to_read, :boolean, default: false + field :from_read, :boolean, default: true + field :to_hidden, :boolean, default: false + field :from_hidden, :boolean, default: false + field :slug, :string + field :last_message_at, :naive_datetime + + timestamps(inserted_at: :created_at) + end + + @doc false + def changeset(conversation, attrs) do + conversation + |> cast(attrs, []) + |> validate_required([]) + end +end diff --git a/test/philomena/conversations_test.exs b/test/philomena/conversations_test.exs new file mode 100644 index 00000000..94357e2a --- /dev/null +++ b/test/philomena/conversations_test.exs @@ -0,0 +1,62 @@ +defmodule Philomena.ConversationsTest do + use Philomena.DataCase + + alias Philomena.Conversations + + describe "conversations" do + alias Philomena.Conversations.Conversation + + @valid_attrs %{} + @update_attrs %{} + @invalid_attrs %{} + + def conversation_fixture(attrs \\ %{}) do + {:ok, conversation} = + attrs + |> Enum.into(@valid_attrs) + |> Conversations.create_conversation() + + conversation + end + + test "list_conversations/0 returns all conversations" do + conversation = conversation_fixture() + assert Conversations.list_conversations() == [conversation] + end + + test "get_conversation!/1 returns the conversation with given id" do + conversation = conversation_fixture() + assert Conversations.get_conversation!(conversation.id) == conversation + end + + test "create_conversation/1 with valid data creates a conversation" do + assert {:ok, %Conversation{} = conversation} = Conversations.create_conversation(@valid_attrs) + end + + test "create_conversation/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Conversations.create_conversation(@invalid_attrs) + end + + test "update_conversation/2 with valid data updates the conversation" do + conversation = conversation_fixture() + assert {:ok, %Conversation{} = conversation} = Conversations.update_conversation(conversation, @update_attrs) + end + + test "update_conversation/2 with invalid data returns error changeset" do + conversation = conversation_fixture() + assert {:error, %Ecto.Changeset{}} = Conversations.update_conversation(conversation, @invalid_attrs) + assert conversation == Conversations.get_conversation!(conversation.id) + end + + test "delete_conversation/1 deletes the conversation" do + conversation = conversation_fixture() + assert {:ok, %Conversation{}} = Conversations.delete_conversation(conversation) + assert_raise Ecto.NoResultsError, fn -> Conversations.get_conversation!(conversation.id) end + end + + test "change_conversation/1 returns a conversation changeset" do + conversation = conversation_fixture() + assert %Ecto.Changeset{} = Conversations.change_conversation(conversation) + end + end +end