将单声道与助焊剂结合
Combining mono with flux
我有一个简单的任务。我想添加到订单产品。
public Mono<OrderDTO> addProductsToOrder(String orderId, String[] productIds)
{
final List<String> productIdsList = Arrays.asList(productIds);
final Mono<Order> order = orderRepository.findById(orderId)
.switchIfEmpty(orderShouldExists(orderId));
final Flux<BaseProductInfo> products = fromIterable(productIdsList)
.flatMap(productId -> productRepository.findById(productId)
.switchIfEmpty(productShouldExists(productId)));
return order.then(combineLatest(order, products, Tuples::of)
.map(objects -> objects.getT1().addProduct(objects.getT2()))
.take(productIdsList.size())
.last()
.flatMap(orderRepository::save)
.map(orderMapper::mapToDTO));
}
我还想实现的是 return 更新订单。下面的代码无法正常工作。有时会保存所有产品,有时会保存 none 个。我使用的数据库是反应性的 mongo.
从您的代码来看,您似乎正在尝试将 BaseProductInfo
的列表添加到 Order
并保存。您可以通过执行以下操作来实现此目的:
public Mono<OrderDTO> addProductsToOrder(String orderId, String[] productIds) {
final List<String> productIdsList = Arrays.asList(productIds);
final Mono<Order> order = orderRepository.findById(orderId)
.switchIfEmpty(orderShouldExists(orderId));
final Mono<List<BaseProductInfo>> products = fromIterable(productIdsList)
.flatMap(productId -> productRepository.findById(productId)
.switchIfEmpty(productShouldExists(productId)))
.collectList();
return order.zipWith(products).map(tuple -> tuple.getT1().addProducts(tuple.getT2)).flatMap(orderRepository::save)
.map(orderMapper::mapToDTO));
}
我用这种方法解决了问题。
public Mono<OrderDTO> addProductsToOrder(String orderId, String[] productIds)
{
return orderRepository.findById(orderId)
.switchIfEmpty(orderShouldExists(orderId))
.flatMap(order -> Flux
.fromArray(productIds)
.flatMap(productId -> productRepository.findById(productId)
.switchIfEmpty(productShouldExists(productId))
)
.doOnNext(order::addProduct)
.then(Mono.just(order))
)
.flatMap(orderRepository::save)
.map(orderMapper::mapToDTO);
}
我有一个简单的任务。我想添加到订单产品。
public Mono<OrderDTO> addProductsToOrder(String orderId, String[] productIds)
{
final List<String> productIdsList = Arrays.asList(productIds);
final Mono<Order> order = orderRepository.findById(orderId)
.switchIfEmpty(orderShouldExists(orderId));
final Flux<BaseProductInfo> products = fromIterable(productIdsList)
.flatMap(productId -> productRepository.findById(productId)
.switchIfEmpty(productShouldExists(productId)));
return order.then(combineLatest(order, products, Tuples::of)
.map(objects -> objects.getT1().addProduct(objects.getT2()))
.take(productIdsList.size())
.last()
.flatMap(orderRepository::save)
.map(orderMapper::mapToDTO));
}
我还想实现的是 return 更新订单。下面的代码无法正常工作。有时会保存所有产品,有时会保存 none 个。我使用的数据库是反应性的 mongo.
从您的代码来看,您似乎正在尝试将 BaseProductInfo
的列表添加到 Order
并保存。您可以通过执行以下操作来实现此目的:
public Mono<OrderDTO> addProductsToOrder(String orderId, String[] productIds) {
final List<String> productIdsList = Arrays.asList(productIds);
final Mono<Order> order = orderRepository.findById(orderId)
.switchIfEmpty(orderShouldExists(orderId));
final Mono<List<BaseProductInfo>> products = fromIterable(productIdsList)
.flatMap(productId -> productRepository.findById(productId)
.switchIfEmpty(productShouldExists(productId)))
.collectList();
return order.zipWith(products).map(tuple -> tuple.getT1().addProducts(tuple.getT2)).flatMap(orderRepository::save)
.map(orderMapper::mapToDTO));
}
我用这种方法解决了问题。
public Mono<OrderDTO> addProductsToOrder(String orderId, String[] productIds)
{
return orderRepository.findById(orderId)
.switchIfEmpty(orderShouldExists(orderId))
.flatMap(order -> Flux
.fromArray(productIds)
.flatMap(productId -> productRepository.findById(productId)
.switchIfEmpty(productShouldExists(productId))
)
.doOnNext(order::addProduct)
.then(Mono.just(order))
)
.flatMap(orderRepository::save)
.map(orderMapper::mapToDTO);
}