From 479783cb7dd70a6680a13fc6abdefadc22757f9d Mon Sep 17 00:00:00 2001 From: "Liam P. White" Date: Wed, 28 Aug 2019 13:21:17 -0400 Subject: [PATCH] context: messages --- lib/philomena/conversations.ex | 96 ++++++++++++++++++++++++++ lib/philomena/conversations/message.ex | 20 ++++++ test/philomena/conversations_test.exs | 57 +++++++++++++++ 3 files changed, 173 insertions(+) create mode 100644 lib/philomena/conversations/message.ex diff --git a/lib/philomena/conversations.ex b/lib/philomena/conversations.ex index 51ce0dc8..8bca0edb 100644 --- a/lib/philomena/conversations.ex +++ b/lib/philomena/conversations.ex @@ -101,4 +101,100 @@ defmodule Philomena.Conversations do def change_conversation(%Conversation{} = conversation) do Conversation.changeset(conversation, %{}) end + + alias Philomena.Conversations.Message + + @doc """ + Returns the list of messages. + + ## Examples + + iex> list_messages() + [%Message{}, ...] + + """ + def list_messages do + Repo.all(Message) + end + + @doc """ + Gets a single message. + + Raises `Ecto.NoResultsError` if the Message does not exist. + + ## Examples + + iex> get_message!(123) + %Message{} + + iex> get_message!(456) + ** (Ecto.NoResultsError) + + """ + def get_message!(id), do: Repo.get!(Message, id) + + @doc """ + Creates a message. + + ## Examples + + iex> create_message(%{field: value}) + {:ok, %Message{}} + + iex> create_message(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_message(attrs \\ %{}) do + %Message{} + |> Message.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a message. + + ## Examples + + iex> update_message(message, %{field: new_value}) + {:ok, %Message{}} + + iex> update_message(message, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_message(%Message{} = message, attrs) do + message + |> Message.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a Message. + + ## Examples + + iex> delete_message(message) + {:ok, %Message{}} + + iex> delete_message(message) + {:error, %Ecto.Changeset{}} + + """ + def delete_message(%Message{} = message) do + Repo.delete(message) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking message changes. + + ## Examples + + iex> change_message(message) + %Ecto.Changeset{source: %Message{}} + + """ + def change_message(%Message{} = message) do + Message.changeset(message, %{}) + end end diff --git a/lib/philomena/conversations/message.ex b/lib/philomena/conversations/message.ex new file mode 100644 index 00000000..c0846eeb --- /dev/null +++ b/lib/philomena/conversations/message.ex @@ -0,0 +1,20 @@ +defmodule Philomena.Conversations.Message do + use Ecto.Schema + import Ecto.Changeset + + schema "messages" do + belongs_to :conversation, Philomena.Conversations.Conversation + belongs_to :from, Philomena.Users.User + + field :body, :string + + timestamps(inserted_at: :created_at) + end + + @doc false + def changeset(message, attrs) do + message + |> cast(attrs, []) + |> validate_required([]) + end +end diff --git a/test/philomena/conversations_test.exs b/test/philomena/conversations_test.exs index 94357e2a..54fb1562 100644 --- a/test/philomena/conversations_test.exs +++ b/test/philomena/conversations_test.exs @@ -59,4 +59,61 @@ defmodule Philomena.ConversationsTest do assert %Ecto.Changeset{} = Conversations.change_conversation(conversation) end end + + describe "messages" do + alias Philomena.Conversations.Message + + @valid_attrs %{} + @update_attrs %{} + @invalid_attrs %{} + + def message_fixture(attrs \\ %{}) do + {:ok, message} = + attrs + |> Enum.into(@valid_attrs) + |> Conversations.create_message() + + message + end + + test "list_messages/0 returns all messages" do + message = message_fixture() + assert Conversations.list_messages() == [message] + end + + test "get_message!/1 returns the message with given id" do + message = message_fixture() + assert Conversations.get_message!(message.id) == message + end + + test "create_message/1 with valid data creates a message" do + assert {:ok, %Message{} = message} = Conversations.create_message(@valid_attrs) + end + + test "create_message/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Conversations.create_message(@invalid_attrs) + end + + test "update_message/2 with valid data updates the message" do + message = message_fixture() + assert {:ok, %Message{} = message} = Conversations.update_message(message, @update_attrs) + end + + test "update_message/2 with invalid data returns error changeset" do + message = message_fixture() + assert {:error, %Ecto.Changeset{}} = Conversations.update_message(message, @invalid_attrs) + assert message == Conversations.get_message!(message.id) + end + + test "delete_message/1 deletes the message" do + message = message_fixture() + assert {:ok, %Message{}} = Conversations.delete_message(message) + assert_raise Ecto.NoResultsError, fn -> Conversations.get_message!(message.id) end + end + + test "change_message/1 returns a message changeset" do + message = message_fixture() + assert %Ecto.Changeset{} = Conversations.change_message(message) + end + end end