mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-01-20 06:37:59 +01:00
New /filters/:id API endpoint (#48)
* New /filters/:id API endpoint * Fix to use can? permissions * Fixes requested
This commit is contained in:
parent
c0edd49ec6
commit
7ddd8b8ec4
3 changed files with 46 additions and 0 deletions
28
lib/philomena_web/controllers/api/json/filter_controller.ex
Executable file
28
lib/philomena_web/controllers/api/json/filter_controller.ex
Executable file
|
@ -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
|
17
lib/philomena_web/filter_json.ex
Executable file
17
lib/philomena_web/filter_json.ex
Executable file
|
@ -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
|
|
@ -119,6 +119,7 @@ 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]
|
||||||
|
resources "/filters", FilterController, only: [:show]
|
||||||
|
|
||||||
resources "/forums", ForumController, only: [:show, :index] do
|
resources "/forums", ForumController, only: [:show, :index] do
|
||||||
resources "/topics", Forum.TopicController, only: [:show, :index] do
|
resources "/topics", Forum.TopicController, only: [:show, :index] do
|
||||||
|
|
Loading…
Reference in a new issue