Chris Paul
09/10/2020, 11:58 AMInternalStockAlreadyExists error class. Here's a crude solution that enables me to retry saving one more time, but it feels wrong:
private fun attemptSave(stockItem: InternalStockItem): Either<InternalStockAlreadyExistsError, InternalStockItem> {
val result = stockRepository.saveStockItem(stockItem)
val outcome = result.getOrElse {
val secondTry = stockRepository.fetchStockByPublicReference(stockItem.publicReference)
val secondTry = existing!!.copy(
value = "updated value"
)
stockRepository.saveStockItem(secondTry).getOrElse { null }
}
return outcome?.right() ?: InternalStockAlreadyExistsError.left()
}