0
votes

I have a simple CRUD controller. When updating I want merge the updated model with the model that has to be updated, this is what I have at the moment:

@PutMapping("{id}")
public Mono<Map> update(@PathVariable UUID id, @RequestBody Server updatedServer) {
    Mono<Server> server = this.serverRepository.findById(id);

    // Update the server here.
    // server.setName(updatedServer.getName());
    // server.setHost(updatedServer.getHost());

    server.flatMap(this.serverRepository::save).subscribe();

    return Mono.just(Collections.singletonMap("success", true));
}

How can I edit the server variable before the save? When subscribing on the Mono it will be executed after the save.

I know this is a pretty simple question, but I just can't find a way to do it.

1

1 Answers

1
votes

I always find the answer just after asking the question...
I just used another flatmap to edit the object:

@PutMapping("{id}")
public Mono<Server> update(@PathVariable UUID id, @RequestBody Server updatedServer) {
    Mono<Server> server = this.serverRepository.findById(id);

    return server.flatMap(value -> {
        value.setName(updatedServer.getName());
        value.setHost(updatedServer.getHost());
        return Mono.just(value); // Can this be done cleaner?
    }).flatMap(this.serverRepository::save);
}

Or when still returning success: true:

@PutMapping("{id}")
public Mono<Map> update(@PathVariable UUID id, @RequestBody Server updatedServer) {
    Mono<Server> server = this.serverRepository.findById(id);

    return server.flatMap(value -> {
        value.setName(updatedServer.getName());
        value.setHost(updatedServer.getHost());
        return Mono.just(value); // Can this be done cleaner?
    }).flatMap(this.serverRepository::save).subscribe();

    return Mono.just(Collections.singletonMap("success", true));
}