From 35421bf226c6a854a77fd7514d568e744aef4dcc Mon Sep 17 00:00:00 2001 From: Harsh Verma <55652117+Harsh3305@users.noreply.github.com> Date: Sun, 22 Jan 2023 18:24:30 +0530 Subject: [PATCH 1/2] Done --- .../mart/product/service/ProductService.kt | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/com/hrv/mart/product/service/ProductService.kt b/src/main/kotlin/com/hrv/mart/product/service/ProductService.kt index 2987501..9c666d7 100644 --- a/src/main/kotlin/com/hrv/mart/product/service/ProductService.kt +++ b/src/main/kotlin/com/hrv/mart/product/service/ProductService.kt @@ -7,6 +7,7 @@ import org.springframework.http.HttpStatus import org.springframework.http.server.reactive.ServerHttpResponse import org.springframework.stereotype.Service import reactor.core.publisher.Mono +import reactor.kotlin.core.publisher.switchIfEmpty @Service class ProductService( @@ -30,18 +31,14 @@ class ProductService( } } fun getProductFromId(productId: String, response: ServerHttpResponse) = - isProductExist(productId) - .flatMap { isExist -> - if (isExist) { - setHTTPOkCode(response) - .then( - productRepository - .findById(productId) - ) - } else { - setHTTPNotfoundCode(response) - .then(Mono.empty()) - } + productRepository.findById(productId) + .flatMap {product -> + setHTTPOkCode(response) + .then(Mono.just(product)) + } + .switchIfEmpty { + setHTTPNotfoundCode(response) + .then(Mono.empty()) } fun deleteProduct(productId: String, response: ServerHttpResponse) = isProductExist(productId) From 657faefcb293ee55de926749038f03bcc3178dea Mon Sep 17 00:00:00 2001 From: Harsh Verma <55652117+Harsh3305@users.noreply.github.com> Date: Sun, 22 Jan 2023 18:33:10 +0530 Subject: [PATCH 2/2] Update in product test --- .../kotlin/com/hrv/mart/product/TestProductController.kt | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/test/kotlin/com/hrv/mart/product/TestProductController.kt b/src/test/kotlin/com/hrv/mart/product/TestProductController.kt index 21c5e34..789b58c 100644 --- a/src/test/kotlin/com/hrv/mart/product/TestProductController.kt +++ b/src/test/kotlin/com/hrv/mart/product/TestProductController.kt @@ -53,9 +53,6 @@ class TestProductController { } @Test fun `should return product if product exist`() { - doReturn(Mono.just(true)) - .`when`(productRepository) - .existsById(product.id) doReturn(Mono.just(product)) .`when`(productRepository) .findById(product.id) @@ -65,9 +62,9 @@ class TestProductController { } @Test fun `should return empty mono if product does not exist`() { - doReturn(Mono.just(false)) + doReturn(Mono.empty()) .`when`(productRepository) - .existsById(product.id) + .findById(product.id) StepVerifier.create(productController.getProductFromId(product.id, response)) .expectComplete() .verify()