Future<Response>
getObjects({int count: 0, int offset: 0, String pageBy: null, String pageAfter: null, String pagePrior: null, List<String> sortBy: null })
Source
@httpGet
Future<Response> getObjects(
{@HTTPQuery("count") int count: 0,
@HTTPQuery("offset") int offset: 0,
@HTTPQuery("pageBy") String pageBy: null,
@HTTPQuery("pageAfter") String pageAfter: null,
@HTTPQuery("pagePrior") String pagePrior: null,
@HTTPQuery("sortBy") List<String> sortBy: null}) async {
_query.fetchLimit = count;
_query.offset = offset;
if (pageBy != null) {
var direction;
var pageValue;
if (pageAfter != null) {
direction = QuerySortOrder.ascending;
pageValue = pageAfter;
} else if (pagePrior != null) {
direction = QuerySortOrder.descending;
pageValue = pagePrior;
} else {
return new Response.badRequest(body: {
"error":
"If defining pageBy, either pageAfter or pagePrior must be defined. 'null' is a valid value"
});
}
var pageByProperty = _query.entity.properties[pageBy];
if (pageByProperty == null) {
throw new HTTPResponseException(400,
"pageBy key $pageBy does not exist for ${_query.entity.tableName}");
}
pageValue = _parseValueForProperty(pageValue, pageByProperty);
_query.pageBy((t) => t[pageBy], direction,
boundingValue: pageValue == "null" ? null : pageValue);
}
if (sortBy != null) {
sortBy.forEach((sort) {
var split = sort.split(",").map((str) => str.trim()).toList();
if (split.length != 2) {
throw new HTTPResponseException(400,
"sortBy keys must be string pairs delimited by a comma: key,asc or key,desc");
}
if (_query.entity.properties[split.first] == null) {
throw new HTTPResponseException(400,
"sortBy key ${split.first} does not exist for ${_query.entity.tableName}");
}
if (split.last != "asc" && split.last != "desc") {
throw new HTTPResponseException(400,
"sortBy order must be either asc or desc, not ${split.last}");
}
var sortOrder = split.last == "asc"
? QuerySortOrder.ascending
: QuerySortOrder.descending;
_query.sortBy((t) => t[split.first], sortOrder);
});
}
_query = await willFindObjectsWithQuery(_query);
var results = await _query?.fetch();
return didFindObjects(results);
}