Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use AgRest to return Cayenne Object #686

Open
Jorge-Fern opened this issue Aug 30, 2024 · 1 comment
Open

Use AgRest to return Cayenne Object #686

Jorge-Fern opened this issue Aug 30, 2024 · 1 comment
Labels

Comments

@Jorge-Fern
Copy link

Not really sure where to ask this, but here it goes. Sorry if this is not the right place.

I have some requests that have to make complex actions so I would like to use cayenne directly to do the hardwork and then use arrest to send the objet as a response.

Something like this.


 //get new context from the existing runtime
 ObjectContext cont = MyBootApp.cayenneRuntime.newContext();

 //get a object from db
 var user = SQLSelect.query(User.class, "select * from user where id = $iduser")
        .param("iduser", userid)
        .selectOne(cont);


 //do my stuff (simple example)

 user.getCars().forEach( car -> {
         car.setStolen(true);
         mailService.sendLocation(user.getmail(),  car.getLastLocation());
         //etc...
            
 });


  //not real code here, just what I would like to do
  AgRequest request = AgJaxrs.request(config)
                .addIncludes(includes)  //possible set includes and excludes like always
                .build();


  return AgJaxrs.parseObject(user)
                .request(request)
                .get();

is it possible to do something like this?

@stariy95
Copy link
Member

Hi @Jorge-Fern

I got something like this in my project. That just sorts data in the response, but this should also allow to add/change data completely.

protected DataResponse<Project> fetchProjects(String include) {
    AgRequest request = AgJaxrs.request(configuration).addInclude(include).build();
    return AgJaxrs.select(Project.class, configuration)
                .stage(SelectStage.FETCH_DATA, new MavenProjectSorter(mavenService))
                .request(request)
                .get();
}

static class MavenProjectSorter implements Consumer<SelectContext<Project>> {
    // ...
    @Override
    public void accept(SelectContext<Project> context) {
        RootResourceEntity<Project> entity = context.getEntity();
        // response data is redefined here
        entity.setData(mavenService.sortProjects(entity.getData()));
    }
}

You could also play with the SelectStage.ASSEMBLE_QUERY stage, that would allow to shape the Cayenne query itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants