mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 13:47:58 +01:00
context: conversations
This commit is contained in:
parent
1a0b0f41e9
commit
b7824c4abc
3 changed files with 192 additions and 0 deletions
104
lib/philomena/conversations.ex
Normal file
104
lib/philomena/conversations.ex
Normal file
|
@ -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
|
26
lib/philomena/conversations/conversation.ex
Normal file
26
lib/philomena/conversations/conversation.ex
Normal file
|
@ -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
|
62
test/philomena/conversations_test.exs
Normal file
62
test/philomena/conversations_test.exs
Normal file
|
@ -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
|
Loading…
Reference in a new issue