I have a method to register a user:
public Mono<Either<AppError, UserDTO> > registerUser(RegisterUserDTO dto) {
Mono<Either<AppError, Mono<UserDTO> > > result = userFactory
.create(dto.username(), dto.password(), UserRole.COMMON)
.map(it -> it
.map(user -> reactiveUserRepository
.add(user)
.map(User::toDTO)
)
);
return result;
}
Create method from UserFactory returns Mono<Either<AppError, User> > and add method from ReactiveUserRepository returns Mono< User >. Of course, it doesn't compile, because i need Mono<Either<AppError, UserDTO> > instead of Mono<Either<AppError, Mono< UserDTO > > >. How i can do that?
Update: It's a solution from other forum:
public Mono<Either<AppError, UserDTO> > registerUser(RegisterUserDTO dto) {
return userFactory
.create(dto.username(), dto.password(), UserRole.COMMON)
.flatMap(it -> it
.map(user -> reactiveUserRepository
.add(user)
.map(u -> Either.<AppError, UserDTO>right(u.toDTO() ) )
)
.mapLeft(error -> Mono.just(Either.<AppError, UserDTO>left(error) ) )
.getOrElseGet(error -> error)
);
}
Anybody has better idea or it's best one? (It looks horrible, i know)