'WebFlux check field in DB
Tell me how to properly organize the check in the Mongo collection so that it is not null
createStripCustomerId - Here I have a check, if the field is not filled, execute the request and save. If the field is filled, then just return the current object
It confuses me that customerRepository.save(customer) - returns Mono As a result, you have to wrap the customer in Mono
Maybe there is an elegant solution?
private Mono<String> getStripCustomerId() {
return currentUser.getCustomer()
.map(customerLogin -> customerRepository.findById(customerLogin.getId()))
.flatMap(this::createStripCustomerId)
.map(Customer::getExternalCustomers)
.map(map -> map.get(PaymentSystem.STRIPE));
}
private Mono<Customer> createStripCustomerId(Mono<Customer> customerMono) {
return customerMono.flatMap(customer -> {
if (customer.getExternalCustomers()
.get(PaymentSystem.STRIPE) == null) {
var request = customerConverter.convertDocumentToStripe(customer);
var response = customerExternalService.createCustomer(request);
customer.setExternalCustomers(Map.of(PaymentSystem.STRIPE, response.getCustomerId()));
return customerRepository.save(customer);
} else {
return Mono.fromCallable(() -> customer); - This wrapper..
}
});
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
