我有一个程序,只有在插入的数据与旧数据不同时才会更新。
我的解决方案需要一个嵌套的flatMap,因为保存后我需要创建一个日志记录。这是我的代码:
public Mono<RescheduleConfiguration> findByIdAndUpdateStatus(MandatoryRequest mandatoryRequest, String id, RescheduleConfigStatus updatedStatus) {
return rescheduleConfigurationRepository
.findById(id)
.switchIfEmpty(
Mono.error(
new BusinessLogicException(
ResponseCode.DATA_NOT_EXIST.getCode(),
ResponseCode.DATA_NOT_EXIST.getMessage()
)
)
)
.flatMap(rescheduleConfiguration -> {
if(updatedStatus.equals(rescheduleConfiguration.getStatus())){
return Mono.just(rescheduleConfiguration);
}
return rescheduleConfigurationLogService
.createRescheduleConfigurationLog(mandatoryRequest, rescheduleConfiguration)
.flatMap(newRescheduleConfiguration -> {
newRescheduleConfiguration.setStatus(updatedStatus);
return rescheduleConfigurationRepository.save(newRescheduleConfiguration);
});
});
}