agrestio / agrest

Server-side Java REST Framework for easy access to data graphs from various backends
https://agrest.io
Apache License 2.0
81 stars 34 forks source link

Recursive EntityUpdate parser #634

Closed andrus closed 1 year ago

andrus commented 1 year ago

While Agrest still doesn't know how to process nested updates, let's enable the parser to parse them anyways into a separate map inside parent EntityUpdate. For now this will allow the users to implement manual processing, and in the future may become a foundation of Agrest's own nested merge algorithm.

An example of manual saving of relationships:

@POST
@Path("e3")
public DataResponse<E3> create(@Context UriInfo uriInfo, EntityUpdate<E3> update) {

    DataResponse<E3> e3Response = AgJaxrs.create(E3.class, config)
            .clientParams(uriInfo.getQueryParameters())
            .syncAndSelect(update);

    int e2Id = Cayenne.intPKForObject(e3Response.getData().get(0));

    EntityUpdate<E2> e2Update = update.getToOne(E3.E2.getName());
    if (e2Update != null) {
        AgJaxrs
                .create(E2.class, config)
                .parent(E3.class, e2Id, E3.E2.getName())
                .sync(e2Update);
    }

    return e3Response;
}