只需检查
existsWithId()
.我将这样实施:
@Override
public Mono<String> createItem(ItemCreateParam itemCreateParam) {
return reactiveItemRepository.existsById(itemCreateParam.getName())
.doOnNext(exists -> {
if (exists) {
throw new AppException(ErrorCode.ITEM_EXISTS);
}
})
.flatMap(exists -> convert(item))
.flatMap(converted -> reactiveTemplateRepository.save(converted))
.map(saved -> saved.getName());
}
请注意
AppException
类型可以是任何其他类型,但它应该扩展
RuntimeException
.