Home > OS >  How to populate table in MongoDB when call to endpoint is made? - MongoDb, SpringBoot
How to populate table in MongoDB when call to endpoint is made? - MongoDb, SpringBoot

Time:11-06

I am using Spring Boot with MongoDB. I am beginner to MongoDb.

What I am trying to achieve is when I get a call on my endpoint, or when my endpoint is requested, I would like to create a table in database that will have status REQUESTED, applied userID and then fields url, version, requestedDate(populated on table creation), completedDate (populated when URL is saved to database).

After, I do all the logic that I have to, in my case logic is that I should retrieve URL for user and save it to the same table that was created when request for URL was issued.

After URL is saved in same table created at request time I should have userId, url, version, requestedDate, completedDate and state as COMPLETED.

Code that I have so far is:

UserUrl.java

public class UserUrl{
        public final MongoUserId mongoUserId;
        public final Optional<String> url;
        public final long version;
        public final Instant requestedDate;
        public final Instant completeDate;
        public final State state;
    
    
        public UserUrl(MongoUserId mongoUserId,
                       Optional<String> url,
                       long version,
                       Instant requestedDate,
                       State state) {
            this.mongoUserId = mongoUserId;
            this.url = url;
            this.version = version;
            this.requestedDate = requestedDate;
            this.state = state;
        }
    
        public static UserUrl create(MongoUserId mongoUserId){
            return new UserUrl(
                    mongoUserId,
                    Optional.empty(),
                    0,
                    Instant.now(),
                    State.REQUESTED
                    );
        }
    
        public UserUrl withUrl(String url){
            return new UserUrl(
                    this.mongoUserId,
                    Optional.of(url),
                    0,
                    Instant.now(),
                    State.COMPLETED
            );
        }
}

UserController.java

@RequestMapping(value = "/trigger/url/{userId}",
            method = RequestMethod.GET)
    public void getUrlForUser(@PathVariable("userId") String userId) {

        userUrlRepository.save(UserUrl.create(MongoUserId.of(userId)));

         URL getUrl = userDataService.getUrlUser(userId);
         String url = getUrl.toString();
         if (url != null) {
             
             //here I should probably do something to set withUrl method in model 

             return new ResponseEntity<>(url, HttpStatus.OK);
         } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

I am not sure how should I populate UserUrl model when I have URL?

Also, if I put completedDate in constructor, then I suppose to have it on creation as well, but then what value should I apply to it?

Any advice appreciated :)

CodePudding user response:

You should be using the entity persisted using the CrudRepository, updating it and saving it again if needed:

@RequestMapping(value = "/trigger/url/{userId}", method = RequestMethod.GET)
public void getUrlForUser(@PathVariable("userId") String userId) {

    UserUrl userUrl = userUrlRepository.save(UserUrl.create(MongoUserId.of(userId)));

    URL getUrl = userDataService.getUrlUser(userId);
    String url = getUrl.toString();
    if (url != null) {
        userUrlRepository.save(userUrl.withUrl(url));
        return new ResponseEntity<>(url, HttpStatus.OK);
    } else {
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
}

EDIT

To alter the completeDate upon completion, that is when the url property has been resolved, update the withUrl builder method to propagate the this.requestedDate and initialize the completeDate to Instant.now():

public class UserUrl {

    public final MongoUserId mongoUserId;
    public final Optional<String> url;
    public final long version;
    public final Instant requestedDate;
    public final Instant completeDate;
    public final State state;


    public UserUrl(MongoUserId mongoUserId,
                   Optional<String> url,
                   long version,
                   Instant requestedDate,
                   Instant completeDate,
                   State state) {
        this.mongoUserId = mongoUserId;
        this.url = url;
        this.version = version;
        this.requestedDate = requestedDate;
        this.completeDate = completeDate;
        this.state = state;
    }

    public static UserUrl create(MongoUserId mongoUserId){
        return new UserUrl(
                mongoUserId,
                Optional.empty(),
                0,
                Instant.now(),
                null,
                State.REQUESTED
                );
    }

    public UserUrl withUrl(String url){
        return new UserUrl(
                this.mongoUserId,
                Optional.of(url),
                0,
                this.requestedDate,
                Instant.now(),
                State.COMPLETED
        );
    }
}
  • Related