diff --git a/src/main/java/ru/ip/labworks/labworks/bookshop/controller/AuthorController.java b/src/main/java/ru/ip/labworks/labworks/bookshop/controller/AuthorController.java index e7e7a6a..9bd7c19 100644 --- a/src/main/java/ru/ip/labworks/labworks/bookshop/controller/AuthorController.java +++ b/src/main/java/ru/ip/labworks/labworks/bookshop/controller/AuthorController.java @@ -6,6 +6,7 @@ import ru.ip.labworks.labworks.configuration.WebConfiguration; import javax.validation.Valid; import java.io.IOException; import java.util.List; +import java.util.Map; @RestController @RequestMapping(WebConfiguration.REST_API + "/author") @@ -56,4 +57,9 @@ public class AuthorController { public void deleteAuthor(@PathVariable Long id){ authorService.deleteAuthor(id); } + + @GetMapping("/books") + public Map> getAuthorsBooks(){ + return authorService.AllAuthorsAndBooks(); + } } diff --git a/src/main/java/ru/ip/labworks/labworks/bookshop/repository/AuthorRepository.java b/src/main/java/ru/ip/labworks/labworks/bookshop/repository/AuthorRepository.java index 786e275..b167911 100644 --- a/src/main/java/ru/ip/labworks/labworks/bookshop/repository/AuthorRepository.java +++ b/src/main/java/ru/ip/labworks/labworks/bookshop/repository/AuthorRepository.java @@ -10,6 +10,6 @@ public interface AuthorRepository extends JpaRepository { @Query("select a.lastname as author, b.name as book " + "from Author a " + "join a.books b " + - "group by a.id") + "group by a.id, a.lastname, b.name") List getAuthorsWithBooks(); } diff --git a/src/main/java/ru/ip/labworks/labworks/bookshop/service/AuthorService.java b/src/main/java/ru/ip/labworks/labworks/bookshop/service/AuthorService.java index 9e0b4bb..320115b 100644 --- a/src/main/java/ru/ip/labworks/labworks/bookshop/service/AuthorService.java +++ b/src/main/java/ru/ip/labworks/labworks/bookshop/service/AuthorService.java @@ -23,13 +23,13 @@ public class AuthorService { @Autowired private final AuthorRepository authorRepository; @Autowired - private final BookRepository bookRepository; + private final BookService bookService; @Autowired private final ValidatorUtil validatorUtil; - public AuthorService(AuthorRepository authorRepository, BookRepository bookRepository, ValidatorUtil validatorUtil){ + public AuthorService(AuthorRepository authorRepository, BookService bookService, ValidatorUtil validatorUtil){ this.authorRepository = authorRepository; - this.bookRepository = bookRepository; + this.bookService = bookService; this.validatorUtil = validatorUtil; } @@ -107,8 +107,8 @@ public class AuthorService { @Transactional public void addBookToAuthor(Long id, Long bookId){ Optional author = authorRepository.findById(id); - if (author.isPresent() && !author.get().getBooks().contains(bookRepository.findById(bookId).get())){ - author.get().addBook(bookRepository.findById(bookId).get()); + if (author.isPresent() && !author.get().getBooks().contains(bookService.findBook(bookId))){ + author.get().addBook(bookService.findBook(bookId)); } authorRepository.save(author.get()); } @@ -116,8 +116,8 @@ public class AuthorService { @Transactional public void removeBookFromAuthor(Long id, Long bookId){ Optional author = authorRepository.findById(id); - if(author.isPresent() && author.get().getBooks().contains(bookRepository.findById(bookId).get())){ - author.get().removeBook(bookRepository.findById(bookId).get()); + if(author.isPresent() && author.get().getBooks().contains(bookService.findBook(bookId))){ + author.get().removeBook(bookService.findBook(bookId)); } authorRepository.save(author.get()); } diff --git a/src/main/java/ru/ip/labworks/labworks/bookshop/service/BookService.java b/src/main/java/ru/ip/labworks/labworks/bookshop/service/BookService.java index d95b607..fa581f0 100644 --- a/src/main/java/ru/ip/labworks/labworks/bookshop/service/BookService.java +++ b/src/main/java/ru/ip/labworks/labworks/bookshop/service/BookService.java @@ -24,12 +24,12 @@ public class BookService { @Autowired private final ValidatorUtil validatorUtil; @Autowired - private final GenreRepository genreRepository; + private final GenreService genreService; - public BookService(BookRepository bookRepository, ValidatorUtil validatorUtil, GenreRepository genreRepository){ + public BookService(BookRepository bookRepository, ValidatorUtil validatorUtil, GenreService genreService){ this.bookRepository = bookRepository; this.validatorUtil = validatorUtil; - this.genreRepository = genreRepository; + this.genreService = genreService; } private Date ParseToDate(String s){ @@ -112,8 +112,8 @@ public class BookService { @Transactional public void addGenreToBook(Long id, Long genreId){ Optional book = bookRepository.findById(id); - if (book.isPresent() && !book.get().getGenres().contains(genreRepository.findById(genreId).get())){ - book.get().addGenre(genreRepository.findById(genreId).get()); + if (book.isPresent() && !book.get().getGenres().contains(genreService.findGenre(genreId))){ + book.get().addGenre(genreService.findGenre(genreId)); } bookRepository.save(book.get()); } @@ -121,8 +121,8 @@ public class BookService { @Transactional public void removeGenreFromBook(Long id, Long genreId){ Optional book = bookRepository.findById(id); - if(book.isPresent() && book.get().getGenres().contains(genreRepository.findById(genreId).get())){ - book.get().removeGenre(genreRepository.findById(genreId).get()); + if(book.isPresent() && book.get().getGenres().contains(genreService.findGenre(genreId))){ + book.get().removeGenre(genreService.findGenre(genreId)); } bookRepository.save(book.get()); } diff --git a/src/main/resources/frontend/spa-vue/src/pages/Authors.vue b/src/main/resources/frontend/spa-vue/src/pages/Authors.vue index 502d54e..0612495 100644 --- a/src/main/resources/frontend/spa-vue/src/pages/Authors.vue +++ b/src/main/resources/frontend/spa-vue/src/pages/Authors.vue @@ -22,6 +22,7 @@ export default{ authors: [], authorBooks: [], allBooks: [], + mapAuthorsBooks: new Object(), bookid: 0, URL: "http://localhost:8080/", author: new Author(), @@ -42,9 +43,11 @@ export default{ await this.toBase64(); console.log(this.author); axios.post(this.URL + "author", this.author) - .then(() => { + .then((response) => { this.getAuthors(); this.closeModal(); + this.author = response.data; + this.openManyToManyModal(); }) .catch(error => { console.log(error); @@ -72,13 +75,19 @@ export default{ this.getAllBooks(); document.getElementById("manyToManyModal").style.display = "block"; }, + openAuthorsBooksModal(){ + this.getAuthorsBooks(); + document.getElementById("authorsBooksModal").style.display = "block"; + }, closeModal() { document.getElementById("editModal").style.display = "none"; - this.author = new Object(); }, closeManyToManyModal() { document.getElementById("manyToManyModal").style.display = "none"; }, + closeAuthorsBooksModal(){ + document.getElementById("authorsBooksModal").style.display = "none"; + }, async toBase64(){ var file = document.getElementById("photo").files[0]; var reader = new FileReader(); @@ -117,6 +126,7 @@ export default{ }); }, addBook(){ + console.log(this.bookid + " " + this.author.id); axios.post(this.URL + `author/${this.author.id}/Book/${this.bookid}`) .then(() => { this.getAuthorBooks(); @@ -130,6 +140,17 @@ export default{ .then(() =>{ this.getAuthorBooks(); }) + }, + getAuthorsBooks(){ + axios.get(this.URL + "author/books") + .then(response => { + console.log(response.data); + this.mapAuthorsBooks = response.data; + console.log(this.mapAuthorsBooks); + }) + .catch(error => { + console.log(error); + }); } } } @@ -141,6 +162,7 @@ export default{

Author Table:

+ @@ -236,6 +258,40 @@ export default{ +
+ + + + + + + + + + + + +
Фамилия автора:Название книг:
{{ key }} +
    +
  • {{ book }}
  • +
+
+ +
+ + + + diff --git a/src/main/resources/frontend/spa-vue/src/pages/Books.vue b/src/main/resources/frontend/spa-vue/src/pages/Books.vue index 3d2ab5c..8ab1fcb 100644 --- a/src/main/resources/frontend/spa-vue/src/pages/Books.vue +++ b/src/main/resources/frontend/spa-vue/src/pages/Books.vue @@ -42,9 +42,11 @@ export default{ await this.toBase64(); console.log(this.book); axios.post(this.URL + "book", this.book) - .then(() => { + .then((response) => { this.getBooks(); this.closeModal(); + this.book = response.data; + this.openManyToManyModal(); }) .catch(error => { console.log(error);