From ffc82fc69b540b683dc1ef71811719e18496ad31 Mon Sep 17 00:00:00 2001 From: snewcomer24 Date: Wed, 27 Dec 2017 13:29:48 -0800 Subject: [PATCH] Include conversation parts in conversation --- .../controllers/conversation_controller.ex | 11 ++- .../views/conversation_part_view.ex | 3 +- lib/code_corps_web/views/conversation_view.ex | 2 +- .../views/conversation_part_view_test.exs | 6 -- .../views/conversation_view_test.exs | 69 ++++++++++++++++++- 5 files changed, 76 insertions(+), 15 deletions(-) diff --git a/lib/code_corps_web/controllers/conversation_controller.ex b/lib/code_corps_web/controllers/conversation_controller.ex index 5d3b23686..0d2594aa2 100644 --- a/lib/code_corps_web/controllers/conversation_controller.ex +++ b/lib/code_corps_web/controllers/conversation_controller.ex @@ -23,9 +23,9 @@ defmodule CodeCorpsWeb.ConversationController do @spec show(Conn.t, map) :: Conn.t def show(%Conn{} = conn, %{"id" => id}) do with %User{} = current_user <- conn |> CodeCorps.Guardian.Plug.current_resource, - %Conversation{} = conversation <- Messages.get_conversation(id) |> preload(), + %Conversation{} = conversation <- Messages.get_conversation(id) |> preload_show(), {:ok, :authorized} <- current_user |> Policy.authorize(:show, conversation, %{}) do - conn |> render("show.json-api", data: conversation) + conn |> render("show.json-api", data: conversation, opts: [include: "conversation_parts"]) end end @@ -41,9 +41,14 @@ defmodule CodeCorpsWeb.ConversationController do end end - @preloads [:conversation_parts, :message, :user] + @preloads [:message, :user] + @preloads_show [:message, :user, conversation_parts: [:author]] def preload(data) do Repo.preload(data, @preloads) end + + def preload_show(data) do + Repo.preload(data, @preloads_show) + end end diff --git a/lib/code_corps_web/views/conversation_part_view.ex b/lib/code_corps_web/views/conversation_part_view.ex index ed0d50054..a0f5ec60f 100644 --- a/lib/code_corps_web/views/conversation_part_view.ex +++ b/lib/code_corps_web/views/conversation_part_view.ex @@ -6,5 +6,4 @@ defmodule CodeCorpsWeb.ConversationPartView do attributes [:body, :inserted_at, :read_at, :updated_at] has_one :author, type: "user", field: :author_id - has_one :conversation, type: "conversation", field: :conversation_id -end +end \ No newline at end of file diff --git a/lib/code_corps_web/views/conversation_view.ex b/lib/code_corps_web/views/conversation_view.ex index 90fb80fbc..8ca78fa68 100644 --- a/lib/code_corps_web/views/conversation_view.ex +++ b/lib/code_corps_web/views/conversation_view.ex @@ -8,5 +8,5 @@ defmodule CodeCorpsWeb.ConversationView do has_one :user, type: "user", field: :user_id has_one :message, type: "message", field: :message_id - has_many :conversation_parts, serializer: CodeCorpsWeb.ConversationPartView, identifiers: :always + has_many :conversation_parts, serializer: CodeCorpsWeb.ConversationPartView, include: false, identifiers: :when_included end diff --git a/test/lib/code_corps_web/views/conversation_part_view_test.exs b/test/lib/code_corps_web/views/conversation_part_view_test.exs index c953e4076..37308e107 100644 --- a/test/lib/code_corps_web/views/conversation_part_view_test.exs +++ b/test/lib/code_corps_web/views/conversation_part_view_test.exs @@ -23,12 +23,6 @@ defmodule CodeCorpsWeb.ConversationPartViewTest do "id" => conversation_part.author_id |> Integer.to_string, "type" => "user" } - }, - "conversation" => %{ - "data" => %{ - "id" => conversation_part.conversation_id |> Integer.to_string, - "type" => "conversation" - } } } }, diff --git a/test/lib/code_corps_web/views/conversation_view_test.exs b/test/lib/code_corps_web/views/conversation_view_test.exs index 44d593bab..31b1f2eae 100644 --- a/test/lib/code_corps_web/views/conversation_view_test.exs +++ b/test/lib/code_corps_web/views/conversation_view_test.exs @@ -3,9 +3,8 @@ defmodule CodeCorpsWeb.ConversationViewTest do alias CodeCorps.Repo - test "renders all attributes and relationships properly" do + test "renders index attributes and relationships properly" do conversation = insert(:conversation) - conversation_part = insert(:conversation_part, conversation: conversation) rendered_json = CodeCorpsWeb.ConversationView @@ -14,6 +13,52 @@ defmodule CodeCorpsWeb.ConversationViewTest do data: conversation |> Repo.preload(:conversation_parts) ) + expected_json = %{ + "data" => %{ + "id" => conversation.id |> Integer.to_string, + "type" => "conversation", + "attributes" => %{ + "read-at" => conversation.read_at, + "status" => conversation.status, + "inserted-at" => conversation.inserted_at, + "updated-at" => conversation.updated_at + }, + "relationships" => %{ + "conversation-parts" => %{}, + "message" => %{ + "data" => %{ + "id" => conversation.message_id |> Integer.to_string, + "type" => "message" + } + }, + "user" => %{ + "data" => %{ + "id" => conversation.user_id |> Integer.to_string, + "type" => "user" + } + } + } + }, + "jsonapi" => %{ + "version" => "1.0" + } + } + + assert rendered_json == expected_json + end + + test "renders show attributes and relationships properly" do + conversation = insert(:conversation) + conversation_part = insert(:conversation_part, conversation: conversation) + + rendered_json = + CodeCorpsWeb.ConversationView + |> render( + "show.json-api", + data: conversation |> Repo.preload(:conversation_parts), + opts: [include: "conversation_parts"] + ) + expected_json = %{ "data" => %{ "id" => conversation.id |> Integer.to_string, @@ -49,7 +94,25 @@ defmodule CodeCorpsWeb.ConversationViewTest do }, "jsonapi" => %{ "version" => "1.0" - } + }, + "included" => [%{ + "attributes" => %{ + "body" => conversation_part.body, + "inserted-at" => conversation_part.inserted_at, + "read-at" => conversation_part.read_at, + "updated-at" => conversation_part.updated_at + }, + "relationships" => %{ + "author" => %{ + "data" => %{ + "id" => conversation_part.author.id |> Integer.to_string, + "type" => "user" + } + } + }, + "id" => conversation_part.id |> Integer.to_string, + "type" => "conversation-part" + }] } assert rendered_json == expected_json