From 113b1a5511198df54f06e64865fe4958aac1a58e Mon Sep 17 00:00:00 2001 From: MartenM Date: Fri, 21 Jan 2022 12:35:04 +0100 Subject: [PATCH 1/4] Added sorting to the getResourceUpdates action --- src/controller/ResourceUpdateController.php | 2 +- src/support/Database.php | 8 ++++++-- src/util/RequestUtil.php | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/controller/ResourceUpdateController.php b/src/controller/ResourceUpdateController.php index 92f4f67..61d045e 100644 --- a/src/controller/ResourceUpdateController.php +++ b/src/controller/ResourceUpdateController.php @@ -26,7 +26,7 @@ public function getResourceUpdates() { $out = array(); if (Req::checkIdParam()) { - $updates = $this->database->getResourceUpdates($_GET['id'], Req::page()); + $updates = $this->database->getResourceUpdates($_GET['id'], Req::page(), Req::sorting()); if (is_null($updates)) return NULL; foreach ($updates as $update) { diff --git a/src/support/Database.php b/src/support/Database.php index 0f71a84..a01e288 100644 --- a/src/support/Database.php +++ b/src/support/Database.php @@ -133,13 +133,17 @@ public function getResourceUpdate($update_id) { return NULL; } - public function getResourceUpdates($resource_id, $page) { + public function getResourceUpdates($resource_id, $page, $sorting = null) { $page = $page == 1 ? 0 : 10 * ($page - 1); + // Default sorting option for this method. + if($sorting == null) $sorting = 'asc'; + if (!is_null($this->conn)) { - $updatesStmt = $this->conn->prepare($this->_resource_update('AND r.resource_id = :resource_id LIMIT 10 OFFSET :offset')); + $updatesStmt = $this->conn->prepare($this->_resource_update('AND r.resource_id = :resource_id ORDER BY id :order LIMIT 10 OFFSET :offset')); $updatesStmt->bindParam(':resource_id', $resource_id); $updatesStmt->bindParam(':offset', $page, \PDO::PARAM_INT); + $updatesStmt->bindParam(':order', $sorting, \PDO::PARAM_STR); if ($updatesStmt->execute()) { return $updatesStmt->fetchAll(); diff --git a/src/util/RequestUtil.php b/src/util/RequestUtil.php index 8f24488..9d033a3 100644 --- a/src/util/RequestUtil.php +++ b/src/util/RequestUtil.php @@ -74,6 +74,20 @@ public static function page() { return 1; } + public static function sorting() { + $value = $_GET['sort'] ?? null; + + // Preconditions + if($value == null || !is_string($value)) return; + + // Sorting methods + if(strcasecmp($value, 'asc')) return 'asc'; + if(strcasecmp($value, 'desc')) return 'desc'; + + // Return default null + return NULL; + } + public static function category() { $value = $_GET['category'] ?? null; From 21f60466c71288e3843977c8180107a0a38bf51e Mon Sep 17 00:00:00 2001 From: MartenM Date: Fri, 21 Jan 2022 12:36:19 +0100 Subject: [PATCH 2/4] Added a comment --- src/util/RequestUtil.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/RequestUtil.php b/src/util/RequestUtil.php index 9d033a3..f3081c4 100644 --- a/src/util/RequestUtil.php +++ b/src/util/RequestUtil.php @@ -84,7 +84,7 @@ public static function sorting() { if(strcasecmp($value, 'asc')) return 'asc'; if(strcasecmp($value, 'desc')) return 'desc'; - // Return default null + // Return default null. This allows different defaults per method. return NULL; } From 0b5d94dba86b85cacce42b226fdc85b05083b1d0 Mon Sep 17 00:00:00 2001 From: Jacob Andersen Date: Thu, 3 Nov 2022 22:28:34 -0700 Subject: [PATCH 3/4] Fixup and test PR. Working as expected. --- src/support/Database.php | 8 +++++--- src/util/RequestUtil.php | 9 ++++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/support/Database.php b/src/support/Database.php index a01e288..e6990da 100644 --- a/src/support/Database.php +++ b/src/support/Database.php @@ -137,13 +137,15 @@ public function getResourceUpdates($resource_id, $page, $sorting = null) { $page = $page == 1 ? 0 : 10 * ($page - 1); // Default sorting option for this method. - if($sorting == null) $sorting = 'asc'; + if (is_null($sorting)) $sorting = 'asc'; if (!is_null($this->conn)) { - $updatesStmt = $this->conn->prepare($this->_resource_update('AND r.resource_id = :resource_id ORDER BY id :order LIMIT 10 OFFSET :offset')); + // PDO tries to quote the sorting method. Can't bind it normally. Should be OK, sorting is enforced to be 'asc' or 'desc'. + $querySuffix = sprintf("AND r.resource_id = :resource_id ORDER BY r.resource_update_id %s LIMIT 10 OFFSET :offset", $sorting); + + $updatesStmt = $this->conn->prepare($this->_resource_update($querySuffix)); $updatesStmt->bindParam(':resource_id', $resource_id); $updatesStmt->bindParam(':offset', $page, \PDO::PARAM_INT); - $updatesStmt->bindParam(':order', $sorting, \PDO::PARAM_STR); if ($updatesStmt->execute()) { return $updatesStmt->fetchAll(); diff --git a/src/util/RequestUtil.php b/src/util/RequestUtil.php index f3081c4..1abed7f 100644 --- a/src/util/RequestUtil.php +++ b/src/util/RequestUtil.php @@ -78,11 +78,14 @@ public static function sorting() { $value = $_GET['sort'] ?? null; // Preconditions - if($value == null || !is_string($value)) return; + if (is_null($value) || !is_string($value)) return; // Sorting methods - if(strcasecmp($value, 'asc')) return 'asc'; - if(strcasecmp($value, 'desc')) return 'desc'; + if(strcasecmp($value, 'asc') == 0) { + return 'asc'; + } else if (strcasecmp($value, 'desc') == 0) { + return 'desc'; + } // Return default null. This allows different defaults per method. return NULL; From 2b6c91a9ebbf53e87c6a021c777eeaece67c1d8d Mon Sep 17 00:00:00 2001 From: Marten Date: Wed, 15 Feb 2023 15:54:44 +0100 Subject: [PATCH 4/4] Fixed a non-value return value --- src/util/RequestUtil.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/util/RequestUtil.php b/src/util/RequestUtil.php index 1abed7f..3fa406b 100644 --- a/src/util/RequestUtil.php +++ b/src/util/RequestUtil.php @@ -75,10 +75,10 @@ public static function page() { } public static function sorting() { - $value = $_GET['sort'] ?? null; + $value = $_GET['sort'] ?? NULL; // Preconditions - if (is_null($value) || !is_string($value)) return; + if (is_null($value) || !is_string($value)) return NULL; // Sorting methods if(strcasecmp($value, 'asc') == 0) {