diff --git a/lib/philomena_web/controllers/api/json/filter_controller.ex b/lib/philomena_web/controllers/api/json/filter_controller.ex new file mode 100755 index 00000000..a1242d1e --- /dev/null +++ b/lib/philomena_web/controllers/api/json/filter_controller.ex @@ -0,0 +1,28 @@ +defmodule PhilomenaWeb.Api.Json.FilterController do + use PhilomenaWeb, :controller + + alias PhilomenaWeb.FilterJson + alias Philomena.Filters.Filter + alias Philomena.Repo + import Ecto.Query + + def show(conn, %{"id" => id}) do + user = conn.assigns.current_user + + filter = + Filter + |> where(id: ^id) + |> preload(:user) + |> Repo.one() + + case Canada.Can.can?(user, :show, filter) do + true -> + json(conn, %{filter: FilterJson.as_json(filter)}) + + _ -> + conn + |> put_status(:not_found) + |> text("") + end + end +end diff --git a/lib/philomena_web/filter_json.ex b/lib/philomena_web/filter_json.ex new file mode 100755 index 00000000..4c799bb5 --- /dev/null +++ b/lib/philomena_web/filter_json.ex @@ -0,0 +1,17 @@ +defmodule PhilomenaWeb.FilterJson do + def as_json(filter) do + %{ + id: filter.id, + name: filter.name, + description: filter.description, + public: filter.public, + system: filter.system, + user_count: filter.user_count, + user_id: filter.user_id, + hidden_tag_ids: filter.hidden_tag_ids, + spoilered_tag_ids: filter.spoilered_tag_ids, + hidden_complex: filter.hidden_complex_str, + spoilered_complex: filter.spoilered_complex_str + } + end +end diff --git a/lib/philomena_web/router.ex b/lib/philomena_web/router.ex index 73727e36..31c5535e 100644 --- a/lib/philomena_web/router.ex +++ b/lib/philomena_web/router.ex @@ -119,6 +119,7 @@ defmodule PhilomenaWeb.Router do resources "/comments", CommentController, only: [:show] resources "/posts", PostController, only: [:show] resources "/profiles", ProfileController, only: [:show] + resources "/filters", FilterController, only: [:show] resources "/forums", ForumController, only: [:show, :index] do resources "/topics", Forum.TopicController, only: [:show, :index] do