Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace TaskList with jsonapi #1187

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ config :code_corps, :processor, CodeCorps.Processor.Async

config :code_corps, password_reset_timeout: 3600

config :jsonapi,
remove_links: true,
underscore_to_dash: true

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env}.exs"
Expand Down
5 changes: 3 additions & 2 deletions lib/code_corps_web/controllers/task_list_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule CodeCorpsWeb.TaskListController do
use CodeCorpsWeb, :controller

alias CodeCorps.{Helpers.Query, TaskList}
alias CodeCorpsWeb.{TaskListView}

action_fallback CodeCorpsWeb.FallbackController
plug CodeCorpsWeb.Plug.DataToAttributes
Expand All @@ -18,13 +19,13 @@ defmodule CodeCorpsWeb.TaskListController do
|> Repo.all()
|> preload()

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

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

Expand Down
43 changes: 43 additions & 0 deletions lib/code_corps_web/views/project_jsonapi_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
defmodule CodeCorpsWeb.ProjectJsonapiView do
@moduledoc false
alias CodeCorps.StripeService.Validators.ProjectCanEnableDonations
alias CodeCorps.Presenters.ImagePresenter

use JSONAPI.View, type: "project"
use CodeCorpsWeb, :view

def fields, do: [
:approval_requested,
:approved,
:can_activate_donations,
:cloudinary_public_id,
:description,
:donations_active,
:icon_thumb_url,
:icon_large_url,
:inserted_at,
:long_description_body,
:long_description_markdown,
:should_link_externally,
:slug,
:title,
:total_monthly_donated,
:updated_at,
:website
]

def can_activate_donations(project, _conn) do
case ProjectCanEnableDonations.validate(project) do
{:ok, _} -> true
{:error, _} -> false
end
end

def donations_active(project, _conn) do
Enum.any?(project.donation_goals) && project.stripe_connect_plan != nil
end

def icon_large_url(project, _conn), do: ImagePresenter.large(project)

def icon_thumb_url(project, _conn), do: ImagePresenter.thumbnail(project)
end
2 changes: 0 additions & 2 deletions lib/code_corps_web/views/project_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ defmodule CodeCorpsWeb.ProjectView do
has_many :project_skills, serializer: CodeCorpsWeb.ProjectSkillView, identifiers: :always
has_many :project_users, serializer: CodeCorpsWeb.ProjectUserView, identifiers: :always
has_many :skills, serializer: CodeCorpsWeb.SkillView, identifiers: :always
has_many :tasks, serializer: CodeCorpsWeb.TaskView, identifiers: :always
has_many :task_lists, serializer: CodeCorpsWeb.TaskListView, identifiers: :always

def can_activate_donations(project, _conn) do
case ProjectCanEnableDonations.validate(project) do
Expand Down
43 changes: 43 additions & 0 deletions lib/code_corps_web/views/task_jsonapi_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
defmodule CodeCorpsWeb.TaskJsonapiView do
@moduledoc false
use CodeCorpsWeb, :view
use JSONAPI.View, type: "task"

def fields, do: [
:archived, :body, :created_at, :created_from, :has_github_pull_request,
:inserted_at, :markdown, :modified_at, :modified_from, :number, :order,
:overall_status, :status, :title, :updated_at
]

# def relationships do
# [github_issue]
# end

# has_one :github_issue, type: "github-issue", field: :github_issue_id
# has_one :github_pull_request, serializer: CodeCorpsWeb.GithubPullRequestView, identifiers: :always
# has_one :github_repo, type: "github-repo", field: :github_repo_id
# has_one :project, type: "project", field: :project_id
# has_one :task_list, type: "task-list", field: :task_list_id
# has_one :user, type: "user", field: :user_id
# has_one :user_task, serializer: CodeCorpsWeb.UserTaskView, identifiers: :always

# has_many :comments, serializer: CodeCorpsWeb.CommentView, identifiers: :always
# has_many :task_skills, serializer: CodeCorpsWeb.TaskSkillView, identifiers: :always

def has_github_pull_request(%{
github_pull_request: %CodeCorps.GithubPullRequest{}
}), do: true
def has_github_pull_request(%{github_pull_request: nil}), do: false

def overall_status(%{
github_pull_request: %CodeCorps.GithubPullRequest{merged: merged, state: state}
}, _conn) do
case merged do
true -> "merged"
false -> state
end
end
def overall_status(%{github_pull_request: nil, status: status}, _conn) do
status
end
end
10 changes: 6 additions & 4 deletions lib/code_corps_web/views/task_list_view.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
defmodule CodeCorpsWeb.TaskListView do
@moduledoc false
use CodeCorpsWeb, :view
use JaSerializer.PhoenixView
use JSONAPI.View, type: "task-list"

attributes [:done, :inbox, :name, :order, :pull_requests, :inserted_at, :updated_at]
alias CodeCorpsWeb.{ProjectJsonapiView, TaskJsonapiView}

has_one :project, type: "project", field: :project_id
def fields, do: [:done, :inbox, :name, :order, :pull_requests, :inserted_at, :updated_at]

has_many :tasks, serializer: CodeCorpsWeb.TaskView, identifiers: :always
def relationships do
[project: {ProjectJsonapiView, :include}, tasks: {TaskJsonapiView, :include}]
end
end
2 changes: 1 addition & 1 deletion lib/code_corps_web/views/task_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ defmodule CodeCorpsWeb.TaskView do
def overall_status(%{github_pull_request: nil, status: status}, _conn) do
status
end
end
end
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ defmodule CodeCorps.Mixfile do
{:ja_serializer, "~> 0.12"}, # JSON API
{:joken, "~> 1.5"}, # JWT encoding
{:mix_test_watch, "~> 0.5", only: :dev, runtime: false},
{:jsonapi, "~> 0.6"},
{:money, "~> 1.2.1"},
{:poison, "~> 3.0", override: true},
{:scout_apm, "~> 0.0"},
Expand Down
Loading