Created
December 25, 2021 18:06
-
-
Save GaetanoPiazzolla/6c89d2a0889180d2545e946b1d79781c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // r2dbc | |
| public Mono<OrderDTO> addOrder(@RequestParam("bookIsbn") String bookIsbn, @RequestParam("firstName") String firstName) { | |
| return Mono.just(UUID.randomUUID()).flatMap(uuid -> { | |
| log.info("addOrder() {} running", uuid); | |
| Mono<User> user = userRepository.findByFirstName(firstName); | |
| Mono<Book> book = bookRepository.findByIsbn(bookIsbn); | |
| return Mono.zip(user, book).flatMap(zipFlux -> { | |
| log.info("addOrder() {} I've got user and book", uuid); | |
| Order order = new Order(); | |
| order.setUserId(zipFlux.getT1().getUserId()); | |
| order.setBookId(zipFlux.getT2().getBookId()); | |
| order.setQuantity(1); | |
| return orderRepository.save(order).map(orderMapper::toDto); | |
| } | |
| ).doFinally((s) -> { | |
| log.info("addOrder() {} executed", uuid); | |
| }); | |
| }); | |
| } | |
| // jdbc | |
| public ResponseEntity<OrderDTO> addOrder(@RequestParam("bookIsbn") String bookIsbn, @RequestParam("firstName") String firstName) { | |
| UUID uuid = UUID.randomUUID(); | |
| log.info("addOrder() {} running", uuid); | |
| User user = userRepository.findByFirstName(firstName); | |
| Book book = bookRepository.findByIsbn(bookIsbn); | |
| log.info("addOrder() {} I've got user and book", uuid); | |
| Order order = new Order(); | |
| order.setUser(user); | |
| order.setQuantity(1); | |
| order.setBook(book); | |
| order = orderRepository.save(order); | |
| OrderDTO orderDTO = orderMapper.toDto(order); | |
| log.info("addOrder() {} executed", uuid); | |
| return ResponseEntity.ok(orderDTO); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code snippet Related to medium article:
https://blog.devgenius.io/an-epic-tale-comparing-jdbc-and-r2dbc-in-a-real-world-scenario-part-2-2-d908df49651c#823f-8a39553a5cf4