Search in sources :

Example 1 with DynamoDbCacheService

use of com.formkiq.stacks.dynamodb.DynamoDbCacheService 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);
}
Also used : ApiPagination(com.formkiq.lambda.apigateway.ApiPagination) QueryRequest(com.formkiq.stacks.api.QueryRequest) HashMap(java.util.HashMap) BadException(com.formkiq.lambda.apigateway.exception.BadException) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DynamoDbCacheService(com.formkiq.stacks.dynamodb.DynamoDbCacheService) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse) PaginationMapToken(com.formkiq.stacks.dynamodb.PaginationMapToken)

Aggregations

ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)1 ApiPagination (com.formkiq.lambda.apigateway.ApiPagination)1 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)1 BadException (com.formkiq.lambda.apigateway.exception.BadException)1 QueryRequest (com.formkiq.stacks.api.QueryRequest)1 DynamicDocumentItem (com.formkiq.stacks.dynamodb.DynamicDocumentItem)1 DynamoDbCacheService (com.formkiq.stacks.dynamodb.DynamoDbCacheService)1 PaginationMapToken (com.formkiq.stacks.dynamodb.PaginationMapToken)1 HashMap (java.util.HashMap)1