Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
29 lines (24 sloc) 864 Bytes
defmodule Discuss.CommentsChannel do
use Discuss.Web, :channel
alias Discuss.{Topic, Comment}
def join("comments:" <> topic_id, _params, socket) do
topic_id = String.to_integer(topic_id)
topic = Topic
|> Repo.get(topic_id)
|> Repo.preload(comments: [:user])
{:ok, %{comments: topic.comments}, assign(socket, :topic, topic)}
end
def handle_in(name, %{"content" => content}, socket) do
topic = socket.assigns.topic
changeset = topic
|> build_assoc(:comments, user_id: socket.assigns.user_id)
|> Comment.changeset(%{content: content})
case Repo.insert(changeset) do
{:ok, comment} ->
broadcast!(socket, "comments:#{socket.assigns.topic.id}:new", %{comment: comment})
{:reply, :ok, socket}
{:error, _reason} ->
{:reply, {:error, %{errors: changeset}}}
end
end
end
You can’t perform that action at this time.