diff --git a/upload/modules/Resources/classes/Endpoints/ListResourcesEndpoint.php b/upload/modules/Resources/classes/Endpoints/ListResourcesEndpoint.php new file mode 100644 index 0000000..8037223 --- /dev/null +++ b/upload/modules/Resources/classes/Endpoints/ListResourcesEndpoint.php @@ -0,0 +1,64 @@ +_route = 'resources'; + $this->_module = 'Resources'; + $this->_description = 'List all resources'; + $this->_method = 'GET'; + } + + public function execute(Nameless2API $api): void { + $query = 'SELECT * FROM nl2_resources'; + $where = ' WHERE id <> 0'; + $limit = ''; + $params = []; + + if (isset($_GET['id'])) { + $where .= ' AND id = ?'; + array_push($params, $_GET['id']); + } + + if (isset($_GET['creator'])) { + $where .= ' AND creator_id = ?'; + array_push($params, $_GET['creator']); + } + + if (isset($_GET['category'])) { + $where .= ' AND category_id = ?'; + array_push($params, $_GET['category']); + } + + if (isset($_GET['limit']) && is_numeric($_GET['limit'])) { + $limit .= ' LIMIT '. $_GET['limit']; + } + + $resources_list = []; + $resources_query = $api->getDb()->query($query . $where . $limit, $params)->results(); + foreach ($resources_query as $resource) { + $author = new User($resource->creator_id); + + $resources_list[] = [ + 'id' => $resource->id, + 'name' => $resource->name, + 'description' => $resource->description, + 'author' => [ + 'id' => $resource->id, + 'username' => $author->exists() ? $author->getDisplayname(true) : $api->getLanguage()->get('general', 'deleted_user'), + ], + 'contributors' => $resource->contributors, + 'created' => $resource->created, + 'updated' => $resource->updated, + 'rating' => $resource->rating, + 'latest_version' => $resource->latest_version, + 'price' => $resource->price, + 'views' => $resource->views, + 'downloads' => $resource->downloads, + 'url' => URL::getSelfURL() . ltrim(URL::build('/resources/resource/' . $resource->id . '-' . URL::urlSafe($resource->name))), + ]; + } + + $api->returnArray(['resources' => $resources_list]); + } +} \ No newline at end of file diff --git a/upload/modules/Resources/init.php b/upload/modules/Resources/init.php index ccd61c3..bb8df9a 100644 --- a/upload/modules/Resources/init.php +++ b/upload/modules/Resources/init.php @@ -17,4 +17,4 @@ require_once ROOT_PATH . '/modules/Resources/hooks/DeleteUserResourcesHook.php'; require_once ROOT_PATH . '/modules/Resources/module.php'; -$module = new Resources_Module($pages, $language, $resource_language); +$module = new Resources_Module($pages, $language, $resource_language, $endpoints); diff --git a/upload/modules/Resources/module.php b/upload/modules/Resources/module.php index fc420d2..9da4fdb 100644 --- a/upload/modules/Resources/module.php +++ b/upload/modules/Resources/module.php @@ -12,7 +12,7 @@ class Resources_Module extends Module { private $_resource_language, $_language; - public function __construct($pages, $language, $resource_language){ + public function __construct($pages, $language, $resource_language, $endpoints){ $this->_resource_language = $resource_language; $this->_language = $language; @@ -44,6 +44,8 @@ public function __construct($pages, $language, $resource_language){ EventHandler::registerListener('deleteUser', 'DeleteUserResourcesHook::execute'); EventHandler::registerListener('cloneGroup', 'CloneGroupResourcesHook::execute'); + + $endpoints->loadEndpoints(ROOT_PATH . '/modules/Resources/classes/Endpoints'); }