0

I'm completely new to Java. I'm writing an API to update user customer mapping details in MongoDB The requirement is to update a record in Mongo, if it is already existing.
If the record is not there then we need to insert new record.

The Payload of API is something similar.

{
userid : "ABC",
customers :{
    cmpId : "customer1",
    cmpid : "customer2"
    }
}

The record in Mongo is similar to the above structure.
If we are entering the details for "ABC" for the first time, a record should be inserted. If a record with userid exists , then we have to loop through the customers and add those which are not there.

So I have written something like this in the service layer :

public CustomerUserMappingDTO AddData(CustomerUserMappingDTO body)
{

    Mono<CustomerUserMappingDTO> existingUser = customerRepository.getUserId(body.getUserid());
    if () // how to check if the record exists
    {   
        // logic to update data in MongoDB
    
    }else {
    
        // logic to insert data to MongoDB
    }
}

I have read something about flapMap to check the tupils , but I'm unable to figure out how to check it.
Can anyone help me here. Many thanks.

CrazyCoder
  • 2,194
  • 10
  • 44
  • 91

1 Answers1

0

Here's a sample of code using map() and flatMap()
You still need to replace the database check & insert

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

class Customer {
    String cmpId;

    public Customer(String cmpId) {
        this.cmpId = cmpId;
    }

    public String getCmpId() {
        return cmpId;
    }
}

class CustomerUserMappingDTO {
    String userid;
    List<Customer> customers;

    public CustomerUserMappingDTO(String userid, List<Customer> customers) {
        this.userid = userid;
        this.customers = customers;
    }

    public Stream<Customer> getCustomersAsStream() {
        return customers.stream();
    }
}

public class Main {
    public static void main(String[] args) {
        // We will suppose Customer foo is already in database
        CustomerUserMappingDTO existingUser = new CustomerUserMappingDTO("abc", Arrays.asList(new Customer("foo"), new Customer("bar")));
        Stream.<CustomerUserMappingDTO>of(existingUser)
                .<Customer>flatMap(CustomerUserMappingDTO::getCustomersAsStream)
                .<Customer>filter(customer -> ! customer.getCmpId().equals("foo")) // Check record does not exist
                .<String>map(customer -> "Customer missing from database: " + customer.getCmpId())
                .forEach(System.out::println); // Insert into database
    }
}

The code does check if customer.cmpId is not foo, otherwise it prints out an helpful message
I also hardtyped my stream so you can follow easily which operation is done

As you only have one existing user, I suggest to replace Stream.of().flatMap() by existingUser.getCustomersAsStream()

IQbrod
  • 2,060
  • 1
  • 6
  • 28
  • Thanks a lot for your reply. In my code existingUser is a Mono Object . How to read MonoObject to check if the data is existing? – CrazyCoder Apr 27 '21 at 16:11
  • @CrazyCoder I have no idea what `Mono` is, it looks like it's a class from `reactor` I found a Stack post about it : https://stackoverflow.com/questions/60705382/mono-class-in-java-what-is-and-when-to-use – IQbrod Apr 28 '21 at 08:18