From 928639bd98f25975e587ff2949abd7031d22a7ab Mon Sep 17 00:00:00 2001 From: Zakharov_Rostislav Date: Fri, 19 Apr 2024 15:43:53 +0400 Subject: [PATCH] lab3 it works. finally --- .../com/ip/library/LibraryApplication.java | 20 ++--- .../library/authors/api/AuthorController.java | 18 ++++- .../library/authors/model/AuthorEntity.java | 29 ++++--- .../authors/repository/AuthorRepository.java | 8 +- .../authors/service/AuthorService.java | 12 ++- .../model/AuthorsBooksEntity.java | 81 +++++++++++++++++++ .../authors_books/model/AuthorsBooksId.java | 55 +++++++++++++ .../ip/library/books/api/BookController.java | 26 +++--- .../ip/library/books/model/BookEntity.java | 34 ++++---- .../books/repository/BookRepository.java | 8 ++ .../ip/library/books/service/BookService.java | 7 +- .../ip/library/users/api/UserController.java | 15 +++- .../ip/library/users/model/UserEntity.java | 1 - .../test/java/com/ip/library/BooksTests.java | 18 ++--- 14 files changed, 256 insertions(+), 76 deletions(-) create mode 100644 SpringApp/library/src/main/java/com/ip/library/authors_books/model/AuthorsBooksEntity.java create mode 100644 SpringApp/library/src/main/java/com/ip/library/authors_books/model/AuthorsBooksId.java diff --git a/SpringApp/library/src/main/java/com/ip/library/LibraryApplication.java b/SpringApp/library/src/main/java/com/ip/library/LibraryApplication.java index 73bcb0a..fba23dc 100644 --- a/SpringApp/library/src/main/java/com/ip/library/LibraryApplication.java +++ b/SpringApp/library/src/main/java/com/ip/library/LibraryApplication.java @@ -54,21 +54,17 @@ public class LibraryApplication implements CommandLineRunner { log.info("Create default authors values"); final var author1 = authorService.create(new AuthorEntity("author1")); final var author2 = authorService.create(new AuthorEntity("author2")); - - final List list1 = new ArrayList<>(); - final List list2 = new ArrayList<>(); - final List list3 = new ArrayList<>(); - - list1.add(author1); - list2.add(author2); - list3.add(author1); - list3.add(author2); log.info("Create default books values"); - final var book1 = bookService.create(new BookEntity("book1", type1, list1)); - final var book2 = bookService.create(new BookEntity("book2", type1, list2)); - final var book3 = bookService.create(new BookEntity("book3", type2, list3)); + final var book1 = bookService.create(new BookEntity("book1", type1)); + final var book2 = bookService.create(new BookEntity("book2", type1)); + final var book3 = bookService.create(new BookEntity("book3", type2)); final var book4 = bookService.create(new BookEntity("book4", type2)); + + authorService.addBook(author1.getId(), book1.getId()); + authorService.addBook(author2.getId(), book2.getId()); + authorService.addBook(author1.getId(), book3.getId()); + authorService.addBook(author2.getId(), book3.getId()); log.info("Create default users values"); final var user1 = userService.create(new UserEntity("user1", "123")); diff --git a/SpringApp/library/src/main/java/com/ip/library/authors/api/AuthorController.java b/SpringApp/library/src/main/java/com/ip/library/authors/api/AuthorController.java index 22f1a66..dbb9037 100644 --- a/SpringApp/library/src/main/java/com/ip/library/authors/api/AuthorController.java +++ b/SpringApp/library/src/main/java/com/ip/library/authors/api/AuthorController.java @@ -15,10 +15,11 @@ import org.springframework.web.bind.annotation.RestController; import com.ip.library.core.configuration.Constants; import com.ip.library.authors.model.AuthorEntity; import com.ip.library.authors.service.AuthorService; +import com.ip.library.books.api.BookDto; import com.ip.library.books.model.BookEntity; +import com.ip.library.books.service.BookService; import jakarta.validation.Valid; -import org.springframework.web.bind.annotation.RequestParam; @RestController @@ -26,11 +27,20 @@ import org.springframework.web.bind.annotation.RequestParam; public class AuthorController { private final AuthorService authorService; private final ModelMapper modelMapper; + private final BookService bookService; - public AuthorController(AuthorService authorService, ModelMapper modelMapper) { + public AuthorController(AuthorService authorService, ModelMapper modelMapper, BookService bookService) { this.authorService = authorService; this.modelMapper = modelMapper; + this.bookService = bookService; } + + private BookDto toBookDto (BookEntity entity) { + BookDto bookDto = modelMapper.map(entity, BookDto.class); + bookDto.setAuthorId(bookService.getBookAuthors( + entity.getId()).stream().map(x -> x.getId()).toList()); + return bookDto; + } private AuthorDto toDto(AuthorEntity entity) { return modelMapper.map(entity, AuthorDto.class); @@ -66,8 +76,8 @@ public class AuthorController { } @GetMapping("/{id}/books") - public List getAuthorBooks(@PathVariable(name = "id") Long id) { - return authorService.getAuthorBooks(id); + public List getAuthorBooks(@PathVariable(name = "id") Long id) { + return authorService.getAuthorBooks(id).stream().map(this::toBookDto).toList(); } } diff --git a/SpringApp/library/src/main/java/com/ip/library/authors/model/AuthorEntity.java b/SpringApp/library/src/main/java/com/ip/library/authors/model/AuthorEntity.java index 9a96e32..e1890f4 100644 --- a/SpringApp/library/src/main/java/com/ip/library/authors/model/AuthorEntity.java +++ b/SpringApp/library/src/main/java/com/ip/library/authors/model/AuthorEntity.java @@ -4,6 +4,7 @@ import java.util.HashSet; import java.util.Objects; import java.util.Set; +import com.ip.library.authors_books.model.AuthorsBooksEntity; import com.ip.library.books.model.BookEntity; import com.ip.library.core.model.BaseEntity; @@ -13,6 +14,8 @@ import jakarta.persistence.Entity; import jakarta.persistence.JoinColumn; import jakarta.persistence.JoinTable; import jakarta.persistence.ManyToMany; +import jakarta.persistence.OneToMany; +import jakarta.persistence.OrderBy; import jakarta.persistence.Table; @Entity @@ -20,13 +23,9 @@ import jakarta.persistence.Table; public class AuthorEntity extends BaseEntity { @Column(nullable = false, unique = true, length = 20) private String name; - @ManyToMany(cascade = { CascadeType.ALL }) - @JoinTable( - name = "authors_books", - joinColumns = { @JoinColumn(name = "author_id", nullable = false) }, - inverseJoinColumns = { @JoinColumn(name = "book_id", nullable = false) } - ) - private Set books = new HashSet<>(); + @OneToMany(mappedBy = "author", cascade = CascadeType.ALL, orphanRemoval = true) + @OrderBy("id ASC") + private Set authorsBooks = new HashSet<>(); public AuthorEntity() { super(); @@ -44,12 +43,20 @@ public class AuthorEntity extends BaseEntity { this.name = name; } - public Set getBooks() { - return books; + public Set getAuthorsBooks() { + return authorsBooks; } - public void setBooks(Set books) { - this.books = books; + public void setBooks(Set 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; } @Override diff --git a/SpringApp/library/src/main/java/com/ip/library/authors/repository/AuthorRepository.java b/SpringApp/library/src/main/java/com/ip/library/authors/repository/AuthorRepository.java index 5cd00a2..78559b9 100644 --- a/SpringApp/library/src/main/java/com/ip/library/authors/repository/AuthorRepository.java +++ b/SpringApp/library/src/main/java/com/ip/library/authors/repository/AuthorRepository.java @@ -15,10 +15,10 @@ public interface AuthorRepository extends PagingAndSortingRepository { Optional findByNameIgnoreCase(String name); @Query( - "select a.books " + - "from AuthorEntity a, BookEntity b " + - "where a.id = ?1 " + - "order by b.id" + "select ab.book " + + "from AuthorsBooksEntity ab " + + "where ab.author.id = ?1 " + + "order by ab.book.id" ) List getAuthorBooks(Long authorId); } diff --git a/SpringApp/library/src/main/java/com/ip/library/authors/service/AuthorService.java b/SpringApp/library/src/main/java/com/ip/library/authors/service/AuthorService.java index a93ffcf..fa324b7 100644 --- a/SpringApp/library/src/main/java/com/ip/library/authors/service/AuthorService.java +++ b/SpringApp/library/src/main/java/com/ip/library/authors/service/AuthorService.java @@ -10,13 +10,16 @@ import com.ip.library.core.error.NotFoundException; import com.ip.library.authors.model.AuthorEntity; import com.ip.library.authors.repository.AuthorRepository; import com.ip.library.books.model.BookEntity; +import com.ip.library.books.service.BookService; @Service public class AuthorService { private final AuthorRepository repository; + private final BookService bookService; - public AuthorService(AuthorRepository repository) { + public AuthorService(AuthorRepository repository, BookService bookService) { this.repository = repository; + this.bookService = bookService; } private void checkNameUniqueness(String name) { @@ -69,4 +72,11 @@ public class AuthorService { public List getAuthorBooks(long authorId) { return repository.getAuthorBooks(authorId); } + + @Transactional + public boolean addBook(long authorId, long bookId) { + final AuthorEntity existsAuthor = get(authorId); + final BookEntity book = bookService.get(bookId); + return existsAuthor.addBook(book); + } } diff --git a/SpringApp/library/src/main/java/com/ip/library/authors_books/model/AuthorsBooksEntity.java b/SpringApp/library/src/main/java/com/ip/library/authors_books/model/AuthorsBooksEntity.java new file mode 100644 index 0000000..b5fbd3e --- /dev/null +++ b/SpringApp/library/src/main/java/com/ip/library/authors_books/model/AuthorsBooksEntity.java @@ -0,0 +1,81 @@ +package com.ip.library.authors_books.model; + +import java.util.Objects; + +import com.ip.library.authors.model.AuthorEntity; +import com.ip.library.books.model.BookEntity; + +import jakarta.persistence.EmbeddedId; +import jakarta.persistence.Entity; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.MapsId; +import jakarta.persistence.Table; + +@Entity +@Table(name = "authors_books") +public class AuthorsBooksEntity { + @EmbeddedId + private AuthorsBooksId id = new AuthorsBooksId(); + @ManyToOne + @MapsId("authorId") + @JoinColumn(name = "author_id") + private AuthorEntity author; + @ManyToOne + @MapsId("bookId") + @JoinColumn(name = "book_id") + private BookEntity book; + + public AuthorsBooksEntity() {} + + public AuthorsBooksEntity(AuthorEntity author, BookEntity book) { + this.author = author; + this.book = book; + } + + public void setId(AuthorsBooksId id) { + this.id = id; + } + + public AuthorsBooksId getId() { + return id; + } + + public void setAuthor(AuthorEntity author) { + this.author = author; + if (!author.getAuthorsBooks().contains(this)) + author.getAuthorsBooks().add(this); + } + + public AuthorEntity getAuthor() { + return author; + } + + public void setBook(BookEntity book) { + this.book = book; + if (!book.getAuthorsBooks().contains(this)) + book.getAuthorsBooks().add(this); + } + + public BookEntity getBook() { + return book; + } + + @Override + public int hashCode() { + return Objects.hash(id, author.getId(), book.getId()); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null || getClass() != obj.getClass()) + return false; + final AuthorsBooksEntity other = (AuthorsBooksEntity) obj; + return + Objects.equals(other.getId(), id) && + Objects.equals(other.getAuthor().getId(), author.getId()) && + Objects.equals(other.getBook().getId(), book.getId()); + } +} diff --git a/SpringApp/library/src/main/java/com/ip/library/authors_books/model/AuthorsBooksId.java b/SpringApp/library/src/main/java/com/ip/library/authors_books/model/AuthorsBooksId.java new file mode 100644 index 0000000..4329897 --- /dev/null +++ b/SpringApp/library/src/main/java/com/ip/library/authors_books/model/AuthorsBooksId.java @@ -0,0 +1,55 @@ +package com.ip.library.authors_books.model; + +import java.util.Objects; +import java.util.Optional; + +import jakarta.persistence.Embeddable; + +@Embeddable +public class AuthorsBooksId { + private Long authorId; + private Long bookId; + + public AuthorsBooksId() {} + + public AuthorsBooksId(Long authorId, Long bookId) { + this.authorId = authorId; + this.bookId = bookId; + } + + public Long getAuthorId() { + return authorId; + } + + public void setAuthorId(Long authorId) { + this.authorId = authorId; + } + + public Long getBookId() { + return bookId; + } + + public void setBookId(Long bookId) { + this.bookId = bookId; + } + + @Override + public int hashCode() { + return Objects.hash( + Optional.ofNullable(authorId).orElse(-1L), + Optional.ofNullable(bookId).orElse(-1L) + ); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null || getClass() != obj.getClass()) + return false; + AuthorsBooksId other = (AuthorsBooksId) obj; + return + Objects.equals(other.authorId, authorId) && + Objects.equals(other.bookId, bookId); + } +} diff --git a/SpringApp/library/src/main/java/com/ip/library/books/api/BookController.java b/SpringApp/library/src/main/java/com/ip/library/books/api/BookController.java index 47f959b..8beb460 100644 --- a/SpringApp/library/src/main/java/com/ip/library/books/api/BookController.java +++ b/SpringApp/library/src/main/java/com/ip/library/books/api/BookController.java @@ -29,22 +29,24 @@ public class BookController { private final AuthorService authorService; private final ModelMapper modelMapper; - public BookController(BookService itemService, TypeService typeService, + public BookController(BookService bookService, TypeService typeService, AuthorService authorService, ModelMapper modelMapper) { - this.bookService = itemService; + this.bookService = bookService; this.typeService = typeService; this.authorService = authorService; this.modelMapper = modelMapper; } - - private BookDto toDto(BookEntity entity) { - return modelMapper.map(entity, BookDto.class); - } + + private BookDto toBookDto (BookEntity entity) { + BookDto bookDto = modelMapper.map(entity, BookDto.class); + bookDto.setAuthorId(bookService.getBookAuthors( + entity.getId()).stream().map(x -> x.getId()).toList()); + return bookDto; + } private BookEntity toEntity(BookDto dto) { final BookEntity entity = modelMapper.map(dto, BookEntity.class); entity.setType(typeService.get(dto.getTypeId())); - entity.setAuthors(dto.getAuthorsId().stream().map(authorService::get).toList()); return entity; } @@ -53,27 +55,27 @@ public class BookController { @RequestParam(name = "typeId", defaultValue = "-1") Long typeId, @RequestParam(name = "page", defaultValue = "0") int page, @RequestParam(name = "size", defaultValue = Constants.DEFAULT_PAGE_SIZE) int size) { - return bookService.getAll(typeId).stream().map(this::toDto).toList(); + return bookService.getAll(typeId).stream().map(this::toBookDto).toList(); } @GetMapping("/{id}") public BookDto get(@PathVariable(name = "id") Long id) { - return toDto(bookService.get(id)); + return toBookDto(bookService.get(id)); } @PostMapping public BookDto create(@RequestBody @Valid BookDto dto) { - return toDto(bookService.create(toEntity(dto))); + return toBookDto(bookService.create(toEntity(dto))); } @PutMapping("/{id}") public BookDto update(@PathVariable(name = "id") Long id, @RequestBody BookDto dto) { - return toDto(bookService.update(id, toEntity(dto))); + return toBookDto(bookService.update(id, toEntity(dto))); } @DeleteMapping("/{id}") public BookDto delete(@PathVariable(name = "id") Long id) { - return toDto(bookService.delete(id)); + return toBookDto(bookService.delete(id)); } @GetMapping("/{bookId}/users/number") diff --git a/SpringApp/library/src/main/java/com/ip/library/books/model/BookEntity.java b/SpringApp/library/src/main/java/com/ip/library/books/model/BookEntity.java index 568288c..b7c6317 100644 --- a/SpringApp/library/src/main/java/com/ip/library/books/model/BookEntity.java +++ b/SpringApp/library/src/main/java/com/ip/library/books/model/BookEntity.java @@ -7,12 +7,12 @@ import java.util.Objects; import java.util.Set; import com.ip.library.core.model.BaseEntity; -import com.ip.library.favorites.model.FavoriteEntity; import com.ip.library.types.model.TypeEntity; import jakarta.persistence.CascadeType; import jakarta.persistence.Column; import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToMany; import jakarta.persistence.ManyToOne; @@ -21,6 +21,7 @@ import jakarta.persistence.OrderBy; import jakarta.persistence.Table; import com.ip.library.authors.model.AuthorEntity; +import com.ip.library.authors_books.model.AuthorsBooksEntity; @Entity @Table(name = "books") @@ -31,9 +32,9 @@ public class BookEntity extends BaseEntity { @JoinColumn(name = "type_id", nullable = false) @OrderBy("id ASC") private TypeEntity type; - @ManyToMany(mappedBy = "books") + @OneToMany(mappedBy = "book", cascade = CascadeType.ALL, orphanRemoval = true) @OrderBy("id ASC") - private List authors = new ArrayList<>(); + private Set authorsBooks = new HashSet<>(); public BookEntity() { super(); @@ -44,12 +45,6 @@ public class BookEntity extends BaseEntity { this.type = type; } - public BookEntity(String name, TypeEntity type, List authors) { - this.name = name; - this.type = type; - this.authors = authors; - } - public String getName() { return name; } @@ -66,17 +61,25 @@ public class BookEntity extends BaseEntity { this.type = type; } - public List getAuthors() { - return authors; + public Set getAuthorsBooks() { + return authorsBooks; } - public void setAuthors(List authors) { - this.authors = authors; + public void setAuthors(Set authorsBooks) { + this.authorsBooks = authorsBooks; + } + + 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; } @Override public int hashCode() { - return Objects.hash(id, name, type, authors); + return Objects.hash(id, name, type); } @Override @@ -89,7 +92,6 @@ public class BookEntity extends BaseEntity { return Objects.equals(other.getId(), id) && Objects.equals(other.getName(), name) - && Objects.equals(other.getType(), type) - && Objects.equals(other.getAuthors(), authors); + && Objects.equals(other.getType(), type); } } diff --git a/SpringApp/library/src/main/java/com/ip/library/books/repository/BookRepository.java b/SpringApp/library/src/main/java/com/ip/library/books/repository/BookRepository.java index 95b8603..cd9e8f0 100644 --- a/SpringApp/library/src/main/java/com/ip/library/books/repository/BookRepository.java +++ b/SpringApp/library/src/main/java/com/ip/library/books/repository/BookRepository.java @@ -8,6 +8,7 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.PagingAndSortingRepository; +import com.ip.library.authors.model.AuthorEntity; import com.ip.library.books.model.BookEntity; public interface BookRepository extends @@ -24,4 +25,11 @@ public interface BookRepository extends "from FavoriteEntity f " + "where f.book.id = ?1") int getBookSubscribersNumber(long bookId); + @Query( + "select ab.author " + + "from AuthorsBooksEntity ab " + + "where ab.book.id = ?1 " + + "order by ab.author.id" + ) + List getBookAuthors(Long bookId); } diff --git a/SpringApp/library/src/main/java/com/ip/library/books/service/BookService.java b/SpringApp/library/src/main/java/com/ip/library/books/service/BookService.java index 0557de8..5631c87 100644 --- a/SpringApp/library/src/main/java/com/ip/library/books/service/BookService.java +++ b/SpringApp/library/src/main/java/com/ip/library/books/service/BookService.java @@ -7,6 +7,7 @@ import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import com.ip.library.authors.model.AuthorEntity; import com.ip.library.books.model.BookEntity; import com.ip.library.books.repository.BookRepository; import com.ip.library.core.error.NotFoundException; @@ -75,7 +76,6 @@ public class BookService { checkNameUniqueness(entity.getName()); existsEntity.setName(entity.getName()); existsEntity.setType(entity.getType()); - existsEntity.setAuthors(entity.getAuthors()); return repository.save(existsEntity); } @@ -90,4 +90,9 @@ public class BookService { public int getBookSubscribersNumber(long bookId) { return repository.getBookSubscribersNumber(bookId); } + + @Transactional(readOnly = true) + public List getBookAuthors(long bookId) { + return repository.getBookAuthors(bookId); + } } diff --git a/SpringApp/library/src/main/java/com/ip/library/users/api/UserController.java b/SpringApp/library/src/main/java/com/ip/library/users/api/UserController.java index 95696dd..c3a47c5 100644 --- a/SpringApp/library/src/main/java/com/ip/library/users/api/UserController.java +++ b/SpringApp/library/src/main/java/com/ip/library/users/api/UserController.java @@ -15,6 +15,7 @@ import org.springframework.web.bind.annotation.RestController; import com.ip.library.books.api.BookDto; import com.ip.library.books.model.BookEntity; +import com.ip.library.books.service.BookService; import com.ip.library.core.api.PageDto; import com.ip.library.core.api.PageDtoMapper; import com.ip.library.core.configuration.Constants; @@ -28,18 +29,24 @@ import jakarta.validation.Valid; public class UserController { private final UserService userService; private final ModelMapper modelMapper; + private final BookService bookService; - public UserController(UserService userService, ModelMapper modelMapper) { + public UserController(UserService userService, ModelMapper modelMapper, BookService bookService) { this.userService = userService; this.modelMapper = modelMapper; + this.bookService = bookService; } + + private BookDto toBookDto (BookEntity entity) { + BookDto bookDto = modelMapper.map(entity, BookDto.class); + bookDto.setAuthorId(bookService.getBookAuthors( + entity.getId()).stream().map(x -> x.getId()).toList()); + return bookDto; + } private UserDto toUserDto(UserEntity entity) { return modelMapper.map(entity, UserDto.class); } - private BookDto toBookDto (BookEntity entity) { - return modelMapper.map(entity, BookDto.class); - } private UserEntity toUserEntity(UserDto dto) { return modelMapper.map(dto, UserEntity.class); diff --git a/SpringApp/library/src/main/java/com/ip/library/users/model/UserEntity.java b/SpringApp/library/src/main/java/com/ip/library/users/model/UserEntity.java index 81471d1..756654a 100644 --- a/SpringApp/library/src/main/java/com/ip/library/users/model/UserEntity.java +++ b/SpringApp/library/src/main/java/com/ip/library/users/model/UserEntity.java @@ -7,7 +7,6 @@ import java.util.Set; import com.ip.library.books.model.BookEntity; import com.ip.library.core.model.BaseEntity; import com.ip.library.favorites.model.FavoriteEntity; -import com.ip.library.favorites.model.UserBookId; import jakarta.persistence.CascadeType; import jakarta.persistence.Column; diff --git a/SpringApp/library/src/test/java/com/ip/library/BooksTests.java b/SpringApp/library/src/test/java/com/ip/library/BooksTests.java index a95b233..a72ea75 100644 --- a/SpringApp/library/src/test/java/com/ip/library/BooksTests.java +++ b/SpringApp/library/src/test/java/com/ip/library/BooksTests.java @@ -51,7 +51,7 @@ class BooksTests { list1.add(author2); bookService.create(new BookEntity("book1", type)); bookService.create(new BookEntity("book2", type2)); - book = bookService.create(new BookEntity("book3", type, list1)); + book = bookService.create(new BookEntity("book3", type)); } @Test @@ -59,8 +59,6 @@ class BooksTests { Assertions.assertEquals(3, bookService.getAll().size()); Assertions.assertEquals("book3", book.getName()); Assertions.assertEquals(type, book.getType()); - Assertions.assertTrue(book.getAuthors().contains(author1)); - Assertions.assertTrue(book.getAuthors().contains(author2)); Assertions.assertEquals(0, bookService.getBookSubscribersNumber(book.getId())); } @@ -86,7 +84,7 @@ class BooksTests { bookService.delete(book.getId()); Assertions.assertEquals(2, bookService.getAll().size()); final BookEntity newEntity = bookService.create( - new BookEntity(book.getName(), book.getType(), book.getAuthors())); + new BookEntity(book.getName(), book.getType())); Assertions.assertEquals(3, bookService.getAll().size()); Assertions.assertNotEquals(book.getId(), newEntity.getId()); } @@ -95,11 +93,11 @@ class BooksTests { void nullNameTest() { Assertions.assertThrows( DataIntegrityViolationException.class, - () -> bookService.create(new BookEntity(null, book.getType(), book.getAuthors())) + () -> bookService.create(new BookEntity(null, book.getType())) ); Assertions.assertThrows( DataIntegrityViolationException.class, - () -> bookService.update(book.getId(), new BookEntity(null, book.getType(), book.getAuthors())) + () -> bookService.update(book.getId(), new BookEntity(null, book.getType())) ); } @@ -107,11 +105,11 @@ class BooksTests { void nullTypeTest() { Assertions.assertThrows( DataIntegrityViolationException.class, - () -> bookService.create(new BookEntity(book.getName() + "TEST", null, book.getAuthors())) + () -> bookService.create(new BookEntity(book.getName() + "TEST", null)) ); Assertions.assertThrows( DataIntegrityViolationException.class, - () -> bookService.update(book.getId(), new BookEntity(book.getName() + "TEST", null, book.getAuthors())) + () -> bookService.update(book.getId(), new BookEntity(book.getName() + "TEST", null)) ); } @@ -119,11 +117,11 @@ class BooksTests { void uniqueNameTest() { Assertions.assertThrows( IllegalArgumentException.class, - () -> bookService.create(new BookEntity(book.getName(), book.getType(), book.getAuthors())) + () -> bookService.create(new BookEntity(book.getName(), book.getType())) ); Assertions.assertThrows( IllegalArgumentException.class, - () -> bookService.update(book.getId(), new BookEntity(book.getName(), book.getType(), book.getAuthors())) + () -> bookService.update(book.getId(), new BookEntity(book.getName(), book.getType())) ); } }