Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added sorting to the getResourceUpdates action #64

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/controller/ResourceUpdateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 8 additions & 2 deletions src/support/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +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 (is_null($sorting)) $sorting = 'asc';

if (!is_null($this->conn)) {
$updatesStmt = $this->conn->prepare($this->_resource_update('AND r.resource_id = :resource_id 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);

Expand Down
17 changes: 17 additions & 0 deletions src/util/RequestUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,23 @@ public static function page() {
return 1;
}

public static function sorting() {
$value = $_GET['sort'] ?? NULL;

// Preconditions
if (is_null($value) || !is_string($value)) return NULL;

// Sorting methods
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;
}

public static function category() {
$value = $_GET['category'] ?? null;

Expand Down