Skip to content

Commit

Permalink
WIP Adding support for comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Richey committed Feb 4, 2016
1 parent db9104b commit d583698
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .iex.exs
@@ -1,3 +1,6 @@
alias Pxblog.Repo
alias Pxblog.User
alias Pxblog.Post
alias Pxblog.Comment
import Ecto
import Ecto.Query
26 changes: 26 additions & 0 deletions test/controllers/comment_controller_test.exs
@@ -0,0 +1,26 @@
defmodule Pxblog.CommentControllerTest do
use Pxblog.ConnCase

alias Pxblog.Factory

@valid_attrs %{author: "Some Person", body: "This is a sample comment"}
@invalid_attrs %{}

setup do
user = Factory.create(:user)
post = Factory.create(:post, user: user)

{:ok, conn: conn, user: user, post: post}
end

test "creates resource and redirects when data is valid", %{conn: conn, post: post} do
conn = post conn, post_comment_path(conn, :create, post), comment: @valid_attrs
assert redirected_to(conn) == user_post_path(conn, :show, post.user, post)
assert Repo.get_by(assoc(post, :comments), @valid_attrs)
end

test "does not create resource and renders errors when data is invalid", %{conn: conn, post: post} do
conn = post conn, post_comment_path(conn, :create, post), comment: @invalid_attrs
assert html_response(conn, 200) =~ "Oops, something went wrong"
end
end
78 changes: 78 additions & 0 deletions web/controllers/comment_controller.ex
@@ -0,0 +1,78 @@
defmodule Pxblog.CommentController do
use Pxblog.Web, :controller

alias Pxblog.Comment
alias Pxblog.Post

plug :scrub_params, "comment" when action in [:create, :update]

def create(conn, %{"comment" => comment_params, "post_id" => post_id}) do
post = Repo.get!(Post, post_id) |> Repo.preload([:user, :comments])
changeset = post
|> build_assoc(:comments)
|> Comment.changeset(comment_params)

case Repo.insert(changeset) do
{:ok, _comment} ->
conn
|> put_flash(:info, "Comment created successfully!")
|> redirect(to: user_post_path(conn, :show, post.user, post))
{:error, changeset} ->
render(conn, Pxblog.PostView, "show.html", post: post, user: post.user, comment_changeset: changeset)
end
end

def update(conn, _), do: conn
def delete(conn, _), do: conn

end


#alias Pxblog.Comment
#alias Pxblog.Post

#plug :assign_post
#plug :authorize_user when action in [:delete]


#def delete(conn, %{"id" => id}) do
#comment = Repo.get!(Comment, id)
#Repo.delete!(comment)
#conn
#|> put_flash(:info, "Comment deleted successfully.")
#|> redirect(to: user_post_path(conn, :index, conn.assigns[:post].user, conn.assigns[:post]))
#end

#defp assign_post(conn, _opts) do
#case conn.params do
#%{"post_id" => post_id} ->
#post = Repo.get(Post, post_id) |> Repo.preload(:user)
#case post do
#nil -> invalid_post(conn)
#post -> assign(conn, :post, post)
#end
#_ ->
#invalid_post(conn)
#end
#end

#defp invalid_post(conn) do
#conn
#|> put_flash(:error, "Invalid post!")
#|> redirect(to: page_path(conn, :index))
#|> halt
#end

#defp authorize_user(conn, _opts) do
#user = get_session(conn, :current_user)
#post = conn.assigns[:post]
#if user && Pxblog.RoleChecker.is_admin?(user) do
#conn
#else
#conn
#|> put_flash(:error, "You are not authorized to modify that post!")
#|> redirect(to: page_path(conn, :index))
#|> halt
#end
#end
#end
8 changes: 6 additions & 2 deletions web/controllers/post_controller.ex
Expand Up @@ -35,8 +35,12 @@ defmodule Pxblog.PostController do
end

def show(conn, %{"id" => id}) do
post = Repo.get!(assoc(conn.assigns[:user], :posts), id)
render(conn, "show.html", post: post)
post = Repo.get!(assoc(conn.assigns[:user], :posts), id) |> Repo.preload(:comments)

comment_changeset = post
|> build_assoc(:comments)
|> Pxblog.Comment.changeset()
render(conn, "show.html", post: post, comment_changeset: comment_changeset)
end

def edit(conn, %{"id" => id}) do
Expand Down
3 changes: 3 additions & 0 deletions web/router.ex
Expand Up @@ -21,6 +21,9 @@ defmodule Pxblog.Router do
resources "/posts", PostController
end
resources "/sessions", SessionController, only: [:new, :create, :delete]
resources "/posts", PostController, only: [] do
resources "/comments", CommentController, only: [:create, :delete, :update]
end
end

# Other scopes may use custom stacks.
Expand Down
21 changes: 21 additions & 0 deletions web/templates/comment/comment.html.eex
@@ -0,0 +1,21 @@
<div class="comment">
<div class="row">
<div class="col-xs-4">
<strong><%= @comment.author %></strong>
</div>
<div class="col-xs-4">
<em><%= @comment.inserted_at %></em>
</div>
<div class="col-xs-4 text-right">
<%= unless @comment.approved do %>
<button class="btn btn-xs btn-primary approve">Approve</button>
<% end %>
<button class="btn btn-xs btn-danger delete">Delete</button>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<%= @comment.body %>
</div>
</div>
</div>
25 changes: 25 additions & 0 deletions web/templates/comment/form.html.eex
@@ -0,0 +1,25 @@
<h2>New Comment</h2>
<%= form_for @changeset, @action, fn f -> %>
<%= if @changeset.action do %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below.</p>
</div>
<% end %>

<div class="form-group">
<%= label f, :author, class: "control-label" %>
<%= text_input f, :author, class: "form-control" %>
<%= error_tag f, :author %>
</div>

<div class="form-group">
<%= label f, :body, class: "control-label" %>
<%= textarea f, :body, class: "form-control", id: "body-editor" %>
<%= error_tag f, :body %>
</div>

<div class="form-group">
<%= submit "Submit", class: "btn btn-primary" %>
</div>
<% end %>

18 changes: 15 additions & 3 deletions web/templates/post/show.html.eex
@@ -1,18 +1,30 @@
<h2>Show post</h2>

<ul>

<li>
<strong>Title:</strong>
<%= @post.title %>
</li>

<li>
<strong>Body:</strong>
<%= @post.body %>
<div class="post-body">
<%= markdown(@post.body) %>
</div>
</li>

</ul>

<hr>

<div class="comments">
<h2>Comments</h2>
<%= for comment <- @post.comments do %>
<%= render Pxblog.CommentView, "comment.html", comment: comment %>
<% end %>
</div>

<hr>
<%= render Pxblog.CommentView, "form.html", changeset: @comment_changeset, action: post_comment_path(@conn, :create, @post) %>

<%= link "Edit", to: user_post_path(@conn, :edit, @user, @post) %>
<%= link "Back", to: user_post_path(@conn, :index, @user) %>
3 changes: 3 additions & 0 deletions web/views/comment_view.ex
@@ -0,0 +1,3 @@
defmodule Pxblog.CommentView do
use Pxblog.Web, :view
end

0 comments on commit d583698

Please sign in to comment.