philomena/lib/philomena_web/controllers/api/json/topics_controller.ex
SomewhatDamaged ae5bfe14e4 Add /forums API Endpoints (#14)
* Adding routes for /forums

Adding routes for new /forums endpoints

* New API Routes

`/forum`
`/forum/:forum_id`

* Add New API Routes

`/forums/:forum_id/topics/:topic_id/`
`/forums/:forum_id/topics/:topic_id/:post_id`

* Adds New API Routes

`/forums/:forum_id/topics`
`/forums/:forum_id/topics/:topic_id`

* Forum JSON formatter

* Updated

Correctly filters out things that should not be shown.

* Added topic JSON filter

* Added correct pagination

* Spacing and neatened

* Cleaned up

Consolidated queries and neatened up spacing.
Fixed name

* Removed unneeded aliases

* Cleaned up

Consolidated queries
Fixed spacing
Removed unneeded aliases

* Cleaned up

Spacing

* Cleaned up

spacing

* Depluralized

* Cleaned up

Removed space

* Removed extra spaces

* Removed extra spaces

* Removed extra spaces

* Removed preloads

* Cleaned up

Grouped clauses

* Consolodated further

* Cleaned up preloads

* Cleaned up

Preloads
consolidated clauses
grouped clauses

* Cleaned up

removed `id`
`locked`

* Removed topic_id

Not useful since the API client should already know it.

* More consolodation of terms

* Add total post count

* Removed unneeded variable

* Last cleanup, I swear.
2020-01-02 17:47:12 -05:00

43 lines
1.2 KiB
Elixir

defmodule PhilomenaWeb.Api.Json.TopicController do
use PhilomenaWeb, :controller
alias PhilomenaWeb.TopicJson
alias Philomena.Topics.Topic
alias Philomena.Repo
import Ecto.Query
def show(conn, %{"forum_id" => forum_id, "id" => id}) do
topic =
Topic
|> join(:inner, [t], _ in assoc(t, :forum))
|> where(slug: ^id)
|> where(hidden_from_users: false)
|> where([_t, f], f.access_level == "normal" and f.short_name == ^forum_id)
|> order_by(desc: :sticky, desc: :last_replied_to_at)
|> preload([:user])
|> Repo.one()
cond do
is_nil(topic) ->
conn
|> put_status(:not_found)
|> text("")
true ->
json(conn, %{topic: TopicJson.as_json(topic)})
end
end
def index(conn, %{"forum_id" => id}) do
topics =
Topic
|> join(:inner, [t], _ in assoc(t, :forum))
|> where(hidden_from_users: false)
|> where([_t, f], f.access_level == "normal" and f.short_name == ^id)
|> order_by(desc: :sticky, desc: :last_replied_to_at)
|> preload([:user])
|> Repo.paginate(conn.assigns.scrivener)
json(conn, %{topic: Enum.map(topics, &TopicJson.as_json/1)})
end
end