title: How to use Mono and Flux in handler functions with Spring WebFlux Reactive ways
body: <p>Check for <code>--&gt;</code></p>
  
  <p>My model looks something like this. </p>
  
  <pre><code>@Document
  public class PlanDetails {
  
      @Id
      private String id;
      private String name;
      private Double balance;
      private Double internet;
      private Date date;
  
      --&gt; //String of id's basically.
      private List&lt;String&gt; members;
  
      public PlanDetails(){}
  
      public PlanDetails(String id, String name, Double balance, Double internet, Date date, List&lt;String&gt; members){
          this.id = id;
          this.name = name;
          this.balance = balance;
          this.internet = internet;
          this.date = date;
          this.members = members;
      }
  
      public String getId() {
          return id;
      }
  
      /* Getter setters ommited for brevity */
  </code></pre>
  
  <p>Here is my handler class to handle my functional endpoints. </p>
  
  <pre><code>package com.startelco.plandetailsapi.handler;
  
  import com.startelco.plandetailsapi.model.PlanDetails;
  import com.startelco.plandetailsapi.repository.PlanRepository;
  import org.springframework.http.HttpStatus;
  import org.springframework.stereotype.Component;
  import org.springframework.web.reactive.function.BodyInserters;
  import org.springframework.web.reactive.function.server.ServerRequest;
  import org.springframework.web.reactive.function.server.ServerResponse;
  import reactor.core.publisher.Flux;
  import reactor.core.publisher.Mono;
  
  import static org.springframework.http.MediaType.APPLICATION_JSON;
  @Component
  public class PlanDetailsHandler {
  
      private PlanRepository repository;
  
  
      public PlanDetailsHandler(PlanRepository repository){
          this.repository = repository;
      }
  
      //Get All Users
      public Mono&lt;ServerResponse&gt; getAllUsers(ServerRequest request){
          Flux&lt;PlanDetails&gt; users = repository.findAll();
  
          return ServerResponse.ok()
                  .contentType(APPLICATION_JSON)
                  .body(users,PlanDetails.class);
      }
  
      --&gt; //Get User by ID
      public Mono&lt;ServerResponse&gt; getUserDetails(ServerRequest request){
          String id = request.pathVariable("id");
  
          Mono&lt;PlanDetails&gt; userMono = repository.findById(id);
          Mono&lt;ServerResponse&gt; notFound = ServerResponse.notFound().build();
  
          return userMono.flatMap(user -&gt;
                  ServerResponse.ok()
                          .contentType(APPLICATION_JSON)
                          .body(BodyInserters.fromObject(user))
                          .switchIfEmpty(notFound)
                  );
      }
  
  
      //Create a user
      public Mono&lt;ServerResponse&gt; saveUser(ServerRequest request){
          Mono&lt;PlanDetails&gt; userMono = request.bodyToMono(PlanDetails.class);
  
          return userMono.flatMap(user -&gt;
                  ServerResponse.status(HttpStatus.CREATED)
                          .contentType(APPLICATION_JSON)
                          .body(repository.save(user),PlanDetails.class)
                  );
      }
  
  
      //Update user by ID
      public Mono&lt;ServerResponse&gt; updateUser(ServerRequest request) {
          String id = request.pathVariable("id");
          Mono&lt;PlanDetails&gt; existingUserMono = this.repository.findById(id);
          Mono&lt;PlanDetails&gt; userMono = request.bodyToMono(PlanDetails.class);
  
          Mono&lt;ServerResponse&gt; notFound = ServerResponse.notFound().build();
  
          return userMono.zipWith(existingUserMono,
                  (user, existingUser) -&gt;
                          new PlanDetails(existingUser.getId(), user.getName(), user.getBalance(), user.getInternet(),user.getDate(),user.getMembers())
          )
                  .flatMap(user -&gt;
                          ServerResponse.ok()
                                  .contentType(APPLICATION_JSON)
                                  .body(repository.save(user), PlanDetails.class)
                  ).switchIfEmpty(notFound);
      }
  
  
  
      //Delete All Users
      public Mono&lt;ServerResponse&gt; deleteAllUsers(ServerRequest request) {
          return ServerResponse.ok()
                  .build(repository.deleteAll());
      }
  }
  </code></pre>
  
  <p><strong>QUESTION:</strong> What is the Reactive way of coding <code>getMemberDetails</code> with reusing the previously written function <code>getUserDetails.</code>?</p>
  
  <blockquote>
    <p>Requirement.</p>
  </blockquote>
  
  <p><strong>DESIRED FUNCTIONALITY : Get Member Details Function.</strong></p>
  
  <pre><code>public Mono&lt;ServerResponse&gt; getMemberDetails(ServerRequest request) {
     --&gt; //Pseudocode
     String id = request.pathVariable("id");
  
     Mono&lt;PlanDetails&gt; userMono = repository.findById(id);
     Mono&lt;ServerResponse&gt; notFound = ServerResponse.notFound().build();
  
     if(userMono.member is not null){
         int length = userMono.member.length
         Flux&lt;PlanDetails&gt; memberFlux;
         for(int i=0;i&lt;length;i++){
              Mono&lt;PlanDetails&gt; member = getUserDetails(id=userMono.member[i]);
              memberFlux.add(member);
         }
      }else{
         return ServerResponse.ok().build(memberFlux=null or empty array);
      }
  
      return ServerResponse.ok().build(memberFlux.flatmap);
  
  }
  </code></pre>
  
  <p><strong>DESIRED BEHAVIOR</strong> </p>
  
  <pre><code>{
      "id": "5b42ecc11cde674475cab39a",
      "name": "Alex Svirsky",
      "balance": 140,
      "internet": 20,
      "date": 1531107095659,
      "members": [
          "5b42ecdd1cde674475cab39b",
          "5b42ed421cde674475cab39c",
          "5b42ed5d1cde674475cab39d"
      ]
  }
  </code></pre>
  
  <p>REST call in my routes is defined as  <code>http://localhost:8080/users/members/5b42ecc11cde674475cab39a</code> </p>
  
  <pre><code>[    {
          "id": "5b42ecdd1cde674475cab39b",
          "name": "Bob Marley",
          "balance": 120,
          "internet": 9,
          "date": 1531107095559,
          "members": null
      },
      {
          "id": "5b42ed421cde674475cab39c",
          "name": "Charlie Sheen",
          "balance": 10,
          "internet": 9,
          "date": 1531107095555,
          "members": null
      },
      {
          "id": "5b42ed5d1cde674475cab39d",
          "name": "Dale Carnegie",
          "balance": 100,
          "internet": 9,
          "date": 1531107055555,
          "members": null
      }
  ]
  </code></pre>