CollActionteam / collaction_backend

Backend code for CollAction
6 stars 4 forks source link

Pagination for crowdactions #84

Open rubenhorn opened 2 years ago

rubenhorn commented 2 years ago

From Jira CAN-99:

Currently, there is no pagination needed as the list of actions is small but hopefully, there will be a lot of them so pagination will be mandatory in the end https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.Pagination.html

Outline:
New query parameter page: e.g. api-dev.collaction.org/crowdactions?status=ended&page=eyJwayI6ImFjdCIsInNrIjoic3VzdGFpbmFiaWxpdHkjZm9vZCN2ZWdhbk1vbnRoMjAtMTIifQ==

New response:

{
  data: [...], // The list of crowdactions returned by the backend
  next_page: "eyJwayI6ImFjdCIsInNrIjoic3VzdGFpbmFiaWxpdHkjZm9vZCN2ZWdhbk1vbnRoMjAtMTIifQ==" // Next page key as Base64 or null
}

Obtaining value for next_page to return from models/crowdactions.go#listCrowdactions:

  1. Variable startFrom is of type utils.PrimaryKey (aka map[string]*dynamodb.AttributeValue)
  2. If this variable is null or empty, return null
  3. Marshal this variable as a JSON string (avoid optional whitespace characters. E.g. {"pk":"act","sk":"sustainability#food#veganMonth20-12"})
  4. Convert JSON string to base64 Obtain parameter startFrom for models/crowdactions.go#listCrowdactions: (See above, but just in reverse)

Shorter page IDs:
Instead of converting the primary key to a JSON file, concatenate pk and sk and obtain the base64 string which should reduce the length of the generated page ID. Here is an example using the same primary key:
JSON → eyJwayI6ImFjdCIsInNrIjoic3VzdGFpbmFiaWxpdHkjZm9vZCN2ZWdhbk1vbnRoMjAtMTIifQ==
pk.skYWN0LnN1c3RhaW5hYmlsaXR5I2Zvb2QjdmVnYW5Nb250aDIwLTEy