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
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ All requests must currently be sent via **GET** to https://api.spigotmc.org/simp

#### listResources
##### Parameters:
| name | type | required | default | description |
|---------- |------ |---------- |--------- |----------------------------------------------------------------------------- |
| category | int | no | none | The resource category to restrict results to (see `listResourceCategories`) |
| page | int | no | 1 | The page number to retrieve. Items are paginated at 10 results per page. |
| name | type | required | default | description |
|-------------|-----------|---------- |-------------|-------------------------------------------------------------------------------|
| category | int | no | none | The resource category to restrict results to (see `listResourceCategories`) |
| page | int | no | 1 | The page number to retrieve. Items are paginated at 10 results per page. |
| order | string | no | id | Field to order the results by. |
| direction | string | no | ASC | Direction to order the results by. |
##### Request: https://api.spigotmc.org/simple/0.2/index.php?action=listResources&category=4&page=2
##### Response (truncated):
```json
Expand Down Expand Up @@ -337,10 +339,12 @@ All requests must currently be sent via **GET** to https://api.spigotmc.org/simp

#### getResourceUpdates
##### Parameters:
| name | type | required | default | description |
|------ |------ |---------- |--------- |----------------------------------------------------------------------------- |
| name | type | required | default | description |
|------ |------ |---------- |--------- |-------------------------------------------------------------------------------|
| id | int | yes | none | The id of the resource for which to retrieve updates |
| page | int | no | 1 | The page number to retrieve. Items are paginated at 10 results per page. |
| page | int | no | 1 | The page number to retrieve. Items are paginated at 10 results per page. |
| order | string | no | id | Field to order the results by. |
| direction | string | no | ASC | Direction to order the results by. |
##### Request: https://api.spigotmc.org/simple/0.2/index.php?action=getResourceUpdates&id=2&page=1
##### Response (truncated):
```json
Expand Down
2 changes: 1 addition & 1 deletion src/controller/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function listResources()
{
$out = [];

$resources = $this->database->listResources(Req::category(), Req::page());
$resources = $this->database->listResources(Req::category(), Req::page(), Req::order(), Req::direction());

if (is_null($resources)) {
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/controller/ResourceUpdateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function getResourceUpdates()
$out = [];

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

foreach ($updates as $update) {
Expand Down
39 changes: 39 additions & 0 deletions src/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ paths:
required: false
schema:
type: integer
- name: order
description: Field to order the results by
in: query
required: false
schema:
type: string
enum:
- id
- first_release
- last_update
- download_count
- name: direction
description: Direction to order the results by
in: query
required: false
schema:
type: string
enum:
- asc
- desc
get:
operationId: listResources
summary: Obtain a list of all resources
Expand Down Expand Up @@ -120,6 +140,25 @@ paths:
required: false
schema:
type: integer
- name: order
description: Field to order the results by
in: query
required: false
schema:
type: string
enum:
- id
- post_date
- download_count
- name: direction
description: Direction to order the results by
in: query
required: false
schema:
type: string
enum:
- asc
- desc
get:
operationId: getResourceUpdates
summary: Obtain all the updates to a resource
Expand Down
44 changes: 36 additions & 8 deletions src/support/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ public static function initializeViaConfig()
);
}

public function listResources($category, $page)
public function listResources($category, $page, $order = null, $direction = null)
{
$page = $page == 1 ? 0 : 10 * ($page - 1);

if (!is_null($this->conn)) {
$categoryClause = is_null($category) ? '' : 'AND r.resource_category_id = :resource_category_id';
$resStmt = $this->_resource($categoryClause, 10, $page);
$resStmt = $this->_resource($categoryClause, 10, $page, $order, $direction);

if (!empty($categoryClause)) {
$resStmt->bindParam(':resource_category_id', $category);
Expand Down Expand Up @@ -140,12 +140,12 @@ public function getResourceUpdate($update_id)
return NULL;
}

public function getResourceUpdates($resource_id, $page)
public function getResourceUpdates($resource_id, $page, $order = null, $direction = null)
{
$page = $page == 1 ? 0 : 10 * ($page - 1);

if (!is_null($this->conn)) {
$updatesStmt = $this->_resource_update('AND r.resource_id = :resource_id', 10, $page);
$updatesStmt = $this->_resource_update('AND r.resource_id = :resource_id', 10, $page, $order, $direction);
$updatesStmt->bindParam(':resource_id', $resource_id);

if ($updatesStmt->execute()) {
Expand Down Expand Up @@ -213,8 +213,15 @@ public function findUser($username)
return NULL;
}

private function _resource($additional_where_clauses, $limit = 1, $offset = null)
private function _resource($additional_where_clauses, $limit = 1, $offset = null, $order = null, $direction = null)
{
$orderOptions = [
'id' => 'r.resource_id',
'first_release' => 'r.resource_date',
'last_update' => 'r.last_update',
'download_count' => 'r.download_count',
];
$orderClause = $this->prepare_order_options($orderOptions, $order, $direction);
$offsetClause = is_null($offset) ? '' : 'OFFSET :offset';

$query = sprintf(
Expand All @@ -228,10 +235,11 @@ private function _resource($additional_where_clauses, $limit = 1, $offset = null
ON r.resource_category_id = rc.resource_category_id
WHERE r.resource_state = 'visible'
%s
ORDER BY r.resource_id ASC
%s
LIMIT :limit
%s",
$additional_where_clauses,
$orderClause,
$offsetClause
);

Expand Down Expand Up @@ -269,8 +277,14 @@ private function _resource_fields($resource_id)
return NULL;
}

private function _resource_update($additional_where_clauses, $limit = 1, $offset = null)
private function _resource_update($additional_where_clauses, $limit = 1, $offset = null, $order = null, $direction = null)
{
$orderOptions = [
'id' => 'r.resource_update_id',
'post_date' => 'r.post_date',
'download_count' => 'rv.download_count',
];
$orderClause = $this->prepare_order_options($orderOptions, $order, $direction);
$offsetClause = is_null($offset) ? '' : 'OFFSET :offset';

$query = sprintf(
Expand All @@ -279,10 +293,11 @@ private function _resource_update($additional_where_clauses, $limit = 1, $offset
INNER JOIN xf_resource_version rv ON r.resource_update_id = rv.resource_update_id
WHERE r.message_state = 'visible' AND rv.version_state = 'visible'
%s
ORDER BY r.resource_update_id ASC
%s
LIMIT :limit
%s",
$additional_where_clauses,
$orderClause,
$offsetClause
);

Expand All @@ -295,4 +310,17 @@ private function _resource_update($additional_where_clauses, $limit = 1, $offset

return $stmt;
}

private function prepare_order_options($order_options, $order, $direction)
{
if (!empty($order) && array_key_exists($order, $order_options)) {
$column = $order_options[$order];
} else {
$column = array_shift($order_options);
}

$dir = strtolower($direction) === 'desc' ? 'DESC' : 'ASC';

return sprintf(' ORDER BY %s %s ', $column, $dir);
}
}
10 changes: 10 additions & 0 deletions src/util/RequestUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,14 @@ public static function category()

return NULL;
}

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

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