From 0a266600232002be74d22d8557cfca19671f5497 Mon Sep 17 00:00:00 2001 From: Noah Bjerkli Aanonli Date: Thu, 22 Aug 2024 11:55:15 +0200 Subject: [PATCH] Implement GET endpoint --- .../wls/order/controller/OrderController.kt | 10 ++++++++++ .../nb/mlt/wls/order/service/OrderService.kt | 20 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/main/kotlin/no/nb/mlt/wls/order/controller/OrderController.kt b/src/main/kotlin/no/nb/mlt/wls/order/controller/OrderController.kt index c2ea2856..89ebba80 100644 --- a/src/main/kotlin/no/nb/mlt/wls/order/controller/OrderController.kt +++ b/src/main/kotlin/no/nb/mlt/wls/order/controller/OrderController.kt @@ -6,9 +6,13 @@ import io.swagger.v3.oas.annotations.media.Schema import io.swagger.v3.oas.annotations.responses.ApiResponse import io.swagger.v3.oas.annotations.responses.ApiResponses import io.swagger.v3.oas.annotations.tags.Tag +import no.nb.mlt.wls.core.data.HostName +import no.nb.mlt.wls.order.model.Order import no.nb.mlt.wls.order.payloads.ApiOrderPayload import no.nb.mlt.wls.order.service.OrderService import org.springframework.http.ResponseEntity +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestMapping @@ -72,4 +76,10 @@ class OrderController(val orderService: OrderService) { suspend fun createOrder( @RequestBody payload: ApiOrderPayload ): ResponseEntity = orderService.createOrder(payload) + + @GetMapping("/order/{hostName}/{hostOrderId}") + suspend fun getOrder( + @PathVariable("hostName") hostName: HostName, + @PathVariable("hostOrderId") hostOrderId: String + ): ResponseEntity = orderService.getOrder(hostName, hostOrderId) } diff --git a/src/main/kotlin/no/nb/mlt/wls/order/service/OrderService.kt b/src/main/kotlin/no/nb/mlt/wls/order/service/OrderService.kt index eb23ab59..422d4d81 100644 --- a/src/main/kotlin/no/nb/mlt/wls/order/service/OrderService.kt +++ b/src/main/kotlin/no/nb/mlt/wls/order/service/OrderService.kt @@ -3,14 +3,18 @@ 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.core.data.HostName +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 import no.nb.mlt.wls.order.payloads.toSynqPayload import no.nb.mlt.wls.order.repository.OrderRepository +import org.springframework.dao.IncorrectResultSizeDataAccessException import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity import org.springframework.stereotype.Service +import org.springframework.web.server.ResponseStatusException import org.springframework.web.server.ServerErrorException import org.springframework.web.server.ServerWebInputException import java.time.Duration @@ -76,4 +80,20 @@ class OrderService(val db: OrderRepository, val synqService: SynqOrderService) { throw ServerWebInputException("The order must contain product lines") } } + + suspend fun getOrder( + hostName: HostName, + hostOrderId: String + ): ResponseEntity { + val order = + db.findByHostNameAndHostOrderId(hostName, hostOrderId) + .onErrorMap(IncorrectResultSizeDataAccessException::class.java) { + TODO("Handle duplicate order ID's somehow") + } + .awaitSingleOrNull() + if (order != null) { + return ResponseEntity.ok(order) + } + throw ResponseStatusException(HttpStatus.NOT_FOUND, "Order with id $hostOrderId from $hostName was not found") + } }