Skip to content

Commit 62d2998

Browse files
committed
Replace TaskList with jsonapi
1 parent 18ac285 commit 62d2998

File tree

10 files changed

+132
-4016
lines changed

10 files changed

+132
-4016
lines changed

config/config.exs

+3
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ config :code_corps, :processor, CodeCorps.Processor.Async
8787

8888
config :code_corps, password_reset_timeout: 3600
8989

90+
config :jsonapi,
91+
remove_links: true
92+
9093
# Import environment specific config. This must remain at the bottom
9194
# of this file so it overrides the configuration defined above.
9295
import_config "#{Mix.env}.exs"

lib/code_corps_web/controllers/task_list_controller.ex

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ defmodule CodeCorpsWeb.TaskListController do
33
use CodeCorpsWeb, :controller
44

55
alias CodeCorps.{Helpers.Query, TaskList}
6+
alias CodeCorpsWeb.{TaskListView}
67

78
action_fallback CodeCorpsWeb.FallbackController
89
plug CodeCorpsWeb.Plug.DataToAttributes
@@ -18,13 +19,13 @@ defmodule CodeCorpsWeb.TaskListController do
1819
|> Repo.all()
1920
|> preload()
2021

21-
conn |> render("index.json-api", data: task_lists)
22+
conn |> render(TaskListView, "index.json-api", %{data: task_lists, conn: conn})
2223
end
2324

2425
@spec show(Conn.t, map) :: Conn.t
2526
def show(%Conn{} = conn, %{"id" => id}) do
2627
with %TaskList{} = task_list <- TaskList |> Repo.get(id) |> preload() do
27-
conn |> render("show.json-api", data: task_list)
28+
conn |> render(TaskListView, "show.json-api", %{data: task_list, conn: conn, params: id})
2829
end
2930
end
3031

lib/code_corps_web/views/comment_view.ex

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ defmodule CodeCorpsWeb.CommentView do
22
@moduledoc false
33
use CodeCorpsWeb, :view
44
use JaSerializer.PhoenixView
5+
use JSONAPI.View, type: "comment"
56

