philomena/lib/philomena_web/controllers/api/json/comment_controller.ex
liamwhite 723bfa213f
Use JSON views for API (#64)
* AwardJson, LinkJson, UserJson to views

* FilterJson -> view

* ForumJson -> view

* TopicJson -> view

* PostJson -> view

* TagJson -> view

* CommentJson -> view

* GalleryJson -> view
2020-03-29 21:11:38 -04:00

30 lines
634 B
Elixir

defmodule PhilomenaWeb.Api.Json.CommentController do
use PhilomenaWeb, :controller
alias Philomena.Comments.Comment
alias Philomena.Repo
import Ecto.Query
def show(conn, %{"id" => id}) do
comment =
Comment
|> where(id: ^id)
|> preload([:image, :user])
|> Repo.one()
cond do
is_nil(comment) or comment.destroyed_content ->
conn
|> put_status(:not_found)
|> text("")
comment.image.hidden_from_users ->
conn
|> put_status(:forbidden)
|> text("")
true ->
render(conn, "show.json", comment: comment)
end
end
end