From 39eb126ee6ee844db23e7be1582b954f58c13b6b Mon Sep 17 00:00:00 2001 From: Michal Warda Date: Thu, 22 Feb 2024 12:34:19 +0100 Subject: [PATCH] Paginate runs on BE --- .../pipelines/runs/run_controller.ex | 23 +++++++++++++++---- .../organizations/pipelines/runs/run_json.ex | 14 +++++++++-- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/apps/api/lib/buildel_web/controllers/organizations/pipelines/runs/run_controller.ex b/apps/api/lib/buildel_web/controllers/organizations/pipelines/runs/run_controller.ex index 36a0d73fa..6a6aab5d5 100644 --- a/apps/api/lib/buildel_web/controllers/organizations/pipelines/runs/run_controller.ex +++ b/apps/api/lib/buildel_web/controllers/organizations/pipelines/runs/run_controller.ex @@ -14,18 +14,33 @@ defmodule BuildelWeb.OrganizationPipelineRunController do plug(:fetch_current_user) plug(:require_authenticated_user) - def index(conn, %{"organization_id" => organization_id, "pipeline_id" => pipeline_id}) do + defparams :index do + required(:page, :integer) + required(:per_page, :integer) + end + + @default_params %{ + "page" => 0, + "per_page" => 10 + } + + def index(conn, %{"organization_id" => organization_id, "pipeline_id" => pipeline_id} = params) do + params = Map.merge(@default_params, params) user = conn.assigns.current_user - with {:ok, organization} <- Organizations.get_user_organization(user, organization_id), + with {:ok, params} <- validate(:index, params), + {:ok, organization} <- Organizations.get_user_organization(user, organization_id), {:ok, %Pipeline{} = pipeline} <- Pipelines.get_organization_pipeline(organization, pipeline_id), runs <- Pipelines.list_pipeline_runs(pipeline) do - render(conn, :index, runs: runs) + render(conn, :index, runs: runs, pagination_params: params) end end - def show(conn, %{"organization_id" => organization_id, "pipeline_id" => pipeline_id, "id" => id}) do + def show( + conn, + %{"organization_id" => organization_id, "pipeline_id" => pipeline_id, "id" => id} + ) do user = conn.assigns.current_user with {:ok, organization} <- Organizations.get_user_organization(user, organization_id), diff --git a/apps/api/lib/buildel_web/controllers/organizations/pipelines/runs/run_json.ex b/apps/api/lib/buildel_web/controllers/organizations/pipelines/runs/run_json.ex index 76bd630c5..8ffb823df 100644 --- a/apps/api/lib/buildel_web/controllers/organizations/pipelines/runs/run_json.ex +++ b/apps/api/lib/buildel_web/controllers/organizations/pipelines/runs/run_json.ex @@ -1,8 +1,18 @@ defmodule BuildelWeb.OrganizationPipelineRunJSON do alias Buildel.Pipelines.Run - def index(%{runs: runs}) do - %{data: for(run <- runs, do: data(run))} + def index(%{runs: runs, pagination_params: pagination_params}) do + %{ + data: + for(run <- runs, do: data(run)) + |> Enum.drop(pagination_params.page * pagination_params.per_page) + |> Enum.take(pagination_params.per_page), + meta: %{ + total: Enum.count(runs), + page: pagination_params.page, + per_page: pagination_params.per_page + } + } end def show(%{run: run}) do