Adds /api/v1/json/filters endpoints (#55)

* Adds /api/v1/json/filters endpoint

* Modified as per discussion

* Using Repo.paginate properly now

* Added total count to output
This commit is contained in:
SomewhatDamaged 2020-03-26 02:32:25 +11:00 committed by GitHub
parent faae3bda45
commit 84ac9d27d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 0 deletions

View file

@ -0,0 +1,21 @@
defmodule PhilomenaWeb.Api.Json.Filter.SystemFilterController do
use PhilomenaWeb, :controller
alias PhilomenaWeb.FilterJson
alias Philomena.Filters.Filter
alias Philomena.Repo
import Ecto.Query
def index(conn, _params) do
system_filters =
Filter
|> where(system: true)
|> order_by(asc: :id)
|> Repo.paginate(conn.assigns.scrivener)
json(conn, %{
filters: Enum.map(system_filters, &FilterJson.as_json/1),
total: system_filters.total_entries
})
end
end

View file

@ -0,0 +1,31 @@
defmodule PhilomenaWeb.Api.Json.Filter.UserFilterController do
use PhilomenaWeb, :controller
alias PhilomenaWeb.FilterJson
alias Philomena.Filters.Filter
alias Philomena.Repo
import Ecto.Query
def index(conn, _params) do
user = conn.assigns.current_user
case user do
nil ->
conn
|> put_status(:forbidden)
|> text("")
_ ->
user_filters =
Filter
|> where(user_id: ^user.id)
|> order_by(asc: :id)
|> Repo.paginate(conn.assigns.scrivener)
json(conn, %{
filters: Enum.map(user_filters, &FilterJson.as_json/1),
total: user_filters.total_entries
})
end
end
end

View file

@ -119,6 +119,12 @@ defmodule PhilomenaWeb.Router do
resources "/comments", CommentController, only: [:show] resources "/comments", CommentController, only: [:show]
resources "/posts", PostController, only: [:show] resources "/posts", PostController, only: [:show]
resources "/profiles", ProfileController, only: [:show] resources "/profiles", ProfileController, only: [:show]
scope "/filters", Filter, as: :filter do
resources "/user", UserFilterController, only: [:index]
resources "/system", SystemFilterController, only: [:index]
end
resources "/filters", FilterController, only: [:show] resources "/filters", FilterController, only: [:show]
resources "/forums", ForumController, only: [:show, :index] do resources "/forums", ForumController, only: [:show, :index] do