I'm trying to make this code reactive, but without any luck.
This is the old code:
public BookInfo get( int libraryId )
{
Optional<Book> optionalBook = bookRepository.findByLibraryId( bookId );
int bookId = optionalBook
.map( b -> b.id() + 1 )
.orElse( 0 );
return bookRepository.findById( bookId )
.map( pages -> new BookInfo( optionalBook, pages ) );
}
This is what I came up with so far:
public Mono<BookInfo> get( int libraryId )
{
return bookRepository.findByLibraryId( bookId )
.flatMap( book -> bookRepository.findById( book.id() ) )
.switchIfEmpty( Mono.defer( () -> bookRepository.findById( 0 ) ) )
.map( pages -> new BookInfo( null, pages ) );
}
This issue is how can I get the book returned by the findByLibraryId method to the last map?
For this case I only need to pass in an extra value to the last map but what if I have to pass multiples?
Thanks