Skip to content

Commit

Permalink
Implement GET endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
anotheroneofthese committed Aug 22, 2024
1 parent 5e0a5fa commit 0a26660
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/kotlin/no/nb/mlt/wls/order/controller/OrderController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -72,4 +76,10 @@ class OrderController(val orderService: OrderService) {
suspend fun createOrder(
@RequestBody payload: ApiOrderPayload
): ResponseEntity<ApiOrderPayload> = orderService.createOrder(payload)

@GetMapping("/order/{hostName}/{hostOrderId}")
suspend fun getOrder(
@PathVariable("hostName") hostName: HostName,
@PathVariable("hostOrderId") hostOrderId: String
): ResponseEntity<Order> = orderService.getOrder(hostName, hostOrderId)
}
20 changes: 20 additions & 0 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,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
Expand Down Expand Up @@ -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<Order> {
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")
}
}

0 comments on commit 0a26660

Please sign in to comment.