-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
12 changed files
with
260 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package no.nb.mlt.wls.core.data.synq | ||
|
||
data class SynqError(val errorCode: Int, val errorText: String) |
54 changes: 54 additions & 0 deletions
54
src/main/kotlin/no/nb/mlt/wls/order/controller/OrderController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package no.nb.mlt.wls.order.controller | ||
|
||
import io.swagger.v3.oas.annotations.Operation | ||
import io.swagger.v3.oas.annotations.media.Content | ||
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.order.payloads.ApiOrderPayload | ||
import no.nb.mlt.wls.order.service.OrderService | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@RestController | ||
@RequestMapping(path = ["", "/v1"]) | ||
@Tag(name = "Order Controller", description = "API for ordering products via Hermes WLS") | ||
class OrderController(val orderService: OrderService) { | ||
@Operation( | ||
summary = "Creates an order for products from the storage system", | ||
description = """Creates an order for specified products to appropriate storage systems via Hermes WLS. | ||
Orders are automatically sent to the systems that own the respective product(s). | ||
""" | ||
) | ||
@ApiResponses( | ||
ApiResponse( | ||
responseCode = "201", | ||
description = "Created order for specified products to appropriate systems" | ||
), | ||
ApiResponse( | ||
responseCode = "400", | ||
description = | ||
"""Order payload is invalid and was not created. | ||
Error message contains information about the invalid fields.""", | ||
content = [Content(schema = Schema())] | ||
), | ||
ApiResponse( | ||
responseCode = "401", | ||
description = "Client sending the request is not authorized order products.", | ||
content = [Content(schema = Schema())] | ||
), | ||
ApiResponse( | ||
responseCode = "403", | ||
description = "A valid 'Authorization' header is missing from the request.", | ||
content = [Content(schema = Schema())] | ||
) | ||
) | ||
@PostMapping("/order/batch/create") | ||
fun createOrder( | ||
@RequestBody payload: ApiOrderPayload | ||
): ResponseEntity<ApiOrderPayload> = orderService.createOrder(payload) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/kotlin/no/nb/mlt/wls/order/repository/OrderRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package no.nb.mlt.wls.order.repository | ||
|
||
import no.nb.mlt.wls.core.data.HostName | ||
import no.nb.mlt.wls.order.model.Order | ||
import org.springframework.data.mongodb.repository.MongoRepository | ||
import org.springframework.stereotype.Repository | ||
|
||
@Repository | ||
interface OrderRepository : MongoRepository<Order, String> { | ||
fun getByHostNameAndHostOrderId( | ||
hostName: HostName, | ||
hostOrderId: String | ||
): Order? | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/kotlin/no/nb/mlt/wls/order/service/OrderService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package no.nb.mlt.wls.order.service | ||
|
||
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.http.HttpStatus | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.stereotype.Service | ||
import org.springframework.web.server.ServerErrorException | ||
|
||
@Service | ||
class OrderService(val db: OrderRepository, val synqService: SynqOrderService) { | ||
fun createOrder(payload: ApiOrderPayload): ResponseEntity<ApiOrderPayload> { | ||
// TODO - Order validation? | ||
|
||
val existingOrder = db.getByHostNameAndHostOrderId(payload.hostName, payload.orderId) | ||
if (existingOrder != null) { | ||
return ResponseEntity.ok(existingOrder.toApiOrderPayload()) | ||
} | ||
|
||
val synqResponse = synqService.createOrder(payload.toOrder().toSynqPayload()) | ||
if (!synqResponse.statusCode.is2xxSuccessful) { | ||
return ResponseEntity.internalServerError().build() | ||
} | ||
|
||
try { | ||
db.save(payload.toOrder()) | ||
} catch (e: Exception) { | ||
throw ServerErrorException("Failed to save product in database, but created in storage system", e) | ||
} | ||
|
||
return ResponseEntity.status(HttpStatus.CREATED).build() | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/kotlin/no/nb/mlt/wls/order/service/SynqOrderService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package no.nb.mlt.wls.order.service | ||
|
||
import no.nb.mlt.wls.core.data.synq.SynqError | ||
import no.nb.mlt.wls.order.payloads.SynqOrder | ||
import no.nb.mlt.wls.order.payloads.SynqOrderPayload | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.http.HttpEntity | ||
import org.springframework.http.HttpMethod | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.stereotype.Service | ||
import org.springframework.web.client.HttpClientErrorException | ||
import org.springframework.web.client.RestTemplate | ||
import org.springframework.web.server.ServerErrorException | ||
import java.net.URI | ||
|
||
@Service | ||
class SynqOrderService { | ||
val restTemplate: RestTemplate = RestTemplate() | ||
|
||
@Value("\${synq.path.base}") | ||
lateinit var baseUrl: String | ||
|
||
fun createOrder(payload: SynqOrderPayload): ResponseEntity<SynqError> { | ||
val uri = URI.create("$baseUrl/orders/batch") | ||
|
||
// Wrap the order in the way SynQ likes it | ||
val orders = SynqOrder(listOf(payload)) | ||
|
||
try { | ||
return restTemplate.exchange(uri, HttpMethod.POST, HttpEntity(orders), SynqError::class.java) | ||
} catch (e: HttpClientErrorException) { | ||
val errorBody = e.getResponseBodyAs(SynqError::class.java) | ||
|
||
throw ServerErrorException( | ||
"Failed to create product in SynQ, the storage system responded with error code: " + | ||
"'${errorBody?.errorCode ?: "NO ERROR CODE FOUND"}' " + | ||
"and error text: " + | ||
"'${errorBody?.errorText ?: "NO ERROR TEXT FOUND"}'", | ||
e | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 0 additions & 85 deletions
85
src/test/kotlin/no/nb/mlt/wls/controller/ProductControllerTest.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.