如何使用反应式 mongo 在 AbstractMongoEventListener 中正确实现 onBeforeDelete 方法?

问题描述

我是反应式编程的新手,现在我尝试在反应式堆栈(Web 通量、反应器、反应式 mongo)上重写我的测试 Spring Boot Web 应用程序。

我的应用程序是一个简单的图书馆,有 3 种类型的实体:书籍、流派、作者; 本书有一个关于流派和作者的链接。在我没有反应堆栈的旧应用程序中,当我尝试删除与某本书链接的流派或作者时,我在 onBeforeDelete 方法中处理了它,该方法在我的类中覆盖,从 AbstractMongoEventListener 扩展。

看起来像这样:

@Component
public class CascadeAuthorOrGenreDeleteMongoEventListener extends AbstractMongoEventListener<Object> {

    @Autowired
    private BookRepository bookRepository;

    @Autowired
    private AuthorRepository authorRepository;

    @Autowired
    private GenreRepository genreRepository;

    @Override
    public void onBeforeDelete(BeforeDeleteEvent<Object> event) {
        if (Objects.equals(event.getCollectionName(),"genre") || Objects.equals(event.getCollectionName(),"author")) {
            String id = String.valueOf(event.getSource().get("_id"));
            Optional<Genre> genre = genreRepository.findById(id);
            Optional<Author> author = authorRepository.findById(id);
            if (genre.isPresent()) {
                if (bookRepository.findFirstByGenre(genre.get()) != null) {
                    throw new RuntimeException("Genre has related books");
                }
            }
            if (author.isPresent()) {
                if (bookRepository.findFirstByAuthor(author.get()) != null) {
                    throw new RuntimeException("Author has related books");
                }
            }
        }
    }
}  

现在我尝试在反应式堆栈上编写相同的代码,我已经编写了一些代码示例,但我不确定它是否正确,也许我可以使用反应式函数做得更好?我自己想不通。

这是我的例子:

@Component
public class CascadeAuthorOrGenreDeleteMongoEventListener extends AbstractMongoEventListener<Object> {

    @Autowired
    private BookRepository bookRepository;

    @Autowired
    private AuthorRepository authorRepository;

    @Autowired
    private GenreRepository genreRepository;

    @Override
    public void onBeforeDelete(BeforeDeleteEvent<Object> event) {
        if (Objects.equals(event.getCollectionName(),"author")) {
            String id = String.valueOf(event.getSource().get("_id"));
            genreRepository.findById(id)
                    .zipwith(authorRepository.findById(id))
                    .flatMap(tuple -> {
                        final Genre genre = tuple.getT1();
                        final Author author = tuple.getT2();

                        return bookRepository.findFirstByAuthor(author)
                                .switchIfEmpty(Mono.fromCallable(Book::new))
                                .zipwith(bookRepository.findFirstByGenre(genre)
                                        .switchIfEmpty(Mono.fromCallable(Book::new)));
                    }).subscribe(tuple -> {
                            final Book bookByAuthor = tuple.getT1();
                            final Book bookByGenre = tuple.getT2();
            
                            if (bookByAuthor.getId() != null) {
                                throw new RuntimeException("Author has related books");
                            }
                            if (bookByGenre.getId() != null) {
                                throw new RuntimeException("Genre has related books");
                            }
            });
        }
    }
}

是否正确或存在更好的方法

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)