am planning to implement paging using cosmos db continuation token. My api will return results and continuation token to client. My question here is which is the best place to store continuation token? Also the token will be changing per each subsequent request? How to maintain the previous continousTokens?
Here it shows where we get the value from and not where to store it How to pass\user azure continue token via webAPI Pagination in Cosmos DB using Page Size and Page Number
controller.cs
[Route("myApps")]
[HttpGet]
public async Task<IActionResult> GetAllAppsAsync(string continuationToken, CancellationToken cancellationToken)
{
var user = this.GetUser();
var results = await this.appRepository.GetAppsForUserAsync(user, continuationToken, cancellationToken).ConfigureAwait(false);
var result = this.mapper.Map<AppHeader[]>(results.Value);
return this.Ok(new KeyValuePair<string, AppDefinitionHeader[]>(results.Key, result));
}
Repository.cs
public async Task<KeyValuePair<string, IEnumerable<App>>> GetAppForUserAsync(User user, string continuationToken, CancellationToken cancellationToken)
{
try
{
FeedOptions queryOptions = new FeedOptions
{
MaxItemCount = 2,
RequestContinuation = continuationToken
};
string token = string.Empty;
var query = this.factory.GetClient()
.CreateDocumentQuery<AppDefinitionResource>(
UriFactory.CreateDocumentCollectionUri(DatabaseName, CollectionName),
queryOptions)
.AsDocumentQuery();
List<AppDefinition> results = new List<AppDefinition>();
while (query.HasMoreResults && results.Count <= 2)
{
cancellationToken.ThrowIfCancellationRequested();
var response = await query.ExecuteNextAsync<App>(cancellationToken).ConfigureAwait(false);
var apps = this.mapper.Map<App[]>(response);
results.AddRange(apps);
token = response.ResponseContinuation;
}
return new KeyValuePair<string, IEnumerable<App>>(token, results);
}
}
Update :
How to maintain the previous continousTokens?
Ex : If there are 100 records and pageSize=10 and pageNumbers are 1,2,3,4,5,6,7,8,9,10 and if user clicks randomly on any page. In that case how to fetch required records? or just if I place "Previous" button in the below screenshot and want to traverse back in the list.