philomena/lib/philomena_web/controllers/image/comment_controller.ex
2019-11-11 21:53:19 -05:00

33 lines
1.1 KiB
Elixir

defmodule PhilomenaWeb.Image.CommentController do
use PhilomenaWeb, :controller
alias Philomena.{Images.Image, Comments.Comment, Textile.Renderer}
alias Philomena.Repo
import Ecto.Query
plug :load_and_authorize_resource, model: Image, id_name: "image_id", persisted: true
plug :load_and_authorize_resource, model: Comment, only: [:show], preload: [:image, user: [awards: :badge]]
def index(conn, _params) do
comments =
Comment
|> where(image_id: ^conn.assigns.image.id)
|> order_by(desc: :created_at)
|> preload([:image, user: [awards: :badge]])
|> Repo.paginate(conn.assigns.scrivener)
rendered =
comments.entries
|> Renderer.render_collection()
comments =
%{comments | entries: Enum.zip(comments.entries, rendered)}
render(conn, "index.html", layout: false, image: conn.assigns.image, comments: comments)
end
def show(conn, _params) do
rendered = Renderer.render_one(conn.assigns.comment)
render(conn, "show.html", layout: false, image: conn.assigns.image, comment: conn.assigns.comment, body: rendered)
end
end