Skip to content

Commit

Permalink
Create issue comment controller
Browse files Browse the repository at this point in the history
  • Loading branch information
npendery committed Apr 28, 2017
1 parent c098055 commit 9725b3b
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions web/controllers/github_issue_comment_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
defmodule CodeCorps.GithubIssueCommentController do
use CodeCorps.Web, :controller

alias CodeCorps.Comment

def handle(conn, payload) do
case payload["action"] do
"created" ->
# create comment
attributes = convert_comment_attributes(payload)
changeset = %Comment{} |> Comment.create_changeset(attributes)
case Repo.insert(changeset) do
{:ok, _comment} ->
conn
{:error, changeset}
# log error
conn
end
"edited" ->
# update comment
comment = lookup_comment(payload)
attributes = convert_comment_attributes(payload)
changeset = comment |> Comment.changeset(attributes)
case Repo.update(changeset) do
{:ok, _comment} ->
conn
{:error, changeset}
# log error
conn
end
"deleted" ->
# delete comment
comment = lookup_comment(payload)
Repo.delete(comment)
conn
_ ->
# log error or do nothing
conn
end
end

defp lookup_comment(payload) do
comment_id = payload["comment"]["id"]
Comment |> Repo.get_by(github_id: comment_id)
end

defp convert_comment_attributes(payload) do
comment = payload["comment"]
%{
"github_id" => comment["id"],
"markdown" => comment["body"],
"task_id" => CodeCorps.Task |> Repo.get_by(github_id: payload["issue"]["id"]),
"user_id" => CodeCorps.User |> Repo.get_by(github_id: comment["user"]["id"]) # Need to add funtion to create random CC user if no user in system
}
end
end

0 comments on commit 9725b3b

Please sign in to comment.