Katlock
Katlock

Reputation: 1396

How to filter twice using spring flux mono

I want to filter once to make sure the object.getApiKey() equals my class's private apiKey variable and then also filter if the object.getEmail() does not exist in the database table row.

I tried few ways of filtering but so far the only one that is compiling is the following code:

public Mono<String> signupUser(Mono<UserTransfer> userMono) {
        LOG.info("signup user");

        return userMono
                .filter(userTransfer -> {
                    // first filter on apiKey
                    LOG.info(" userTransfer.apiKey: {}, apiKey: {}, match: {}", userTransfer.getApiKey(), apiKey, userTransfer.getApiKey().equals(apiKey));

                    return userTransfer.getApiKey().equals(apiKey);
                })
                .flatMap(userTransfer -> {

                    //I zip the mono found in the repository with the userTransfer so I can reuse it later in the next steps
                    Mono<Tuple2<User, UserTransfer>> tupleMono = userRepository.findByEmail(userTransfer.getEmail()).zipWith(Mono.just(userTransfer));
                    
                    LOG.info("do findByEmail for email: {}", userTransfer.getEmail());

                    return tupleMono;
                })              
                .filter(objects -> {
                    //2nd filter  
                    //however this line is never executed when I run my integ test. Any clue?
                    LOG.info("user findByEmail is {}, should be null since we don't allow user if email is already used.", objects.getT1());
                    
                return objects.getT1() == null;
                } )
                .flatMap(objects -> {
                    User user = new User(objects.getT2().getFirstName(), objects.getT2().getLastName(), objects.getT2().getEmail());
                    return userRepository.save(user).zipWith(Mono.just(objects.getT2()));
                    })


                .flatMap(objects -> {
                    User user = objects.getT1();
                    UserTransfer userTransfer = objects.getT2();
            LOG.info("create authentication with rest call to endpoint {}", authenticationEp);

            WebClient.ResponseSpec responseSpec = webClient.post().uri(authenticationEp).bodyValue(userTransfer).retrieve();
            return responseSpec.bodyToMono(String.class).map(authenticationId -> {
                LOG.info("got back authenticationId from service call: {}", authenticationId);
                return "user created with id: " + user.getEmail() + " with authId: " + authenticationId;
            });
        });
    }
}

Upvotes: 0

Views: 824

Answers (1)

Roman Gorbatenko
Roman Gorbatenko

Reputation: 149

Seems that you need to check "emptiness" of the User object in your Tuple since object can not be null by design of project reactor.

Try to check contents of the object returned from your repository. Good luck

enter image description here

Upvotes: 1

Related Questions