diff --git a/lib/philomena_web/controllers/api/json/filter/system_filter_controller.ex b/lib/philomena_web/controllers/api/json/filter/system_filter_controller.ex new file mode 100755 index 00000000..982d30c2 --- /dev/null +++ b/lib/philomena_web/controllers/api/json/filter/system_filter_controller.ex @@ -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 diff --git a/lib/philomena_web/controllers/api/json/filter/user_filter_controller.ex b/lib/philomena_web/controllers/api/json/filter/user_filter_controller.ex new file mode 100755 index 00000000..64080d12 --- /dev/null +++ b/lib/philomena_web/controllers/api/json/filter/user_filter_controller.ex @@ -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 diff --git a/lib/philomena_web/router.ex b/lib/philomena_web/router.ex index 31c5535e..76782db1 100644 --- a/lib/philomena_web/router.ex +++ b/lib/philomena_web/router.ex @@ -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