Skip to content
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
20 changes: 10 additions & 10 deletions src/controller/AuthorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ public function __construct($database)

public function getAuthor()
{
if (Req::checkIdParam()) {
$author = $this->database->getUser($_GET['id']);
if (!is_null($author) && $author !== false) {
return new Author($author);
}
Req::checkIdParam();

$author = $this->database->getUser(Req::id());
if (!is_null($author) && $author !== false) {
return new Author($author);
}

return NULL;
}

public function findAuthor()
{
if (Req::checkNameParam()) {
$author = $this->database->findUser($_GET['name']);
if (!is_null($author) && $author !== false) {
return new Author($author);
}
Req::checkNameParam();

$author = $this->database->findUser(Req::name());
if (!is_null($author) && $author !== false) {
return new Author($author);
}

return NULL;
Expand Down
27 changes: 12 additions & 15 deletions src/controller/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,28 @@ public function listResources()

public function getResource()
{
if (Req::checkIdParam()) {
$resource = $this->database->getResource(Req::id());
Req::checkIdParam();

if (!is_null($resource) && $resource !== false) {
return new Resource($resource);
}
$resource = $this->database->getResource(Req::id());
if (!is_null($resource) && $resource !== false) {
return new Resource($resource);
}

return NULL;
}

public function getResourcesByAuthor()
{
$out = [];

if (Req::checkIdParam()) {
$resources = $this->database->getResourcesByUser(Req::id(), Req::page());
Req::checkIdParam();

if (is_null($resources)) {
return NULL;
}
$resources = $this->database->getResourcesByUser(Req::id(), Req::page());
if (is_null($resources)) {
return NULL;
}

foreach ($resources as $resource) {
$out[] = new Resource($resource);
}
$out = [];
foreach ($resources as $resource) {
$out[] = new Resource($resource);
}

return $out;
Expand Down
25 changes: 13 additions & 12 deletions src/controller/ResourceUpdateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,28 @@ public function __construct($database)

public function getResourceUpdate()
{
if (Req::checkIdParam()) {
$update = $this->database->getResourceUpdate($_GET['id']);
if (!is_null($update) && $update !== false) {
return new ResourceUpdate($update);
}
Req::checkIdParam();

$update = $this->database->getResourceUpdate(Req::id());
if (!is_null($update) && $update !== false) {
return new ResourceUpdate($update);
}

return NULL;
}

public function getResourceUpdates()
{
$out = [];
Req::checkIdParam();

if (Req::checkIdParam()) {
$updates = $this->database->getResourceUpdates($_GET['id'], Req::page());
if (is_null($updates)) return NULL;
$updates = $this->database->getResourceUpdates(Req::id(), Req::page());
if (is_null($updates)) {
return NULL;
}

foreach ($updates as $update) {
$out[] = new ResourceUpdate($update);
}
$out = [];
foreach ($updates as $update) {
$out[] = new ResourceUpdate($update);
}

return $out;
Expand Down
16 changes: 12 additions & 4 deletions src/util/RequestUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public static function id()
return $_GET['id'] ?? null;
}

/**
* Checks if the 'id' parameter is present and valid.
* Exits with an error message if not.
*
* @return void
*/
public static function checkIdParam()
{
$id = self::id();
Expand All @@ -42,15 +48,19 @@ public static function checkIdParam()
echo new Error(400, "Invalid ID. ID must be numeric.");
exit();
}

return true;
}

public static function name()
{
return $_GET['name'] ?? null;
}

/**
* Checks if the 'name' parameter is present and valid.
* Exits with an error message if not.
*
* @return void
*/
public static function checkNameParam()
{
$name = self::name();
Expand All @@ -66,8 +76,6 @@ public static function checkNameParam()
echo new Error(400, "Invalid name. Name must be at 3-24 characters in length and consist of letters, numbers, and/or a limited set of special characters (_, -, ., and/or one or more spaces).");
exit();
}

return true;
}

public static function page()
Expand Down