Task: Pagination API

Tags

Microservice, Backend

Description

When you visit a website like Facebook or Instagram, you are like visiting an infinite feed of information. Have you thought about how is it possible to load the website that quickly while it has so much data?

This is done with pagination. In the initial load of the website, you only download the first page of data. Usually, that's the first N items. When you continue to scroll down, it starts to load another N items for the second page, then the third page if you keep scrolling... By using pagination, websites can avoid loading unnecessary information to make the initial load fast!

In this task, you are asked to implement a cursor-based pagination API with the given data. With cursor-based pagination it can handle different cases like deletion and insertion elegantly.

Specification

Configuration

configSource: "env"

# The port number the server listens on
PORT=8080
# The size of each page (N in the above description)
PAGE_SIZE=10

The provided data is given in db.json. Please use this dataset (load it to DB or use it directly) to return results for testing.

GET /logs

The endpoint should return a 200 OK with 3 keys:

  • results - an array of JSON objects provided, ordered by the given id descending order
  • next - an URL to the next page, null if there's no more data
  • previous - an URL to the previous page, null if there's no more data before

A 400 should be returned if anything goes wrong.

Example

For demonstration, we use the following config: { "port": 8080, "pageSize": 2 }.

$ curl 'http://localhost:8080/logs'
{"next":"http://localhost:8080/logs/?cursor=cD0xMDA%3D","previous":null,"results":[{"name":"name99","message":"Hello world 99!","time":"2019-05-04T00:51:14.661000Z"},{"name":"name98","message":"Hello world 98!","time":"2019-05-04T00:51:14.660000Z"}]}
$ curl 'http://localhost:8080/logs/?cursor=cD0xMDA%3D'
{"next":"http://localhost:8080/logs/?cursor=cD05OA%3D%3D","previous":"http://localhost:8080/logs/?cursor=cj0xJnA9OTk%3D","results":[{"name":"name97","message":"Hello world 97!","time":"2019-05-04T00:51:14.658000Z"},{"name":"name96","message":"Hello world 96!","time":"2019-05-04T00:51:14.657000Z"}]}
Instructions
Get started
My Submissions

Submit to see your result!

Leaderboard

Nobody here yet, solve it now to get on the board!