title: How to include if-else statements into reactive flow
body: <p>I have a Spring Webflux reactive service which receives a DTO and inserts it into multiple table.
  Sometimes we may need to skip inserting into some tables based on the incoming DTO.</p>
  <p>These are the requirements:</p>
  <ol>
  <li>Create a new Client</li>
  <li>Create a new Client referral if the client referral is present in the DTO.</li>
  <li>Create a client Secondary contact if present in the DTO</li>
  <li>Create client phones if present in the DTO</li>
  </ol>
  <p><strong>Questions:-</strong></p>
  <ol>
  <li>Not sure how to apply the if condition in the reactive flow.</li>
  <li>Is there any better way of doing this?</li>
  <li>Here, except the first one all other operation can run in parallel.</li>
  </ol>
  <pre><code>public Mono&lt;ServerResponse&gt; createClientProfile(ServerRequest request) {
          return secContext.retrieveUser().flatMap(usr -&gt; {
              return request.bodyToMono(ClientDto.class).flatMap(client -&gt; {
                  return toNewClient(client, usr).flatMap(clientRepository::save).flatMap(clientRes -&gt; {
                      return toNewClientReferral(clientRes.getClientId(), client.getDiscount(), usr)
                              .flatMap(clientReferralRepository::save).flatMap(clientReferralRes -&gt; {
                                  return toNewClientSyContact(clientRes.getClientId(), client.getSecondary(), usr)
                                          .flatMap(clientSyContactRepository::save).flatMap(clientSyContactRes -&gt; {
                                              return clientPhoneRepository
                                                      .saveAll(toNewClientPhone(clientRes.getClientId(), client.getPhones(), usr))
                                                      .collectList().flatMap(phoneRes -&gt; {
                                                          return ServerResponse
                                                                  .created(URI.create(String.format(CLIENT_URI_FORMAT,
                                                                          clientRes.getClientId())))
                                                                  .contentType(APPLICATION_JSON).build();
                                                      });
                                          });
                              });
                  });
              });
          });
  
      }
  
  private Mono&lt;Referral&gt; toNewClientReferral(final long clientId, final Discount dto) {
          Referral referral = Referral.of(clientId, 
                  dto.getName(), dto.getType(), dto.getAmount(), dto.getStatus());
  
          return Mono.just(referral);
      }
  </code></pre>
  <blockquote>
  <p>client.getDiscount() can be null,<br />
  client.getSecondary() can be null,
  client.getPhones() can be empty.</p>
  </blockquote>
  <p>I separated the flow with 3 different methods.</p>
  <pre><code>public void createSyContact(ServerRequest request, long clientId) {
          secContext.retrieveUser().flatMap(usr -&gt; {
              return request.bodyToMono(ClientDto.class).flatMap(client -&gt; {
                  if (client.getSecondary() != null) {
                      return toNewClientSyContact(clientId, client.getSecondary(), usr)
                              .flatMap(clientSyContactRepository::save).flatMap(clientRes -&gt; {
                                  return Mono.just(clientRes.getClientId());
                              });
                  } else {
                      return Mono.empty();
                  }
              });
          });
      }
  
      public void createReferral(ServerRequest request, long clientId) {
          secContext.retrieveUser().flatMap(usr -&gt; {
              return request.bodyToMono(ClientDto.class).flatMap(client -&gt; {
                  if (client.getDiscount() != null) {
                      return toNewClientReferral(clientId, client.getDiscount(), usr)
                              .flatMap(clientReferralRepository::save).flatMap(clientRes -&gt; {
                                  return Mono.just(clientRes.getClientId());
                              });
                  } else {
                      return Mono.empty();
                  }
              });
          });
      }
  
      public Mono&lt;Long&gt; createClientWithPhones(ServerRequest request) {
          return secContext.retrieveUser().flatMap(usr -&gt; {
              return request.bodyToMono(ClientDto.class).flatMap(client -&gt; {
                  return toNewClient(client, usr).flatMap(clientRepository::save).flatMap(clientRes -&gt; {
                      return clientPhoneRepository
                              .saveAll(toNewClientPhone(clientRes.getClientId(), client.getPhones(), usr)).collectList()
                              .flatMap(phoneRes -&gt; {
                                  return Mono.just(clientRes.getClientId());
                              });
                  });
              });
          });
      }
  </code></pre>
  <p>Here, createClientWithPhones is mandatory, so no if check there. But the other 2 methods createReferral &amp; createSyContact have if checks. Need to execute createClientWithPhones first and it will return clientId. This clientId should be used in createReferral &amp; createSyContact.</p>
  <pre><code>public Mono&lt;ServerResponse&gt; createClientProfile(ServerRequest request) {
          final List&lt;Long&gt; clinetIdList = new ArrayList&lt;&gt;();
          createClientWithPhones(request).subscribe(result -&gt; {
              clinetIdList.add(result.longValue());
              createSyContact(request, result.longValue());
              createReferral(request, result.longValue());
          });
          return ServerResponse
                  .created(URI.create(String.format(CLIENT_URI_FORMAT,
                          clinetIdList.get(0))))
                  .contentType(APPLICATION_JSON).build();
          
      }
  </code></pre>
  <p>Is this the way to handle this?</p>