потом мб откатить
This commit is contained in:
parent
c8b26c8e24
commit
46918a325f
@ -1,18 +1,13 @@
|
||||
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
|
||||
@ -20,9 +15,6 @@ 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<AuthorsBooksEntity> authorsBooks = new HashSet<>();
|
||||
|
||||
public AuthorEntity() {
|
||||
super();
|
||||
@ -40,20 +32,11 @@ public class AuthorEntity extends BaseEntity {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Set<AuthorsBooksEntity> getAuthorsBooks() {
|
||||
return authorsBooks;
|
||||
}
|
||||
|
||||
public void setBooks(Set<AuthorsBooksEntity> 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 result;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -43,8 +43,6 @@ public class AuthorsBooksEntity {
|
||||
|
||||
public void setAuthor(AuthorEntity author) {
|
||||
this.author = author;
|
||||
if (!author.getAuthorsBooks().contains(this))
|
||||
author.getAuthorsBooks().add(this);
|
||||
}
|
||||
|
||||
public AuthorEntity getAuthor() {
|
||||
|
@ -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, fetch = FetchType.EAGER)
|
||||
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL)
|
||||
@OrderBy("id ASC")
|
||||
private Set<AuthorsBooksEntity> authorsBooks = new HashSet<>();
|
||||
|
||||
@ -68,10 +68,7 @@ public class BookEntity extends BaseEntity {
|
||||
|
||||
public boolean addAuthor(AuthorEntity author) {
|
||||
AuthorsBooksEntity entity = new AuthorsBooksEntity(author, this);
|
||||
boolean result = authorsBooks.add(entity);
|
||||
if (!author.getAuthorsBooks().contains(entity))
|
||||
author.getAuthorsBooks().add(entity);
|
||||
return result;
|
||||
return authorsBooks.add(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -9,6 +9,8 @@ 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<BookEntity, Long>,
|
||||
PagingAndSortingRepository<BookEntity, Long> {
|
||||
@ -53,4 +55,11 @@ 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<AuthorEntity> getAuthors(Long bookId);
|
||||
}
|
||||
|
@ -116,6 +116,11 @@ public class BookService {
|
||||
return bookRepository.getBookSubscribersNumber(bookId);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<AuthorEntity> getAuthors(long bookId) {
|
||||
return bookRepository.getAuthors(bookId);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public boolean addAuthor(long authorId, long bookId) {
|
||||
final AuthorEntity existsAuthor = authorService.get(authorId);
|
||||
|
@ -80,9 +80,5 @@ class AuthorsTests {
|
||||
IllegalArgumentException.class,
|
||||
() -> authorService.create(new AuthorEntity(entity.getName()))
|
||||
);
|
||||
Assertions.assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> authorService.update(entity.getId(), new AuthorEntity(entity.getName()))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -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, book1.getAuthorsBooks().size());
|
||||
Assertions.assertEquals(2, bookService.getAuthors(book1.getId()).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(), book1.getAuthorsBooks().size());
|
||||
Assertions.assertEquals(testAuthors.size(), bookService.getAuthors(book1.getId()).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -142,11 +142,6 @@ 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
|
||||
@ -168,12 +163,10 @@ class BooksTests {
|
||||
@Test
|
||||
void removeAuthorTest() {
|
||||
Assertions.assertTrue(
|
||||
book1.getAuthorsBooks().stream().map(ab -> ab.getId().getAuthorId())
|
||||
.toList().contains(author1.getId()));
|
||||
bookService.getAuthors(book1.getId()).contains(author1));
|
||||
bookService.removeAuthor(author1.getId(), book1.getId());
|
||||
book1 = bookService.get(book1.getId());
|
||||
Assertions.assertTrue(
|
||||
!book1.getAuthorsBooks().stream().map(ab -> ab.getId().getAuthorId())
|
||||
.toList().contains(author1.getId()));
|
||||
!bookService.getAuthors(book1.getId()).contains(author1));
|
||||
}
|
||||
}
|
||||
|
@ -82,9 +82,5 @@ class TypesTests {
|
||||
IllegalArgumentException.class,
|
||||
() -> typeService.create(new TypeEntity(entity.getName()))
|
||||
);
|
||||
Assertions.assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> typeService.update(entity.getId(), new TypeEntity(entity.getName()))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -120,9 +120,5 @@ class UsersTests {
|
||||
IllegalArgumentException.class,
|
||||
() -> userService.create(new UserEntity(user.getLogin(), user.getPassword()))
|
||||
);
|
||||
Assertions.assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> userService.update(user.getId(), new UserEntity(user.getLogin()))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user