Add order and direction parameters to listResources and getResourceUpdates#105
Add order and direction parameters to listResources and getResourceUpdates#105pavog wants to merge 1 commit into
order and direction parameters to listResources and getResourceUpdates#105Conversation
|
While I agree ordering is important to the API, I would suggest exposing specific order fields & directions rather than allowing user to select the field and direction themselves. In your code you're basically already doing this - to be clear, what I'm suggesting is instead of 2 params make just one called something like sort and expose specific values like
Anyway, this is just a suggestion. Thoughts @md-5 ? |
|
Unless I'm missing something isn't that really the same thing? Given that the first half will always be a key and the second half will always be asc or desc? I think this PR also already uses the API names (eg first_release) rather than the internal database columns. Is there any other benefit of doing it as you suggest? |
|
Just looked closer and, yeah, it's already fine. Disregard my suggestion. |
|
Thanks. Do you have any comments/review on the code itself? |
This PR adds the two parameters:
orderanddirection.These two parameters can be used with the actions:
listResourcesandgetResourceUpdates.orderspecifies the database field by which to sort, e.g.,id,post_date, ordownload_count.directionspecifies the sort order:ascfor ascending (e.g., for IDs) ordescfor descending (e.g., for dates such aspost_date).For the implementation, I took inspiration from the XenForo Resource Manager plugin. There, order options are also defined for each model using key-value pairs, as shown here, for example:
By checking with
array_key_exists()and retrieving the value from the arraywe ensure that no SQL injection can occur via the
orderparameter.If
orderis not specified or contains an invalid value,array_shift()is used to take the first entry from theorder_optionsarray. In both of these cases, it is the ID.For
direction, it’s even simpler: The functions checks whetherdirectionisdescotherwise, it defaults to
'asc', so ascending order is the default.Overall, this pull request adds the very useful feature to sort by IDs or dates (e.g.,
last_update). That would be very helpful for our purposes.