-
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.
Create basic implementation for creating orders
- Loading branch information
1 parent
ce2fd18
commit 615d7d4
Showing
5 changed files
with
93 additions
and
9 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
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,19 @@ | ||
package no.nb.mlt.wls.order.controller | ||
|
||
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"]) | ||
class OrderController(val orderService: OrderService) { | ||
// TODO - Swagger | ||
@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() | ||
} | ||
} |
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