-
-
Save vkrmsngh43/13a8753848893a0f6988d8327f656220 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
you cannot do this updatedList.add(entry); or createdList.add(entry);
because, updatedList can take only MyObjects type, but entry is RequestObjects type