I come from a Java background. I'm having a bit of a dilemma, OrderDetail needs an Order before saving it and I need Order needs OrderDetail as well. I thought the best approach was first creating the Order and with this, I will get the Order ID which I pass to the OrderDetail objects that are being created. Now the issue is that orderDetails is a val and cannot be reassigned, so order.orderDetails = orderDetails
does not work. A fix would be changing orderDetails to a var in the JPA Object class, but I feel this defeats the purpose of removing as much mutability as possible.
@RestController
@RequestMapping("/order")
class OrderController(val orderRepository: OrderRepository, val productRepository: ProductRepository) {
// @GetMapping
// fun findOrdersByUser(): Iterable<ViewOrder> = orderRepository.findAll().map { it.toView() };
@PostMapping
fun create(@RequestBody createOrderRequest: CreateOrderRequest): ResponseEntity<ViewOrder> {
val order = orderRepository.save(
Order(
status = OrderStatus.CONFIRMED,
orderPlacedDate = Date(),
totalPrice = BigDecimal.ZERO,
totalWeight = BigDecimal.ZERO,
)
)
val orderDetails = createOrderRequest.products.mapNotNull {
productRepository.findById(it.productId).orElse(null)?.let { product ->
OrderDetail(
product = product,
quantity = it.quantity,
currencyCode = CurrencyCode.DZD,
uom = product.uom,
price = product.price * it.quantity,
order = order
)
}
}
// Update order
order.orderDetails = orderDetails
orderRepository.save(order)
return ResponseEntity.status(HttpStatus.CREATED).body(order.toView())
}
}
OrderDetail
needs to be a property ofOrder
? IfOrder
andOrderDetail
are always instantiated together I think they should be a a single class instead.