New /filters/:id API endpoint (#48)

* New /filters/:id API endpoint

* Fix to use can? permissions

* Fixes requested
This commit is contained in:
SomewhatDamaged 2020-03-13 03:53:25 +11:00 committed by GitHub
parent c0edd49ec6
commit 7ddd8b8ec4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 0 deletions

View 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

View 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

View file

@ -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