Skip to content

Instantly share code, notes, and snippets.

@hkakutalua
Last active July 19, 2020 09:50
Show Gist options
  • Save hkakutalua/281564f0b895e213ebe6d8f781a778bd to your computer and use it in GitHub Desktop.
Save hkakutalua/281564f0b895e213ebe6d8f781a778bd to your computer and use it in GitHub Desktop.
@transactional Without Save
@RestController
@RequestMapping("carts/{cart_id}/items")
class CartItemsController(
private val cartsRepository: CartsRepository,
private val productsRepository: ProductsRepository) {
@PostMapping
fun addItemToCart(
@PathVariable("cart_id") cartId: UUID,
@RequestBody cartItem: CartItemInputModel
) : ResponseEntity<Any> {
...
cart.addProduct(product, cartItem.quantity)
//cartsRepository.save(cart) // with @Transactional there's no need of this line
val savedProductItem = cart.items.first { x -> x.product.id == cartItem.productId }
return ResponseEntity.status(HttpStatus.CREATED)
.body(mapToCartItemViewModel(savedProductItem))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment