revert 46918a325f2c18931e1e0edb8affb3022d56a40f
revert потом мб откатить
This commit is contained in:
parent
46918a325f
commit
e199691eeb
@ -1,13 +1,18 @@
|
|||||||
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
|
||||||
@ -15,6 +20,9 @@ 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();
|
||||||
@ -32,11 +40,20 @@ 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 true;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -43,6 +43,8 @@ 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() {
|
||||||
|
@ -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)
|
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||||
@OrderBy("id ASC")
|
@OrderBy("id ASC")
|
||||||
private Set<AuthorsBooksEntity> authorsBooks = new HashSet<>();
|
private Set<AuthorsBooksEntity> authorsBooks = new HashSet<>();
|
||||||
|
|
||||||
@ -68,7 +68,10 @@ 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);
|
||||||
return authorsBooks.add(entity);
|
boolean result = authorsBooks.add(entity);
|
||||||
|
if (!author.getAuthorsBooks().contains(entity))
|
||||||
|
author.getAuthorsBooks().add(entity);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -9,8 +9,6 @@ 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> {
|
||||||
@ -55,11 +53,4 @@ 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);
|
|
||||||
}
|
}
|
||||||
|
@ -116,11 +116,6 @@ 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);
|
||||||
|
@ -80,5 +80,9 @@ 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()))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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, bookService.getAuthors(book1.getId()).size());
|
Assertions.assertEquals(2, book1.getAuthorsBooks().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(), bookService.getAuthors(book1.getId()).size());
|
Assertions.assertEquals(testAuthors.size(), book1.getAuthorsBooks().size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -142,6 +142,11 @@ 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
|
||||||
@ -163,10 +168,12 @@ class BooksTests {
|
|||||||
@Test
|
@Test
|
||||||
void removeAuthorTest() {
|
void removeAuthorTest() {
|
||||||
Assertions.assertTrue(
|
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());
|
bookService.removeAuthor(author1.getId(), book1.getId());
|
||||||
book1 = bookService.get(book1.getId());
|
book1 = bookService.get(book1.getId());
|
||||||
Assertions.assertTrue(
|
Assertions.assertTrue(
|
||||||
!bookService.getAuthors(book1.getId()).contains(author1));
|
!book1.getAuthorsBooks().stream().map(ab -> ab.getId().getAuthorId())
|
||||||
|
.toList().contains(author1.getId()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -82,5 +82,9 @@ 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()))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -120,5 +120,9 @@ 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()))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user