mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-01-20 06:37:59 +01:00
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:
parent
faae3bda45
commit
84ac9d27d7
3 changed files with 58 additions and 0 deletions
21
lib/philomena_web/controllers/api/json/filter/system_filter_controller.ex
Executable file
21
lib/philomena_web/controllers/api/json/filter/system_filter_controller.ex
Executable 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
|
31
lib/philomena_web/controllers/api/json/filter/user_filter_controller.ex
Executable file
31
lib/philomena_web/controllers/api/json/filter/user_filter_controller.ex
Executable 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
|
|
@ -119,6 +119,12 @@ defmodule PhilomenaWeb.Router do
|
|||
resources "/comments", CommentController, only: [:show]
|
||||
resources "/posts", PostController, 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 "/forums", ForumController, only: [:show, :index] do
|
||||
|
|
Loading…
Reference in a new issue