From e199691eeb427c7beb40006d6f46e5f4600cd74c Mon Sep 17 00:00:00 2001 From: Zakharov_Rostislav Date: Fri, 7 Jun 2024 18:27:41 +0400 Subject: [PATCH] revert 46918a325f2c18931e1e0edb8affb3022d56a40f MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit revert потом мб откатить --- .../controllers/authors/AuthorEntity.java | 19 ++++++++++++++++++- .../authors_books/AuthorsBooksEntity.java | 2 ++ .../library/controllers/books/BookEntity.java | 7 +++++-- .../controllers/books/BookRepository.java | 9 --------- .../controllers/books/BookService.java | 5 ----- .../java/com/ip/library/AuthorsTests.java | 4 ++++ .../test/java/com/ip/library/BooksTests.java | 15 +++++++++++---- .../test/java/com/ip/library/TypesTests.java | 4 ++++ .../test/java/com/ip/library/UsersTests.java | 4 ++++ 9 files changed, 48 insertions(+), 21 deletions(-) diff --git a/SpringApp/library/src/main/java/com/ip/library/controllers/authors/AuthorEntity.java b/SpringApp/library/src/main/java/com/ip/library/controllers/authors/AuthorEntity.java index 8fc1191..629efec 100644 --- a/SpringApp/library/src/main/java/com/ip/library/controllers/authors/AuthorEntity.java +++ b/SpringApp/library/src/main/java/com/ip/library/controllers/authors/AuthorEntity.java @@ -1,13 +1,18 @@ package com.ip.library.controllers.authors; +import java.util.HashSet; import java.util.Objects; +import java.util.Set; import com.ip.library.controllers.authors_books.AuthorsBooksEntity; import com.ip.library.controllers.books.BookEntity; import com.ip.library.core.model.BaseEntity; +import jakarta.persistence.CascadeType; import jakarta.persistence.Column; import jakarta.persistence.Entity; +import jakarta.persistence.OneToMany; +import jakarta.persistence.OrderBy; import jakarta.persistence.Table; @Entity @@ -15,6 +20,9 @@ import jakarta.persistence.Table; public class AuthorEntity extends BaseEntity { @Column(nullable = false, unique = true, length = 20) private String name; + @OneToMany(mappedBy = "author", cascade = CascadeType.ALL, orphanRemoval = true) + @OrderBy("id ASC") + private Set authorsBooks = new HashSet<>(); public AuthorEntity() { super(); @@ -32,11 +40,20 @@ public class AuthorEntity extends BaseEntity { this.name = name; } + public Set getAuthorsBooks() { + return authorsBooks; + } + + public void setBooks(Set authorsBooks) { + this.authorsBooks = authorsBooks; + } + public boolean addBook(BookEntity book) { AuthorsBooksEntity entity = new AuthorsBooksEntity(this, book); + boolean result = authorsBooks.add(entity); if (!book.getAuthorsBooks().contains(entity)) book.getAuthorsBooks().add(entity); - return true; + return result; } @Override diff --git a/SpringApp/library/src/main/java/com/ip/library/controllers/authors_books/AuthorsBooksEntity.java b/SpringApp/library/src/main/java/com/ip/library/controllers/authors_books/AuthorsBooksEntity.java index 3e809fa..e9662ca 100644 --- a/SpringApp/library/src/main/java/com/ip/library/controllers/authors_books/AuthorsBooksEntity.java +++ b/SpringApp/library/src/main/java/com/ip/library/controllers/authors_books/AuthorsBooksEntity.java @@ -43,6 +43,8 @@ public class AuthorsBooksEntity { public void setAuthor(AuthorEntity author) { this.author = author; + if (!author.getAuthorsBooks().contains(this)) + author.getAuthorsBooks().add(this); } public AuthorEntity getAuthor() { 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..b292f10 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<>(); @@ -68,7 +68,10 @@ public class BookEntity extends BaseEntity { public boolean addAuthor(AuthorEntity author) { AuthorsBooksEntity entity = new AuthorsBooksEntity(author, this); - return authorsBooks.add(entity); + boolean result = authorsBooks.add(entity); + if (!author.getAuthorsBooks().contains(entity)) + author.getAuthorsBooks().add(entity); + return result; } @Override diff --git a/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookRepository.java b/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookRepository.java index 42a1b41..1d322f5 100644 --- a/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookRepository.java +++ b/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookRepository.java @@ -9,8 +9,6 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.PagingAndSortingRepository; -import com.ip.library.controllers.authors.AuthorEntity; - public interface BookRepository extends CrudRepository, PagingAndSortingRepository { @@ -55,11 +53,4 @@ public interface BookRepository extends "from FavoriteEntity f " + "where f.book.id = ?1") int getBookSubscribersNumber(long bookId); - - @Query( - "select f.author " + - "from AuthorsBooksEntity f " + - "where f.book.id = ?1 " + - "order by f.author.id") - public List getAuthors(Long bookId); } 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..4eee584 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 @@ -116,11 +116,6 @@ public class BookService { return bookRepository.getBookSubscribersNumber(bookId); } - @Transactional(readOnly = true) - public List getAuthors(long bookId) { - return bookRepository.getAuthors(bookId); - } - @Transactional public boolean addAuthor(long authorId, long bookId) { final AuthorEntity existsAuthor = authorService.get(authorId); diff --git a/SpringApp/library/src/test/java/com/ip/library/AuthorsTests.java b/SpringApp/library/src/test/java/com/ip/library/AuthorsTests.java index ff38049..52b1f8b 100644 --- a/SpringApp/library/src/test/java/com/ip/library/AuthorsTests.java +++ b/SpringApp/library/src/test/java/com/ip/library/AuthorsTests.java @@ -80,5 +80,9 @@ class AuthorsTests { IllegalArgumentException.class, () -> authorService.create(new AuthorEntity(entity.getName())) ); + Assertions.assertThrows( + IllegalArgumentException.class, + () -> authorService.update(entity.getId(), new AuthorEntity(entity.getName())) + ); } } 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..d5bf08e 100644 --- a/SpringApp/library/src/test/java/com/ip/library/BooksTests.java +++ b/SpringApp/library/src/test/java/com/ip/library/BooksTests.java @@ -71,7 +71,7 @@ class BooksTests { Assertions.assertEquals(3, bookService.getAll().size()); Assertions.assertEquals("book1", book1.getName()); Assertions.assertEquals(type1, book1.getType()); - Assertions.assertEquals(2, bookService.getAuthors(book1.getId()).size()); + Assertions.assertEquals(2, book1.getAuthorsBooks().size()); Assertions.assertEquals(0, bookService.getBookSubscribersNumber(book1.getId())); } @@ -94,7 +94,7 @@ class BooksTests { 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()); + Assertions.assertEquals(testAuthors.size(), book1.getAuthorsBooks().size()); } @Test @@ -142,6 +142,11 @@ class BooksTests { () -> bookService.create(new BookEntity(book1.getName(), book1.getType()), new ArrayList<>()) ); + Assertions.assertThrows( + IllegalArgumentException.class, + () -> bookService.update(book1.getId(), new BookEntity(book1.getName(), book1.getType()), + new ArrayList<>()) + ); } @Test @@ -163,10 +168,12 @@ class BooksTests { @Test void removeAuthorTest() { Assertions.assertTrue( - bookService.getAuthors(book1.getId()).contains(author1)); + book1.getAuthorsBooks().stream().map(ab -> ab.getId().getAuthorId()) + .toList().contains(author1.getId())); bookService.removeAuthor(author1.getId(), book1.getId()); book1 = bookService.get(book1.getId()); Assertions.assertTrue( - !bookService.getAuthors(book1.getId()).contains(author1)); + !book1.getAuthorsBooks().stream().map(ab -> ab.getId().getAuthorId()) + .toList().contains(author1.getId())); } } diff --git a/SpringApp/library/src/test/java/com/ip/library/TypesTests.java b/SpringApp/library/src/test/java/com/ip/library/TypesTests.java index 836b23b..578c453 100644 --- a/SpringApp/library/src/test/java/com/ip/library/TypesTests.java +++ b/SpringApp/library/src/test/java/com/ip/library/TypesTests.java @@ -82,5 +82,9 @@ class TypesTests { IllegalArgumentException.class, () -> typeService.create(new TypeEntity(entity.getName())) ); + Assertions.assertThrows( + IllegalArgumentException.class, + () -> typeService.update(entity.getId(), new TypeEntity(entity.getName())) + ); } } diff --git a/SpringApp/library/src/test/java/com/ip/library/UsersTests.java b/SpringApp/library/src/test/java/com/ip/library/UsersTests.java index f9908f8..a9cae15 100644 --- a/SpringApp/library/src/test/java/com/ip/library/UsersTests.java +++ b/SpringApp/library/src/test/java/com/ip/library/UsersTests.java @@ -120,5 +120,9 @@ class UsersTests { IllegalArgumentException.class, () -> userService.create(new UserEntity(user.getLogin(), user.getPassword())) ); + Assertions.assertThrows( + IllegalArgumentException.class, + () -> userService.update(user.getId(), new UserEntity(user.getLogin())) + ); } }