Skip to content

Instantly share code, notes, and snippets.

@vkrmsngh43
Last active April 23, 2020 11:47
Show Gist options
  • Save vkrmsngh43/13a8753848893a0f6988d8327f656220 to your computer and use it in GitHub Desktop.
Save vkrmsngh43/13a8753848893a0f6988d8327f656220 to your computer and use it in GitHub Desktop.
public Mono<ServerResponse> createOrUpdateBulkMapping(ServerRequest serverRequest) {
Flux<RequestObjects> requestFlux = serverRequest.bodyToFlux(RequestObjects.class);
List<RequestObjects> createdList = new ArrayList<>();
List<RequestObjects> updatedList = new ArrayList<>();
return requestFlux.flatMap(entry -> {
Flux<MyObjects> existingMapping = myRepository
.findByIdAndName(entry.getId(), entry.getName());
return existingMapping.map(optional -> {
if (optional != null) {
updatedList.add(entry);
MyObject model = MyObjectMapper.updateModel(entry,
optional);
myRepository.save(model);
return model;
} else {
createdList.add(entry);
MyObject model = MyObjectMapper.convertToModel(entry);
myRepository.save(model)
return model;
}
});
}).then(ServerResponse.ok().body(Mono.just("Bulk Create/Update is successful").log(), String.class));
}
//This successfully executes but doesn't save or update any objects in repository
@eaiman-shoshi
Copy link

you cannot do this updatedList.add(entry); or createdList.add(entry);
because, updatedList can take only MyObjects type, but entry is RequestObjects type

@eaiman-shoshi
Copy link

and it will not work because you are breaking the chain. myRepository.save(model); is outside the chain. you should include it inside your chain. see the solution i have given against you question on stackoverflow and please copy the whole solution and then try to edit after pasting.
https://stackoverflow.com/questions/61362483/implementing-upsert-functionality-for-object-flux#61381724

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment