Skip to content

Commit

Permalink
Map error in OrderService, inline db call
Browse files Browse the repository at this point in the history
  • Loading branch information
anotheroneofthese committed Aug 16, 2024
1 parent 1733fb2 commit 9aa4982
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions src/main/kotlin/no/nb/mlt/wls/order/service/OrderService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package no.nb.mlt.wls.order.service
import io.github.oshai.kotlinlogging.KotlinLogging
import kotlinx.coroutines.reactive.awaitSingle
import kotlinx.coroutines.reactor.awaitSingleOrNull
import no.nb.mlt.wls.order.model.Order
import no.nb.mlt.wls.order.payloads.ApiOrderPayload
import no.nb.mlt.wls.order.payloads.toApiOrderPayload
import no.nb.mlt.wls.order.payloads.toOrder
Expand All @@ -21,7 +20,16 @@ private val logger = KotlinLogging.logger {}
@Service
class OrderService(val db: OrderRepository, val synqService: SynqOrderService) {
suspend fun createOrder(payload: ApiOrderPayload): ResponseEntity<ApiOrderPayload> {
val existingOrder = getByHostNameAndHostOrderId(payload)
val existingOrder =
db.getByHostNameAndHostOrderId(payload.hostName, payload.orderId)
.timeout(Duration.ofSeconds(8))
.onErrorMap(TimeoutException::class.java) {
logger.error(it) {
"Timed out while fetching from WLS database. Relevant payload: $payload"
}
ServerErrorException("Failed to create the order in storage system", it)
}
.awaitSingleOrNull()

if (existingOrder != null) {
return ResponseEntity.badRequest().build()
Expand All @@ -40,16 +48,4 @@ class OrderService(val db: OrderRepository, val synqService: SynqOrderService) {

return ResponseEntity.status(HttpStatus.CREATED).body(order.toApiOrderPayload())
}

suspend fun getByHostNameAndHostOrderId(payload: ApiOrderPayload): Order? {
// TODO - See if timeouts can be made configurable
return db.getByHostNameAndHostOrderId(payload.hostName, payload.orderId)
.timeout(Duration.ofSeconds(8))
.doOnError(TimeoutException::class.java) {
logger.error(it) {
"Timed out while fetching from WLS database. Relevant payload: $payload"
}
}
.awaitSingleOrNull()
}
}

0 comments on commit 9aa4982

Please sign in to comment.