Skip to content

Commit

Permalink
Paginate runs on BE
Browse files Browse the repository at this point in the history
  • Loading branch information
michalwarda committed Feb 22, 2024
1 parent f16503a commit 39eb126
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 39eb126

Please sign in to comment.