From a6c50d4fcd7fde8aa10a5af77140d805272ebd5b Mon Sep 17 00:00:00 2001 From: Zakharov_Rostislav Date: Sun, 9 Jun 2024 13:02:11 +0400 Subject: [PATCH] mvc fix author deletion problem --- .../library/controllers/books/BookEntity.java | 8 +++++- .../controllers/books/BookService.java | 28 +++++++++++++------ .../test/java/com/ip/library/BooksTests.java | 16 +++++++---- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookEntity.java b/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookEntity.java index 8d6a3bf..b1c441b 100644 --- a/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookEntity.java +++ b/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookEntity.java @@ -29,7 +29,7 @@ public class BookEntity extends BaseEntity { @JoinColumn(name = "type_id", nullable = false) @OrderBy("id ASC") private TypeEntity type; - @OneToMany(mappedBy = "book", cascade = CascadeType.ALL) + @OneToMany(mappedBy = "book", cascade = CascadeType.ALL, fetch = FetchType.EAGER) @OrderBy("id ASC") private Set authorsBooks = new HashSet<>(); @@ -71,6 +71,12 @@ public class BookEntity extends BaseEntity { return authorsBooks.add(entity); } + public void removeAuthor(AuthorsBooksEntity authorBookRecord) { + if (authorsBooks.contains(authorBookRecord)) { + authorsBooks.remove(authorBookRecord); + } + } + @Override public int hashCode() { return Objects.hash(id, name, type); diff --git a/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookService.java b/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookService.java index d1df4d1..b29c675 100644 --- a/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookService.java +++ b/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookService.java @@ -92,7 +92,7 @@ public class BookService { } @Transactional - public BookEntity update(long id, BookEntity entity, List authorsId) { + public BookEntity update(long id, BookEntity entity) { if (entity == null) { throw new IllegalArgumentException("Updating BookEntity is null"); } @@ -100,6 +100,12 @@ public class BookService { existsEntity.setName(entity.getName()); existsEntity.setType(entity.getType()); bookRepository.save(existsEntity); + return get(id); + } + + @Transactional + public BookEntity update(long id, BookEntity entity, List authorsId) { + update(id, entity); updateAuthors(id, authorsId); return get(id); } @@ -129,11 +135,15 @@ public class BookService { } @Transactional - public AuthorsBooksEntity removeAuthor(long authorId, long bookId) { - final AuthorsBooksEntity existsEntity = authorsBooksRepository.findById(new AuthorsBooksId(authorId, bookId)) - .orElseThrow(() -> new IllegalArgumentException("Invalid id")); - authorsBooksRepository.delete(existsEntity); - return existsEntity; + public BookEntity removeAuthor(long authorId, long bookId) { + final AuthorsBooksEntity authorBookRecord = authorsBooksRepository.findById( + new AuthorsBooksId(authorId, bookId)) + .orElseThrow(() -> new IllegalArgumentException("Invalid id") + ); + final BookEntity book = get(bookId); + authorsBooksRepository.delete(authorBookRecord); + book.removeAuthor(authorBookRecord); + return book; } @Transactional @@ -150,9 +160,9 @@ public class BookService { @Transactional public void updateAuthors(long bookId, List authorsId) { - BookEntity book = get(bookId); - for (AuthorsBooksEntity ab : book.getAuthorsBooks()) { - long currentAuthorId = ab.getId().getAuthorId(); + List oldAuthors = getAuthors(bookId); + for (AuthorEntity author : oldAuthors) { + long currentAuthorId = author.getId(); if (!authorsId.contains(currentAuthorId)) { removeAuthor(currentAuthorId, bookId); } diff --git a/SpringApp/library/src/test/java/com/ip/library/BooksTests.java b/SpringApp/library/src/test/java/com/ip/library/BooksTests.java index 9d104e2..cf5c4d5 100644 --- a/SpringApp/library/src/test/java/com/ip/library/BooksTests.java +++ b/SpringApp/library/src/test/java/com/ip/library/BooksTests.java @@ -86,15 +86,21 @@ class BooksTests { final String testName = book1.getName() + "TEST"; final TypeEntity testType = typeService.create( new TypeEntity(book1.getType().getName() + "TEST")); + book1 = bookService.update(book1.getId(), new BookEntity(testName, testType)); + Assertions.assertEquals(3, bookService.getAll().size()); + Assertions.assertEquals(testName, book1.getName()); + Assertions.assertEquals(testType, book1.getType()); + } + + @Test + void updateAuthorsTest() { AuthorEntity testAuthor = authorService.create(new AuthorEntity("author")); final List testAuthors = Arrays.asList(new Long[] { testAuthor.getId() }); - book1 = bookService.update(book1.getId(), new BookEntity(testName, testType), testAuthors); - Assertions.assertEquals(3, bookService.getAll().size()); - Assertions.assertEquals(testName, book1.getName()); - Assertions.assertEquals(testType, book1.getType()); - Assertions.assertEquals(testAuthors.size(), bookService.getAuthors(book1.getId()).size()); + book1 = bookService.update(book1.getId(), book1, testAuthors); + Assertions.assertTrue(bookService.getAuthors(book1.getId()).contains(testAuthor)); + Assertions.assertEquals(1, bookService.getAuthors(book1.getId()).size()); } @Test