потом мб откатить

This commit is contained in:
Zakharov_Rostislav 2024-06-07 18:24:44 +04:00
parent c8b26c8e24
commit 46918a325f
9 changed files with 21 additions and 48 deletions

View File

@ -1,18 +1,13 @@
package com.ip.library.controllers.authors; package com.ip.library.controllers.authors;
import java.util.HashSet;
import java.util.Objects; import java.util.Objects;
import java.util.Set;
import com.ip.library.controllers.authors_books.AuthorsBooksEntity; import com.ip.library.controllers.authors_books.AuthorsBooksEntity;
import com.ip.library.controllers.books.BookEntity; import com.ip.library.controllers.books.BookEntity;
import com.ip.library.core.model.BaseEntity; import com.ip.library.core.model.BaseEntity;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column; import jakarta.persistence.Column;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OrderBy;
import jakarta.persistence.Table; import jakarta.persistence.Table;
@Entity @Entity
@ -20,9 +15,6 @@ import jakarta.persistence.Table;
public class AuthorEntity extends BaseEntity { public class AuthorEntity extends BaseEntity {
@Column(nullable = false, unique = true, length = 20) @Column(nullable = false, unique = true, length = 20)
private String name; private String name;
@OneToMany(mappedBy = "author", cascade = CascadeType.ALL, orphanRemoval = true)
@OrderBy("id ASC")
private Set<AuthorsBooksEntity> authorsBooks = new HashSet<>();
public AuthorEntity() { public AuthorEntity() {
super(); super();
@ -40,20 +32,11 @@ public class AuthorEntity extends BaseEntity {
this.name = name; this.name = name;
} }
public Set<AuthorsBooksEntity> getAuthorsBooks() {
return authorsBooks;
}
public void setBooks(Set<AuthorsBooksEntity> authorsBooks) {
this.authorsBooks = authorsBooks;
}
public boolean addBook(BookEntity book) { public boolean addBook(BookEntity book) {
AuthorsBooksEntity entity = new AuthorsBooksEntity(this, book); AuthorsBooksEntity entity = new AuthorsBooksEntity(this, book);
boolean result = authorsBooks.add(entity);
if (!book.getAuthorsBooks().contains(entity)) if (!book.getAuthorsBooks().contains(entity))
book.getAuthorsBooks().add(entity); book.getAuthorsBooks().add(entity);
return result; return true;
} }
@Override @Override

View File

@ -43,8 +43,6 @@ public class AuthorsBooksEntity {
public void setAuthor(AuthorEntity author) { public void setAuthor(AuthorEntity author) {
this.author = author; this.author = author;
if (!author.getAuthorsBooks().contains(this))
author.getAuthorsBooks().add(this);
} }
public AuthorEntity getAuthor() { public AuthorEntity getAuthor() {

View File

@ -29,7 +29,7 @@ public class BookEntity extends BaseEntity {
@JoinColumn(name = "type_id", nullable = false) @JoinColumn(name = "type_id", nullable = false)
@OrderBy("id ASC") @OrderBy("id ASC")
private TypeEntity type; private TypeEntity type;
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL, fetch = FetchType.EAGER) @OneToMany(mappedBy = "book", cascade = CascadeType.ALL)
@OrderBy("id ASC") @OrderBy("id ASC")
private Set<AuthorsBooksEntity> authorsBooks = new HashSet<>(); private Set<AuthorsBooksEntity> authorsBooks = new HashSet<>();
@ -68,10 +68,7 @@ public class BookEntity extends BaseEntity {
public boolean addAuthor(AuthorEntity author) { public boolean addAuthor(AuthorEntity author) {
AuthorsBooksEntity entity = new AuthorsBooksEntity(author, this); AuthorsBooksEntity entity = new AuthorsBooksEntity(author, this);
boolean result = authorsBooks.add(entity); return authorsBooks.add(entity);
if (!author.getAuthorsBooks().contains(entity))
author.getAuthorsBooks().add(entity);
return result;
} }
@Override @Override

View File

@ -9,6 +9,8 @@ import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.PagingAndSortingRepository;
import com.ip.library.controllers.authors.AuthorEntity;
public interface BookRepository extends public interface BookRepository extends
CrudRepository<BookEntity, Long>, CrudRepository<BookEntity, Long>,
PagingAndSortingRepository<BookEntity, Long> { PagingAndSortingRepository<BookEntity, Long> {
@ -53,4 +55,11 @@ public interface BookRepository extends
"from FavoriteEntity f " + "from FavoriteEntity f " +
"where f.book.id = ?1") "where f.book.id = ?1")
int getBookSubscribersNumber(long bookId); int getBookSubscribersNumber(long bookId);
@Query(
"select f.author " +
"from AuthorsBooksEntity f " +
"where f.book.id = ?1 " +
"order by f.author.id")
public List<AuthorEntity> getAuthors(Long bookId);
} }

View File

@ -116,6 +116,11 @@ public class BookService {
return bookRepository.getBookSubscribersNumber(bookId); return bookRepository.getBookSubscribersNumber(bookId);
} }
@Transactional(readOnly = true)
public List<AuthorEntity> getAuthors(long bookId) {
return bookRepository.getAuthors(bookId);
}
@Transactional @Transactional
public boolean addAuthor(long authorId, long bookId) { public boolean addAuthor(long authorId, long bookId) {
final AuthorEntity existsAuthor = authorService.get(authorId); final AuthorEntity existsAuthor = authorService.get(authorId);

View File

@ -80,9 +80,5 @@ class AuthorsTests {
IllegalArgumentException.class, IllegalArgumentException.class,
() -> authorService.create(new AuthorEntity(entity.getName())) () -> authorService.create(new AuthorEntity(entity.getName()))
); );
Assertions.assertThrows(
IllegalArgumentException.class,
() -> authorService.update(entity.getId(), new AuthorEntity(entity.getName()))
);
} }
} }

View File

@ -71,7 +71,7 @@ class BooksTests {
Assertions.assertEquals(3, bookService.getAll().size()); Assertions.assertEquals(3, bookService.getAll().size());
Assertions.assertEquals("book1", book1.getName()); Assertions.assertEquals("book1", book1.getName());
Assertions.assertEquals(type1, book1.getType()); Assertions.assertEquals(type1, book1.getType());
Assertions.assertEquals(2, book1.getAuthorsBooks().size()); Assertions.assertEquals(2, bookService.getAuthors(book1.getId()).size());
Assertions.assertEquals(0, bookService.getBookSubscribersNumber(book1.getId())); Assertions.assertEquals(0, bookService.getBookSubscribersNumber(book1.getId()));
} }
@ -94,7 +94,7 @@ class BooksTests {
Assertions.assertEquals(3, bookService.getAll().size()); Assertions.assertEquals(3, bookService.getAll().size());
Assertions.assertEquals(testName, book1.getName()); Assertions.assertEquals(testName, book1.getName());
Assertions.assertEquals(testType, book1.getType()); Assertions.assertEquals(testType, book1.getType());
Assertions.assertEquals(testAuthors.size(), book1.getAuthorsBooks().size()); Assertions.assertEquals(testAuthors.size(), bookService.getAuthors(book1.getId()).size());
} }
@Test @Test
@ -142,11 +142,6 @@ class BooksTests {
() -> bookService.create(new BookEntity(book1.getName(), book1.getType()), () -> bookService.create(new BookEntity(book1.getName(), book1.getType()),
new ArrayList<>()) new ArrayList<>())
); );
Assertions.assertThrows(
IllegalArgumentException.class,
() -> bookService.update(book1.getId(), new BookEntity(book1.getName(), book1.getType()),
new ArrayList<>())
);
} }
@Test @Test
@ -168,12 +163,10 @@ class BooksTests {
@Test @Test
void removeAuthorTest() { void removeAuthorTest() {
Assertions.assertTrue( Assertions.assertTrue(
book1.getAuthorsBooks().stream().map(ab -> ab.getId().getAuthorId()) bookService.getAuthors(book1.getId()).contains(author1));
.toList().contains(author1.getId()));
bookService.removeAuthor(author1.getId(), book1.getId()); bookService.removeAuthor(author1.getId(), book1.getId());
book1 = bookService.get(book1.getId()); book1 = bookService.get(book1.getId());
Assertions.assertTrue( Assertions.assertTrue(
!book1.getAuthorsBooks().stream().map(ab -> ab.getId().getAuthorId()) !bookService.getAuthors(book1.getId()).contains(author1));
.toList().contains(author1.getId()));
} }
} }

View File

@ -82,9 +82,5 @@ class TypesTests {
IllegalArgumentException.class, IllegalArgumentException.class,
() -> typeService.create(new TypeEntity(entity.getName())) () -> typeService.create(new TypeEntity(entity.getName()))
); );
Assertions.assertThrows(
IllegalArgumentException.class,
() -> typeService.update(entity.getId(), new TypeEntity(entity.getName()))
);
} }
} }

View File

@ -120,9 +120,5 @@ class UsersTests {
IllegalArgumentException.class, IllegalArgumentException.class,
() -> userService.create(new UserEntity(user.getLogin(), user.getPassword())) () -> userService.create(new UserEntity(user.getLogin(), user.getPassword()))
); );
Assertions.assertThrows(
IllegalArgumentException.class,
() -> userService.update(user.getId(), new UserEntity(user.getLogin()))
);
} }
} }