mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-23 20:18:00 +01:00
context: messages
This commit is contained in:
parent
ce54176940
commit
479783cb7d
3 changed files with 173 additions and 0 deletions
|
@ -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
|
||||
|
|
20
lib/philomena/conversations/message.ex
Normal file
20
lib/philomena/conversations/message.ex
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue