Search in sources :

Example 1 with DocumentTags

use of com.formkiq.stacks.dynamodb.DocumentTags in project formkiq-core by formkiq.

the class DocumentTagsRequestHandler method post.

@Override
public ApiRequestHandlerResponse post(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    DocumentTag tag = fromBodyToObject(logger, event, DocumentTag.class);
    DocumentTags tags = fromBodyToObject(logger, event, DocumentTags.class);
    boolean tagValid = isValid(tag);
    boolean tagsValid = isValid(tags);
    if (!tagValid && !tagsValid) {
        throw new BadException("invalid json body");
    }
    if (!tagsValid) {
        tags = new DocumentTags();
        tags.setTags(Arrays.asList(tag));
    }
    tags.getTags().forEach(t -> {
        t.setType(DocumentTagType.USERDEFINED);
        t.setInsertedDate(new Date());
        t.setUserId(getCallingCognitoUsername(event));
    });
    String documentId = event.getPathParameters().get("documentId");
    String siteId = authorizer.getSiteId();
    awsservice.documentService().deleteDocumentTag(siteId, documentId, "untagged");
    awsservice.documentService().addTags(siteId, documentId, tags.getTags(), null);
    ApiResponse resp = tagsValid ? new ApiMessageResponse("Created Tags.") : new ApiMessageResponse("Created Tag '" + tag.getKey() + "'.");
    return new ApiRequestHandlerResponse(SC_CREATED, resp);
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiMessageResponse(com.formkiq.lambda.apigateway.ApiMessageResponse) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) BadException(com.formkiq.lambda.apigateway.exception.BadException) Date(java.util.Date) ApiResponse(com.formkiq.lambda.apigateway.ApiResponse) DocumentTags(com.formkiq.stacks.dynamodb.DocumentTags)

Aggregations

ApiMessageResponse (com.formkiq.lambda.apigateway.ApiMessageResponse)1 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)1 ApiResponse (com.formkiq.lambda.apigateway.ApiResponse)1 BadException (com.formkiq.lambda.apigateway.exception.BadException)1 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)1 DocumentTags (com.formkiq.stacks.dynamodb.DocumentTags)1 Date (java.util.Date)1