use of com.formkiq.lambda.apigateway.ApiResponseStatus in project formkiq-core by formkiq.
the class DocumentIdRequestHandler method patch.
@Override
public ApiRequestHandlerResponse patch(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
boolean isUpdate = event.getHttpMethod().equalsIgnoreCase("patch") && event.getPathParameters().containsKey("documentId");
String siteId = authorizer.getSiteId();
String documentId = UUID.randomUUID().toString();
if (isUpdate) {
documentId = event.getPathParameters().get("documentId");
if (awsservice.documentService().findDocument(siteId, documentId) == null) {
throw new NotFoundException("Document " + documentId + " not found.");
}
}
String maxDocumentCount = null;
DynamicObject item = fromBodyToDynamicObject(logger, event);
updateContentType(event, item);
List<DynamicObject> documents = item.getList("documents");
if (!isUpdate) {
if (!item.hasString("content") && item.getList("documents").isEmpty()) {
throw new BadException("Invalid JSON body.");
}
maxDocumentCount = this.restrictionMaxDocuments.getValue(awsservice, siteId);
if (maxDocumentCount != null && this.restrictionMaxDocuments.enforced(awsservice, siteId, maxDocumentCount)) {
throw new BadException("Max Number of Documents reached");
}
}
addFieldsToObject(event, awsservice, siteId, documentId, item, documents);
item.put("documents", documents);
logger.log("setting userId: " + item.getString("userId") + " contentType: " + item.getString("contentType"));
putObjectToStaging(logger, awsservice, maxDocumentCount, siteId, item);
Map<String, String> uploadUrls = generateUploadUrls(awsservice, siteId, documentId, item, documents);
Map<String, Object> map = buildResponse(siteId, documentId, documents, uploadUrls);
ApiResponseStatus status = isUpdate ? SC_OK : SC_CREATED;
return new ApiRequestHandlerResponse(status, new ApiMapResponse(map));
}
Aggregations