Skip to content

Commit d98b3a4

Browse files
committed
Include conversation parts in conversation
1 parent 810cc30 commit d98b3a4

File tree

5 files changed

+78
-15
lines changed

5 files changed

+78
-15
lines changed

lib/code_corps_web/controllers/conversation_controller.ex

+8-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ defmodule CodeCorpsWeb.ConversationController do
2323
@spec show(Conn.t, map) :: Conn.t
2424
def show(%Conn{} = conn, %{"id" => id}) do
2525
with %User{} = current_user <- conn |> CodeCorps.Guardian.Plug.current_resource,
26-
%Conversation{} = conversation <- Messages.get_conversation(id) |> preload(),
26+
%Conversation{} = conversation <- Messages.get_conversation(id) |> preload_show(),
2727
{:ok, :authorized} <- current_user |> Policy.authorize(:show, conversation, %{}) do
28-
conn |> render("show.json-api", data: conversation)
28+
conn |> render("show.json-api", data: conversation, opts: [include: "conversation_parts"])
2929
end
3030
end
3131

@@ -41,9 +41,14 @@ defmodule CodeCorpsWeb.ConversationController do
4141
end
4242
end
4343

44-
@preloads [:conversation_parts, :message, :user]
44+
@preloads [:message, :user]
45+
@preloads_show [:message, :user, conversation_parts: [:author]]
4546

4647
def preload(data) do
4748
Repo.preload(data, @preloads)
4849
end
50+
51+
def preload_show(data) do
52+
Repo.preload(data, @preloads_show)
53+
end
4954
end

lib/code_corps_web/views/conversation_part_view.ex

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ defmodule CodeCorpsWeb.ConversationPartView do
66
attributes [:body, :inserted_at, :read_at, :updated_at]
77

88
has_one :author, type: "user", field: :author_id
9-
has_one :conversation, type: "conversation", field: :conversation_id
10-
end
9+
end

lib/code_corps_web/views/conversation_view.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ defmodule CodeCorpsWeb.ConversationView do
88
has_one :user, type: "user", field: :user_id
99
has_one :message, type: "message", field: :message_id
1010

11-
has_many :conversation_parts, serializer: CodeCorpsWeb.ConversationPartView, identifiers: :always
11+
has_many :conversation_parts, serializer: CodeCorpsWeb.ConversationPartView, include: false, identifiers: :when_included
1212
end

test/lib/code_corps_web/views/conversation_part_view_test.exs

-6
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ defmodule CodeCorpsWeb.ConversationPartViewTest do
2323
"id" => conversation_part.author_id |> Integer.to_string,
2424
"type" => "user"
2525
}
26-
},
27-
"conversation" => %{
28-
"data" => %{
29-
"id" => conversation_part.conversation_id |> Integer.to_string,
30-
"type" => "conversation"
31-
}
3226
}
3327
}
3428
},

test/lib/code_corps_web/views/conversation_view_test.exs

+68-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ defmodule CodeCorpsWeb.ConversationViewTest do
33

44
alias CodeCorps.Repo
55

6-
test "renders all attributes and relationships properly" do
6+
test "renders index attributes and relationships properly" do
77
conversation = insert(:conversation)
8-
conversation_part = insert(:conversation_part, conversation: conversation)
98

109
rendered_json =
1110
CodeCorpsWeb.ConversationView
@@ -19,9 +18,55 @@ defmodule CodeCorpsWeb.ConversationViewTest do
1918
"id" => conversation.id |> Integer.to_string,
2019
"type" => "conversation",
2120
"attributes" => %{
21+
"inserted-at" => conversation.inserted_at,
2222
"read-at" => conversation.read_at,
2323
"status" => conversation.status,
24+
"updated-at" => conversation.updated_at
25+
},
26+
"relationships" => %{
27+
"conversation-parts" => %{},
28+
"message" => %{
29+
"data" => %{
30+
"id" => conversation.message_id |> Integer.to_string,
31+
"type" => "message"
32+
}
33+
},
34+
"user" => %{
35+
"data" => %{
36+
"id" => conversation.user_id |> Integer.to_string,
37+
"type" => "user"
38+
}
39+
}
40+
}
41+
},
42+
"jsonapi" => %{
43+
"version" => "1.0"
44+
}
45+
}
46+
47+
assert rendered_json == expected_json
48+
end
49+
50+
test "renders show attributes and relationships properly" do
51+
conversation = insert(:conversation)
52+
conversation_part = insert(:conversation_part, conversation: conversation)
53+
54+
rendered_json =
55+
CodeCorpsWeb.ConversationView
56+
|> render(
57+
"show.json-api",
58+
data: conversation |> Repo.preload(:conversation_parts),
59+
opts: [include: "conversation_parts"]
60+
)
61+
62+
expected_json = %{
63+
"data" => %{
64+
"id" => conversation.id |> Integer.to_string,
65+
"type" => "conversation",
66+
"attributes" => %{
2467
"inserted-at" => conversation.inserted_at,
68+
"read-at" => conversation.read_at,
69+
"status" => conversation.status,
2570
"updated-at" => conversation.updated_at
2671
},
2772
"relationships" => %{
@@ -49,7 +94,27 @@ defmodule CodeCorpsWeb.ConversationViewTest do
4994
},
5095
"jsonapi" => %{
5196
"version" => "1.0"
52-
}
97+
},
98+
"included" => [
99+
%{
100+
"attributes" => %{
101+
"body" => conversation_part.body,
102+
"inserted-at" => conversation_part.inserted_at,
103+
"read-at" => conversation_part.read_at,
104+
"updated-at" => conversation_part.updated_at
105+
},
106+
"relationships" => %{
107+
"author" => %{
108+
"data" => %{
109+
"id" => conversation_part.author.id |> Integer.to_string,
110+
"type" => "user"
111+
}
112+
}
113+
},
114+
"id" => conversation_part.id |> Integer.to_string,
115+
"type" => "conversation-part"
116+
}
117+
]
53118
}
54119

55120
assert rendered_json == expected_json

0 commit comments

Comments
 (0)