Add pagination to AdminAPI.AnnouncementController.index

This commit is contained in:
Tusooa Zhu 2022-03-08 21:26:05 -05:00
parent 11a1996bf5
commit eb1a29640f
No known key found for this signature in database
GPG key ID: 7B467EDE43A08224
4 changed files with 78 additions and 2 deletions

View file

@ -61,6 +61,13 @@ defmodule Pleroma.Announcement do
|> Repo.all()
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
Repo.get_by(__MODULE__, id: id)
end

View file

@ -16,8 +16,13 @@ defmodule Pleroma.Web.AdminAPI.AnnouncementController do
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.AnnouncementOperation
def index(conn, _params) do
announcements = Announcement.list_all()
defp default_limit, do: 20
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)
end

View file

@ -21,8 +21,24 @@ defmodule Pleroma.Web.ApiSpec.Admin.AnnouncementOperation do
summary: "Retrieve a list of announcements",
operationId: "AdminAPI.AnnouncementController.index",
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: %{
200 => Operation.response("Response", "application/json", list_of_announcements()),
400 => Operation.response("Forbidden", "application/json", ApiError),
403 => Operation.response("Forbidden", "application/json", ApiError)
}
}

View file

@ -30,6 +30,54 @@ defmodule Pleroma.Web.AdminAPI.AnnouncementControllerTest do
assert [%{"id" => ^id}] = response
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
describe "GET /api/v1/pleroma/admin/announcements/:id" do