Search in sources :

Example 1 with DeleteVisitor

use of com.yahoo.elide.jsonapi.parser.DeleteVisitor in project elide by yahoo.

the class JsonApiPatch method handleRemoveOp.

/**
 * Remove data via patch extension.
 */
private Supplier<Pair<Integer, JsonNode>> handleRemoveOp(String path, JsonNode patchValue, PatchRequestScope requestScope) {
    try {
        JsonApiDocument value = requestScope.getMapper().readJsonApiPatchExtValue(patchValue);
        String fullPath;
        if (path.contains("relationships")) {
            // Reserved keyword for relationships
            fullPath = path;
        } else {
            Data<Resource> data = value.getData();
            if (data == null || data.get() == null) {
                fullPath = path;
            } else {
                Collection<Resource> resources = data.get();
                String id = getSingleResource(resources).getId();
                fullPath = path + "/" + id;
            }
        }
        DeleteVisitor visitor = new DeleteVisitor(new PatchRequestScope(path, value, requestScope));
        return visitor.visit(JsonApiParser.parse(fullPath));
    } catch (IOException e) {
        throw new InvalidEntityBodyException("Could not parse patch extension value: " + patchValue);
    }
}
Also used : InvalidEntityBodyException(com.yahoo.elide.core.exceptions.InvalidEntityBodyException) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) IOException(java.io.IOException) DeleteVisitor(com.yahoo.elide.jsonapi.parser.DeleteVisitor)

Example 2 with DeleteVisitor

use of com.yahoo.elide.jsonapi.parser.DeleteVisitor in project elide by yahoo.

the class Elide method delete.

/**
 * Handle DELETE.
 *
 * @param baseUrlEndPoint base URL with prefix endpoint
 * @param path the path
 * @param jsonApiDocument the json api document
 * @param queryParams the query params
 * @param requestHeaders the request headers
 * @param opaqueUser the opaque user
 * @param apiVersion the API version
 * @param requestId the request ID
 * @return Elide response object
 */
public ElideResponse delete(String baseUrlEndPoint, String path, String jsonApiDocument, MultivaluedMap<String, String> queryParams, Map<String, List<String>> requestHeaders, User opaqueUser, String apiVersion, UUID requestId) {
    return handleRequest(false, opaqueUser, dataStore::beginTransaction, requestId, (tx, user) -> {
        JsonApiDocument jsonApiDoc = StringUtils.isEmpty(jsonApiDocument) ? new JsonApiDocument() : mapper.readJsonApiDocument(jsonApiDocument);
        RequestScope requestScope = new RequestScope(baseUrlEndPoint, path, apiVersion, jsonApiDoc, tx, user, queryParams, requestHeaders, requestId, elideSettings);
        requestScope.setEntityProjection(new EntityProjectionMaker(elideSettings.getDictionary(), requestScope).parsePath(path));
        BaseVisitor visitor = new DeleteVisitor(requestScope);
        return visit(path, requestScope, visitor);
    });
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) BaseVisitor(com.yahoo.elide.jsonapi.parser.BaseVisitor) EntityProjectionMaker(com.yahoo.elide.jsonapi.EntityProjectionMaker) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) RequestScope(com.yahoo.elide.core.RequestScope) DeleteVisitor(com.yahoo.elide.jsonapi.parser.DeleteVisitor)

Aggregations

JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)2 DeleteVisitor (com.yahoo.elide.jsonapi.parser.DeleteVisitor)2 RequestScope (com.yahoo.elide.core.RequestScope)1 InvalidEntityBodyException (com.yahoo.elide.core.exceptions.InvalidEntityBodyException)1 EntityProjectionMaker (com.yahoo.elide.jsonapi.EntityProjectionMaker)1 PatchRequestScope (com.yahoo.elide.jsonapi.extensions.PatchRequestScope)1 Resource (com.yahoo.elide.jsonapi.models.Resource)1 BaseVisitor (com.yahoo.elide.jsonapi.parser.BaseVisitor)1 IOException (java.io.IOException)1