philomena/lib/philomena_web/controllers/comment_controller.ex
2019-08-18 14:14:36 -04:00

62 lines
1.8 KiB
Elixir

defmodule PhilomenaWeb.CommentController do
use PhilomenaWeb, :controller
alias Philomena.Comments
alias Philomena.Comments.Comment
def index(conn, _params) do
comments = Comments.list_comments()
render(conn, "index.html", comments: comments)
end
def new(conn, _params) do
changeset = Comments.change_comment(%Comment{})
render(conn, "new.html", changeset: changeset)
end
def create(conn, %{"comment" => comment_params}) do
case Comments.create_comment(comment_params) do
{:ok, comment} ->
conn
|> put_flash(:info, "Comment created successfully.")
|> redirect(to: Routes.comment_path(conn, :show, comment))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
def show(conn, %{"id" => id}) do
comment = Comments.get_comment!(id)
render(conn, "show.html", comment: comment)
end
def edit(conn, %{"id" => id}) do
comment = Comments.get_comment!(id)
changeset = Comments.change_comment(comment)
render(conn, "edit.html", comment: comment, changeset: changeset)
end
def update(conn, %{"id" => id, "comment" => comment_params}) do
comment = Comments.get_comment!(id)
case Comments.update_comment(comment, comment_params) do
{:ok, comment} ->
conn
|> put_flash(:info, "Comment updated successfully.")
|> redirect(to: Routes.comment_path(conn, :show, comment))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "edit.html", comment: comment, changeset: changeset)
end
end
def delete(conn, %{"id" => id}) do
comment = Comments.get_comment!(id)
{:ok, _comment} = Comments.delete_comment(comment)
conn
|> put_flash(:info, "Comment deleted successfully.")
|> redirect(to: Routes.comment_path(conn, :index))
end
end