From 35d81b57e20f6d9843ced574728add271d01e6f9 Mon Sep 17 00:00:00 2001 From: Nolan Ehrstrom Date: Thu, 13 Jun 2024 12:12:18 -0700 Subject: [PATCH] Add query-based pagination to tasks controller --- .../Http/Controllers/Api/TaskController.php | 28 +++----------- .../Traits/TaskControllerIndexMethods.php | 37 ------------------- 2 files changed, 5 insertions(+), 60 deletions(-) diff --git a/ProcessMaker/Http/Controllers/Api/TaskController.php b/ProcessMaker/Http/Controllers/Api/TaskController.php index 993b97f4a8..7312a872b2 100644 --- a/ProcessMaker/Http/Controllers/Api/TaskController.php +++ b/ProcessMaker/Http/Controllers/Api/TaskController.php @@ -122,47 +122,29 @@ public function index(Request $request, $getTotal = false, User $user = null) $this->applyForCurrentUser($query, $user); - try { - // Count up the total number of results, but - // watch for PMQL query exceptions and handle - // them if they occur - $totalResultCount = $query->count(); - } catch (QueryException $e) { - return $this->handleQueryException($e); - } - - // If only the total is being requested (by a Saved Search), send it now - if ($getTotal === true) { - return $totalResultCount; - } - // Apply filter overdue $query->overdue($request->input('overdue')); - // If we should manually add pagination to the - // query in advance (also used by saved search) - if ($this->isPaginationEnabled()) { - $query->limit($request->input('per_page', 10)); + // If only the total is being requested (by a Saved Search), send it now + if ($getTotal === true) { + return $query->count(); } try { - $response = $query->get(); + $response = $query->paginate($request->input('per_page', 10)); } catch (QueryException $e) { return $this->handleQueryException($e); } $response = $this->applyUserFilter($response, $request, $user); - // Map each item through its resource - $response = $this->applyResource($response); - $inOverdueQuery = ProcessRequestToken::query() ->whereIn('id', $response->pluck('id')) ->where('due_at', '<', Carbon::now()); $response->inOverdue = $inOverdueQuery->count(); - return new TaskCollection($response, $totalResultCount); + return new TaskCollection($response); } /** diff --git a/ProcessMaker/Traits/TaskControllerIndexMethods.php b/ProcessMaker/Traits/TaskControllerIndexMethods.php index a745c46730..3a49a4e105 100644 --- a/ProcessMaker/Traits/TaskControllerIndexMethods.php +++ b/ProcessMaker/Traits/TaskControllerIndexMethods.php @@ -15,36 +15,6 @@ trait TaskControllerIndexMethods { - /** - * Manually enable paginated results from the - * index method() - * - * @return void - */ - public function enableIndexPagination(): void - { - static::$paginate = true; - } - - /** - * Determine if pagination was manually set for the - * index() method results - * - * @return bool - */ - public function isPaginationEnabled(): bool - { - return static::$paginate === true; - } - - /** - * Used by saved search to paginate the results - * from the index() method - * - * @var bool - */ - protected static bool $paginate = false; - private function indexBaseQuery($request) { $query = ProcessRequestToken::with(['processRequest', 'user', 'draft']); @@ -300,13 +270,6 @@ private function applyUserFilter($response, $request, $user) return $response; } - private function applyResource($response) - { - return $response->map(function ($processRequestToken) { - return new Resource($processRequestToken); - }); - } - private function applyForCurrentUser($query, $user) { if ($user->is_administrator) {