use of com.formkiq.lambda.apigateway.ApiPagination in project formkiq-core by formkiq.
the class DocumentsRequestHandler method get.
@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
ApiPagination pagination = getPagination(awsservice.documentCacheService(), event);
final int limit = pagination != null ? pagination.getLimit() : getLimit(logger, event);
final PaginationMapToken ptoken = pagination != null ? pagination.getStartkey() : null;
String tz = getParameter(event, "tz");
String dateString = getParameter(event, "date");
if (StringUtils.isBlank(dateString)) {
if (StringUtils.isNotBlank(tz)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
ZoneOffset offset = DateUtil.getZoneOffset(tz);
sdf.setTimeZone(TimeZone.getTimeZone(offset));
dateString = sdf.format(new Date());
} else {
dateString = this.df.format(new Date());
}
}
ZonedDateTime date = transformToDate(logger, awsservice, dateString, tz);
String siteId = authorizer.getSiteId();
final PaginationResults<DocumentItem> results = awsservice.documentService().findDocumentsByDate(siteId, date, ptoken, limit);
ApiPagination current = createPagination(awsservice.documentCacheService(), event, pagination, results.getToken(), limit);
List<DocumentItem> documents = subList(results.getResults(), limit);
List<DynamicDocumentItem> items = documents.stream().map(m -> new DocumentItemToDynamicDocumentItem().apply(m)).collect(Collectors.toList());
items.forEach(i -> i.put("siteId", siteId != null ? siteId : DEFAULT_SITE_ID));
Map<String, Object> map = new HashMap<>();
map.put("documents", items);
map.put("previous", current.getPrevious());
map.put("next", current.hasNext() ? current.getNext() : null);
return new ApiRequestHandlerResponse(SC_OK, new ApiMapResponse(map));
}
use of com.formkiq.lambda.apigateway.ApiPagination in project formkiq-core by formkiq.
the class SearchRequestHandler method post.
@Override
public ApiRequestHandlerResponse post(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
DynamoDbCacheService cacheService = awsservice.documentCacheService();
ApiPagination pagination = getPagination(cacheService, event);
int limit = pagination != null ? pagination.getLimit() : getLimit(logger, event);
PaginationMapToken ptoken = pagination != null ? pagination.getStartkey() : null;
QueryRequest q = fromBodyToObject(logger, event, QueryRequest.class);
if (q == null || q.query() == null || q.query().tag() == null) {
throw new BadException("Invalid JSON body.");
}
Collection<String> documentIds = q.query().documentIds();
if (documentIds != null) {
if (documentIds.size() > MAX_DOCUMENT_IDS) {
throw new BadException("Maximum number of DocumentIds is " + MAX_DOCUMENT_IDS);
}
if (!getQueryParameterMap(event).containsKey("limit")) {
limit = documentIds.size();
}
}
String siteId = authorizer.getSiteId();
PaginationResults<DynamicDocumentItem> results = awsservice.documentSearchService().search(siteId, q.query(), ptoken, limit);
ApiPagination current = createPagination(cacheService, event, pagination, results.getToken(), limit);
List<DynamicDocumentItem> documents = subList(results.getResults(), limit);
Map<String, Object> map = new HashMap<>();
map.put("documents", documents);
map.put("previous", current.getPrevious());
map.put("next", current.hasNext() ? current.getNext() : null);
ApiMapResponse resp = new ApiMapResponse(map);
return new ApiRequestHandlerResponse(SC_OK, resp);
}
Aggregations