mvc fix author deletion problem
This commit is contained in:
parent
aa707d530e
commit
a6c50d4fcd
@ -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<AuthorsBooksEntity> 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);
|
||||
|
@ -92,7 +92,7 @@ public class BookService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public BookEntity update(long id, BookEntity entity, List<Long> 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<Long> 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<Long> authorsId) {
|
||||
BookEntity book = get(bookId);
|
||||
for (AuthorsBooksEntity ab : book.getAuthorsBooks()) {
|
||||
long currentAuthorId = ab.getId().getAuthorId();
|
||||
List<AuthorEntity> oldAuthors = getAuthors(bookId);
|
||||
for (AuthorEntity author : oldAuthors) {
|
||||
long currentAuthorId = author.getId();
|
||||
if (!authorsId.contains(currentAuthorId)) {
|
||||
removeAuthor(currentAuthorId, bookId);
|
||||
}
|
||||
|
@ -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<Long> 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
|
||||
|
Loading…
x
Reference in New Issue
Block a user