67
attributes [
78
:body, :created_at, :created_from, :inserted_at, :markdown, :modified_at,

lib/code_corps_web/views/project_view.ex

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ defmodule CodeCorpsWeb.ProjectView do
55

66
use CodeCorpsWeb, :view
77
use JaSerializer.PhoenixView
8+
use JSONAPI.View, type: "project"
9+
10+
def fields do
11+
[:approved, :can_activate_donations, :cloudinary_public_id,
12+
:description, :donations_active, :icon_thumb_url,
13+
:icon_large_url, :inserted_at, :long_description_body,
14+
:long_description_markdown, :should_link_externally, :slug, :title,
15+
:total_monthly_donated, :updated_at, :website]
16+
end
817

918
attributes [
1019
:approval_requested,
+14-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
defmodule CodeCorpsWeb.TaskListView do
22
@moduledoc false
33
use CodeCorpsWeb, :view
4-
use JaSerializer.PhoenixView
4+
use JSONAPI.View, type: "task-list"
55

6-
attributes [:done, :inbox, :name, :order, :pull_requests, :inserted_at, :updated_at]
6+
alias CodeCorpsWeb.{ProjectView, TaskView}
77

8-
has_one :project, type: "project", field: :project_id
8+
def render("index.json-api", %{data: task_list, conn: conn}) do
9+
__MODULE__.index(task_list, conn, nil)
10+
end
911

10-
has_many :tasks, serializer: CodeCorpsWeb.TaskView, identifiers: :always
12+
def render("show.json-api", %{data: task_list, conn: conn, params: params}) do
13+
__MODULE__.show(task_list, conn, params)
14+
end
15+
16+
def fields, do: [:done, :inbox, :name, :order, :pull_requests, :inserted_at, :updated_at]
17+
18+
def relationships do
19+
[project: ProjectView, tasks: TaskView]
20+
end
1121
end

lib/code_corps_web/views/task_view.ex

+30-30
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
defmodule CodeCorpsWeb.TaskView do
22
@moduledoc false
33
use CodeCorpsWeb, :view
4-
use JaSerializer.PhoenixView
4+
use JSONAPI.View, type: "task"
55

6-
attributes [
7-
:archived, :body, :created_at, :created_from, :has_github_pull_request,
8-
:inserted_at, :markdown, :modified_at, :modified_from, :number, :order,
9-
:overall_status, :status, :title, :updated_at
10-
]
6+
alias CodeCorpsWeb.{GithubIssueView, GithubPullRequestView, GithubRepoView, ProjectView,
7+
TaskListView, UserView, UserTaskView, CommentView, TaskSkillView}
118

12-
has_one :github_issue, type: "github-issue", field: :github_issue_id
13-
has_one :github_pull_request, serializer: CodeCorpsWeb.GithubPullRequestView, identifiers: :always
14-
has_one :github_repo, type: "github-repo", field: :github_repo_id
15-
has_one :project, type: "project", field: :project_id
16-
has_one :task_list, type: "task-list", field: :task_list_id
17-
has_one :user, type: "user", field: :user_id
18-
has_one :user_task, serializer: CodeCorpsWeb.UserTaskView, identifiers: :always
9+
def fields do
10+
[:archived, :body, :created_at, :created_from, :inserted_at, :markdown,
11+
:modified_at, :modified_from, :number, :order, :status, :title, :updated_at]
12+
end
1913

20-
has_many :comments, serializer: CodeCorpsWeb.CommentView, identifiers: :always
21-
has_many :task_skills, serializer: CodeCorpsWeb.TaskSkillView, identifiers: :always
14+
def relationships do
15+
[comments: CommentView, task_skills: TaskSkillView]
16+
end
2217

23-
def has_github_pull_request(%{
24-
github_pull_request: %CodeCorps.GithubPullRequest{}
25-
}), do: true
26-
def has_github_pull_request(%{github_pull_request: nil}), do: false
18+
# <<<<<<< HEAD
19+
# has_many :comments, serializer: CodeCorpsWeb.CommentView, identifiers: :always
20+
# has_many :task_skills, serializer: CodeCorpsWeb.TaskSkillView, identifiers: :always
2721

28-
def overall_status(%{
29-
github_pull_request: %CodeCorps.GithubPullRequest{merged: merged, state: state}
30-
}, _conn) do
31-
case merged do
32-
true -> "merged"
33-
false -> state
34-
end
35-
end
36-
def overall_status(%{github_pull_request: nil, status: status}, _conn) do
37-
status
38-
end
22+
# def has_github_pull_request(%{
23+
# github_pull_request: %CodeCorps.GithubPullRequest{}
24+
# }), do: true
25+
# def has_github_pull_request(%{github_pull_request: nil}), do: false
26+
27+
# def overall_status(%{
28+
# github_pull_request: %CodeCorps.GithubPullRequest{merged: merged, state: state}
29+
# }, _conn) do
30+
# case merged do
31+
# true -> "merged"
32+
# false -> state
33+
# end
34+
# end
35+
# def overall_status(%{github_pull_request: nil, status: status}, _conn) do
36+
# status
37+
# end
38+
# =======
3939
end

mix.exs

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ defmodule CodeCorps.Mixfile do
7272
{:ja_serializer, "~> 0.12"}, # JSON API
7373
{:joken, "~> 1.5"}, # JWT encoding
7474
{:mix_test_watch, "~> 0.5", only: :dev, runtime: false},
75+
{:jsonapi, git: "https://github.com/jeregrine/jsonapi.git"},
7576
{:money, "~> 1.2.1"},
7677
{:poison, "~> 3.0", override: true},
7778
{:scout_apm, "~> 0.0"},

0 commit comments

Comments
 (0)