apavlidi / IT_API

The Internet services of the IT department of Alexander Technological Education Institute of Thessaloniki
http://api.it.teithe.gr/
MIT License
14 stars 5 forks source link

Announcements: Improved pagination metadata #98

Open theokyr opened 4 years ago

theokyr commented 4 years ago

This PR modifies the responses of the following endpoints:

Previously, the API would respond to these requests like this:

[
  {
    "date": "2020-07-07T20:55:17.940Z",
    "attachments": [],
    ...
  }
]
With this PR, these responses have a new structure: Attribute Description
results Array containing the paginated results
resultCount The amount of results returned
page The zero indexed number of the returned page
totalResultCount The total amount of results that match this query
totalPages The total amount of pages available given the pageSize specified

Example: GET /announcements/public?pageSize=1&page=3

{
  "results": [
    {
      "date": "2020-07-07T20:55:17.398Z",
      "attachments": [],
       ...
    }
  ],
  "resultCount": 1,
  "page": 3,
  "totalResultCount": 12,
  "totalPages": 12
}

Because these changes were made possible using an additional query to get the resulting metadata, the following error codes have been added to handle exceptions during its execution:

Error Code Meaning
1001 Failure getting count of Announcements
1012 Failure getting count of Public Announcements

I am aware these could be breaking changes for these essential routes, but consuming the API using pagination becomes much easier when more pagination metadata are available. Result filtering, selecting, and sorting are fully functional with this PR.