Project-Books / books-api

GraphQL Books API
https://project-books.github.io/#books-api
MIT License
35 stars 60 forks source link

Add mutation to delete an Author and all of its books #90

Closed knjk04 closed 2 years ago

knjk04 commented 3 years ago

Add a mutation in AuthorMutation that deletes the Author (specified by its ID) and all of its books.

removeBooks() should be used

Michaelg2021 commented 3 years ago

Hi I would like to work on this issue

knjk04 commented 3 years ago

Sure, thanks!

knjk04 commented 3 years ago

@Michaelg2021 How are you getting on with this?

Michaelg2021 commented 3 years ago

I'll have it done by the end of the week. Got my progress held up by school work last week, my apologies.

knjk04 commented 3 years ago

@Michaelg2021 Do you have an update on this?

knjk04 commented 3 years ago

@Michaelg2021 Unassigning due to no response

Syemon commented 3 years ago

Hi, can I grab this one?

knjk04 commented 3 years ago

Hi @Syemon, sure. Thanks!

Syemon commented 3 years ago

@knjk04 I see that a book can have multiple authors. Should I delete book only if there are no relations with authors left?

knjk04 commented 3 years ago

@Syemon Yes, good idea!

Syemon commented 3 years ago

@knjk04 I have problems with deleting books with only one relation. I have tried this approach.

    public void deleteAuthor(@NonNull Author author) {
        Set<Book> booksWithOneAuthor = author.getBooks()
                .stream()
                .filter(book -> book.getAuthors().size() == 1)
                .collect(Collectors.toSet());
        Set<Book> books = author.getBooks();
        books.forEach(author::removeBook);

        bookRepository.deleteAll(booksWithOneAuthor);

        authorRepository.deleteById(author.getId());
    }

And it appears that records of book_author table (the ones with two authors) were not cleared Cannot delete or update a parent row: a foreign key constraint fails (`booksapi`.`book_author`, CONSTRAINT `FKbjqhp85wjv8vpr0beygh6jsgo` FOREIGN KEY (`author_id`) REFERENCES `author` (`id`)) Can I take an approach where I would delete such relations in book_author using native query?