wmiys / api.alpha

Api for the the backend
0 stars 0 forks source link

Setup pagination #114

Closed rrickgauer closed 3 years ago

rrickgauer commented 3 years ago

Need to add pagination to any resource that returns more than 1 object in its response.

rrickgauer commented 3 years ago

Pagination includes the URL parameters:

To implement this, I'll need to utilize MySQL's LIMIT and OFFSET commands.

Calculating Limit and Offset

Limit is simple to calculate. It is equal to the value provided in the per_page url parameter.

Offset is a little more complicated. Since MySQL is 0-based index, this is how to calculate the offset:

OFFSET = (page - 1) * per_page
OFFSET = (3 - 1) * 20
OFFSET = 2 * 20
OFFSET = 40

Then the resulting MySQL statement would look like:

SELECT * FROM table_name LIMIT 20 OFFSET 40;
rrickgauer commented 3 years ago

For search products, have a default per_page of 20 and a max of 50