Add pagination to AdminAPI.AnnouncementController.index
This commit is contained in:
parent
11a1996bf5
commit
eb1a29640f
@ -61,6 +61,13 @@ defmodule Pleroma.Announcement do
|
|||||||
|> Repo.all()
|
|> Repo.all()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def list_paginated(%{limit: limited_number, offset: offset_number}) do
|
||||||
|
__MODULE__
|
||||||
|
|> limit(^limited_number)
|
||||||
|
|> offset(^offset_number)
|
||||||
|
|> Repo.all()
|
||||||
|
end
|
||||||
|
|
||||||
def get_by_id(id) do
|
def get_by_id(id) do
|
||||||
Repo.get_by(__MODULE__, id: id)
|
Repo.get_by(__MODULE__, id: id)
|
||||||
end
|
end
|
||||||
|
@ -16,8 +16,13 @@ defmodule Pleroma.Web.AdminAPI.AnnouncementController do
|
|||||||
|
|
||||||
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.AnnouncementOperation
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.AnnouncementOperation
|
||||||
|
|
||||||
def index(conn, _params) do
|
defp default_limit, do: 20
|
||||||
announcements = Announcement.list_all()
|
|
||||||
|
def index(conn, params) do
|
||||||
|
limit = Map.get(params, :limit, default_limit())
|
||||||
|
offset = Map.get(params, :offset, 0)
|
||||||
|
|
||||||
|
announcements = Announcement.list_paginated(%{limit: limit, offset: offset})
|
||||||
|
|
||||||
render(conn, "index.json", announcements: announcements)
|
render(conn, "index.json", announcements: announcements)
|
||||||
end
|
end
|
||||||
|
@ -21,8 +21,24 @@ defmodule Pleroma.Web.ApiSpec.Admin.AnnouncementOperation do
|
|||||||
summary: "Retrieve a list of announcements",
|
summary: "Retrieve a list of announcements",
|
||||||
operationId: "AdminAPI.AnnouncementController.index",
|
operationId: "AdminAPI.AnnouncementController.index",
|
||||||
security: [%{"oAuth" => ["admin:read"]}],
|
security: [%{"oAuth" => ["admin:read"]}],
|
||||||
|
parameters: [
|
||||||
|
Operation.parameter(
|
||||||
|
:limit,
|
||||||
|
:query,
|
||||||
|
%Schema{type: :integer, minimum: 1},
|
||||||
|
"the maximum number of announcements to return"
|
||||||
|
),
|
||||||
|
Operation.parameter(
|
||||||
|
:offset,
|
||||||
|
:query,
|
||||||
|
%Schema{type: :integer, minimum: 0},
|
||||||
|
"the offset of the first announcement to return"
|
||||||
|
)
|
||||||
|
| admin_api_params()
|
||||||
|
],
|
||||||
responses: %{
|
responses: %{
|
||||||
200 => Operation.response("Response", "application/json", list_of_announcements()),
|
200 => Operation.response("Response", "application/json", list_of_announcements()),
|
||||||
|
400 => Operation.response("Forbidden", "application/json", ApiError),
|
||||||
403 => Operation.response("Forbidden", "application/json", ApiError)
|
403 => Operation.response("Forbidden", "application/json", ApiError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,54 @@ defmodule Pleroma.Web.AdminAPI.AnnouncementControllerTest do
|
|||||||
|
|
||||||
assert [%{"id" => ^id}] = response
|
assert [%{"id" => ^id}] = response
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "it paginates announcements", %{conn: conn} do
|
||||||
|
_announcements = Enum.map(0..20, fn _ -> insert(:announcement) end)
|
||||||
|
|
||||||
|
response =
|
||||||
|
conn
|
||||||
|
|> get("/api/v1/pleroma/admin/announcements")
|
||||||
|
|> json_response_and_validate_schema(:ok)
|
||||||
|
|
||||||
|
assert length(response) == 20
|
||||||
|
end
|
||||||
|
|
||||||
|
test "it paginates announcements with custom params", %{conn: conn} do
|
||||||
|
announcements = Enum.map(0..20, fn _ -> insert(:announcement) end)
|
||||||
|
|
||||||
|
response =
|
||||||
|
conn
|
||||||
|
|> get("/api/v1/pleroma/admin/announcements", limit: 5, offset: 7)
|
||||||
|
|> json_response_and_validate_schema(:ok)
|
||||||
|
|
||||||
|
assert length(response) == 5
|
||||||
|
assert Enum.at(response, 0)["id"] == Enum.at(announcements, 7).id
|
||||||
|
end
|
||||||
|
|
||||||
|
test "it returns empty list with out-of-bounds offset", %{conn: conn} do
|
||||||
|
_announcements = Enum.map(0..20, fn _ -> insert(:announcement) end)
|
||||||
|
|
||||||
|
response =
|
||||||
|
conn
|
||||||
|
|> get("/api/v1/pleroma/admin/announcements", offset: 21)
|
||||||
|
|> json_response_and_validate_schema(:ok)
|
||||||
|
|
||||||
|
assert [] = response
|
||||||
|
end
|
||||||
|
|
||||||
|
test "it rejects invalid pagination params", %{conn: conn} do
|
||||||
|
conn
|
||||||
|
|> get("/api/v1/pleroma/admin/announcements", limit: 0)
|
||||||
|
|> json_response_and_validate_schema(400)
|
||||||
|
|
||||||
|
conn
|
||||||
|
|> get("/api/v1/pleroma/admin/announcements", limit: -1)
|
||||||
|
|> json_response_and_validate_schema(400)
|
||||||
|
|
||||||
|
conn
|
||||||
|
|> get("/api/v1/pleroma/admin/announcements", offset: -1)
|
||||||
|
|> json_response_and_validate_schema(400)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "GET /api/v1/pleroma/admin/announcements/:id" do
|
describe "GET /api/v1/pleroma/admin/announcements/:id" do
|
||||||
|
Loading…
Reference in New Issue
Block a user