mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-24 12:37:58 +01:00
035b887f53
* Add new route Adding route for `/api/v1/json/posts/:id` * Adding route controller Adding route for `/api/v1/json/posts/:id`
31 lines
780 B
Elixir
31 lines
780 B
Elixir
defmodule PhilomenaWeb.Api.Json.PostController do
|
|
use PhilomenaWeb, :controller
|
|
|
|
alias PhilomenaWeb.PostJson
|
|
alias Philomena.Posts.Post
|
|
alias Philomena.Repo
|
|
import Ecto.Query
|
|
|
|
def show(conn, %{"id" => post_id}) do
|
|
post =
|
|
Post
|
|
|> join(:inner, [p], _ in assoc(p, :topic))
|
|
|> join(:inner, [_p, t], _ in assoc(t, :forum))
|
|
|> where(id: ^post_id)
|
|
|> where(destroyed_content: false)
|
|
|> where([_p, t], t.hidden_from_users == false)
|
|
|> where([_p, _t, f], f.access_level == "normal")
|
|
|> preload([:user, :topic])
|
|
|> Repo.one()
|
|
|
|
cond do
|
|
is_nil(post) ->
|
|
conn
|
|
|> put_status(:not_found)
|
|
|> text("")
|
|
|
|
true ->
|
|
json(conn, %{post: PostJson.as_json(post)})
|
|
end
|
|
end
|
|
end
|