mvc fix author deletion problem

This commit is contained in:
Zakharov_Rostislav 2024-06-09 13:02:11 +04:00
parent aa707d530e
commit a6c50d4fcd
3 changed files with 37 additions and 15 deletions

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) @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<>();
@ -71,6 +71,12 @@ public class BookEntity extends BaseEntity {
return authorsBooks.add(entity); return authorsBooks.add(entity);
} }
public void removeAuthor(AuthorsBooksEntity authorBookRecord) {
if (authorsBooks.contains(authorBookRecord)) {
authorsBooks.remove(authorBookRecord);
}
}
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(id, name, type); return Objects.hash(id, name, type);

View File

@ -92,7 +92,7 @@ public class BookService {
} }
@Transactional @Transactional
public BookEntity update(long id, BookEntity entity, List<Long> authorsId) { public BookEntity update(long id, BookEntity entity) {
if (entity == null) { if (entity == null) {
throw new IllegalArgumentException("Updating BookEntity is null"); throw new IllegalArgumentException("Updating BookEntity is null");
} }
@ -100,6 +100,12 @@ public class BookService {
existsEntity.setName(entity.getName()); existsEntity.setName(entity.getName());
existsEntity.setType(entity.getType()); existsEntity.setType(entity.getType());
bookRepository.save(existsEntity); bookRepository.save(existsEntity);
return get(id);
}
@Transactional
public BookEntity update(long id, BookEntity entity, List<Long> authorsId) {
update(id, entity);
updateAuthors(id, authorsId); updateAuthors(id, authorsId);
return get(id); return get(id);
} }
@ -129,11 +135,15 @@ public class BookService {
} }
@Transactional @Transactional
public AuthorsBooksEntity removeAuthor(long authorId, long bookId) { public BookEntity removeAuthor(long authorId, long bookId) {
final AuthorsBooksEntity existsEntity = authorsBooksRepository.findById(new AuthorsBooksId(authorId, bookId)) final AuthorsBooksEntity authorBookRecord = authorsBooksRepository.findById(
.orElseThrow(() -> new IllegalArgumentException("Invalid id")); new AuthorsBooksId(authorId, bookId))
authorsBooksRepository.delete(existsEntity); .orElseThrow(() -> new IllegalArgumentException("Invalid id")
return existsEntity; );
final BookEntity book = get(bookId);
authorsBooksRepository.delete(authorBookRecord);
book.removeAuthor(authorBookRecord);
return book;
} }
@Transactional @Transactional
@ -150,9 +160,9 @@ public class BookService {
@Transactional @Transactional
public void updateAuthors(long bookId, List<Long> authorsId) { public void updateAuthors(long bookId, List<Long> authorsId) {
BookEntity book = get(bookId); List<AuthorEntity> oldAuthors = getAuthors(bookId);
for (AuthorsBooksEntity ab : book.getAuthorsBooks()) { for (AuthorEntity author : oldAuthors) {
long currentAuthorId = ab.getId().getAuthorId(); long currentAuthorId = author.getId();
if (!authorsId.contains(currentAuthorId)) { if (!authorsId.contains(currentAuthorId)) {
removeAuthor(currentAuthorId, bookId); removeAuthor(currentAuthorId, bookId);
} }

View File

@ -86,15 +86,21 @@ class BooksTests {
final String testName = book1.getName() + "TEST"; final String testName = book1.getName() + "TEST";
final TypeEntity testType = typeService.create( final TypeEntity testType = typeService.create(
new TypeEntity(book1.getType().getName() + "TEST")); 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")); AuthorEntity testAuthor = authorService.create(new AuthorEntity("author"));
final List<Long> testAuthors = Arrays.asList(new Long[] { final List<Long> testAuthors = Arrays.asList(new Long[] {
testAuthor.getId() testAuthor.getId()
}); });
book1 = bookService.update(book1.getId(), new BookEntity(testName, testType), testAuthors); book1 = bookService.update(book1.getId(), book1, testAuthors);
Assertions.assertEquals(3, bookService.getAll().size()); Assertions.assertTrue(bookService.getAuthors(book1.getId()).contains(testAuthor));
Assertions.assertEquals(testName, book1.getName()); Assertions.assertEquals(1, bookService.getAuthors(book1.getId()).size());
Assertions.assertEquals(testType, book1.getType());
Assertions.assertEquals(testAuthors.size(), bookService.getAuthors(book1.getId()).size());
} }
@Test @Test