1) Contextualization about the problem:
I am trying 'delete' items in 03 different DB-Collections(Reactive MongoDB), using 03 different services/repo (userService + postService + userRepo);
My Goal is DELETE an object (in each collection) simultaneously, using the same chaining-code;
Below is the code for the above situation:
1.1) Code:
Current working status: not working;
Current behaviour: not executes any delete, weither delete-userService, delete-postService, or delete-userRepo.
@Slf4j
@Service
@AllArgsConstructor
public class UserService implements UserServiceInt {
private final UserRepo userRepo;
private final PostServiceInt postServ;
private final CommentServiceInt comServ;
private final CustomExceptions customExceptions;
@Override
public Mono<Void> deleteInThreeCollections(String id) {
return userRepo
.findById(id)
.switchIfEmpty(customExceptions.userNotFoundException())
.map(user -> {
userRepo.delete(user); // First deletion - delete-method from userRepo
return user;
})
.flatMapMany(user -> postServ.findPostsByAuthorId(user.getId()))
.map(post -> {
postServ.delete(post); // Second deletion - delete-method from postService
return post;
})
.flatMap(post -> comServ.findCommentsByPostId(post.getPostId()))
.map(comServ::delete) // Third deletion - delete-method from commentService
.then()
;
}
}
2) Question:
- How Can I delete different elements in different DB-Collections,
- by using only ONE ‘chained delete method’ that uses THREE ‘delete methods’ from
- THREE different services/repo (userService + postService + userRepo?
- by using only ONE ‘chained delete method’ that uses THREE ‘delete methods’ from
3) Updating:
Solution Found
@Override
public Mono<Void> deleteInThreeCollections(String id) {
return userRepo
.findById(id)
.switchIfEmpty(customExceptions.userNotFoundException())
.flatMap(
user -> postServ
.findPostsByAuthorId(user.getId())
.flatMap(
post -> comServ.findCommentsByPostId(
post.getPostId())
.flatMap(comServ::delete)
.thenMany(
postServ.findPostsByAuthorId(
post.getAuthor()
.getId()))
.flatMap(postServ::delete)
)
.then(userRepo.delete(user))
);
}
Thanks a lot for any help