Compare commits

...

3 Commits

Author SHA1 Message Date
maxnes3
d14bf5334f Всё 2023-05-15 19:10:49 +04:00
maxnes3
0e51a0e3d6 Merge branch 'LabWork_04' into LabWork_05 2023-05-15 19:08:30 +04:00
maxnes3
0ba3312dae Сдал 2023-05-15 19:06:57 +04:00
7 changed files with 84 additions and 18 deletions

View File

@ -6,6 +6,7 @@ import ru.ip.labworks.labworks.configuration.WebConfiguration;
import javax.validation.Valid; import javax.validation.Valid;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Map;
@RestController @RestController
@RequestMapping(WebConfiguration.REST_API + "/author") @RequestMapping(WebConfiguration.REST_API + "/author")
@ -56,4 +57,9 @@ public class AuthorController {
public void deleteAuthor(@PathVariable Long id){ public void deleteAuthor(@PathVariable Long id){
authorService.deleteAuthor(id); authorService.deleteAuthor(id);
} }
@GetMapping("/books")
public Map<String, List<String>> getAuthorsBooks(){
return authorService.AllAuthorsAndBooks();
}
} }

View File

@ -94,4 +94,6 @@ public class AuthorMvcController {
authorService.removeBookFromAuthor(id, bookid); authorService.removeBookFromAuthor(id, bookid);
return "redirect:/author/" + id.toString() + "/books"; return "redirect:/author/" + id.toString() + "/books";
} }
private void Bt(){}
} }

View File

@ -10,6 +10,6 @@ public interface AuthorRepository extends JpaRepository<Author, Long> {
@Query("select a.lastname as author, b.name as book " + @Query("select a.lastname as author, b.name as book " +
"from Author a " + "from Author a " +
"join a.books b " + "join a.books b " +
"group by a.id") "group by a.id, a.lastname, b.name")
List<Object[]> getAuthorsWithBooks(); List<Object[]> getAuthorsWithBooks();
} }

View File

@ -23,13 +23,13 @@ public class AuthorService {
@Autowired @Autowired
private final AuthorRepository authorRepository; private final AuthorRepository authorRepository;
@Autowired @Autowired
private final BookRepository bookRepository; private final BookService bookService;
@Autowired @Autowired
private final ValidatorUtil validatorUtil; private final ValidatorUtil validatorUtil;
public AuthorService(AuthorRepository authorRepository, BookRepository bookRepository, ValidatorUtil validatorUtil){ public AuthorService(AuthorRepository authorRepository, BookService bookService, ValidatorUtil validatorUtil){
this.authorRepository = authorRepository; this.authorRepository = authorRepository;
this.bookRepository = bookRepository; this.bookService = bookService;
this.validatorUtil = validatorUtil; this.validatorUtil = validatorUtil;
} }
@ -107,8 +107,8 @@ public class AuthorService {
@Transactional @Transactional
public void addBookToAuthor(Long id, Long bookId){ public void addBookToAuthor(Long id, Long bookId){
Optional<Author> author = authorRepository.findById(id); Optional<Author> author = authorRepository.findById(id);
if (author.isPresent() && !author.get().getBooks().contains(bookRepository.findById(bookId).get())){ if (author.isPresent() && !author.get().getBooks().contains(bookService.findBook(bookId))){
author.get().addBook(bookRepository.findById(bookId).get()); author.get().addBook(bookService.findBook(bookId));
} }
authorRepository.save(author.get()); authorRepository.save(author.get());
} }
@ -116,8 +116,8 @@ public class AuthorService {
@Transactional @Transactional
public void removeBookFromAuthor(Long id, Long bookId){ public void removeBookFromAuthor(Long id, Long bookId){
Optional<Author> author = authorRepository.findById(id); Optional<Author> author = authorRepository.findById(id);
if(author.isPresent() && author.get().getBooks().contains(bookRepository.findById(bookId).get())){ if(author.isPresent() && author.get().getBooks().contains(bookService.findBook(bookId))){
author.get().removeBook(bookRepository.findById(bookId).get()); author.get().removeBook(bookService.findBook(bookId));
} }
authorRepository.save(author.get()); authorRepository.save(author.get());
} }

View File

@ -24,12 +24,12 @@ public class BookService {
@Autowired @Autowired
private final ValidatorUtil validatorUtil; private final ValidatorUtil validatorUtil;
@Autowired @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.bookRepository = bookRepository;
this.validatorUtil = validatorUtil; this.validatorUtil = validatorUtil;
this.genreRepository = genreRepository; this.genreService = genreService;
} }
private Date ParseToDate(String s){ private Date ParseToDate(String s){
@ -112,8 +112,8 @@ public class BookService {
@Transactional @Transactional
public void addGenreToBook(Long id, Long genreId){ public void addGenreToBook(Long id, Long genreId){
Optional<Book> book = bookRepository.findById(id); Optional<Book> book = bookRepository.findById(id);
if (book.isPresent() && !book.get().getGenres().contains(genreRepository.findById(genreId).get())){ if (book.isPresent() && !book.get().getGenres().contains(genreService.findGenre(genreId))){
book.get().addGenre(genreRepository.findById(genreId).get()); book.get().addGenre(genreService.findGenre(genreId));
} }
bookRepository.save(book.get()); bookRepository.save(book.get());
} }
@ -121,8 +121,8 @@ public class BookService {
@Transactional @Transactional
public void removeGenreFromBook(Long id, Long genreId){ public void removeGenreFromBook(Long id, Long genreId){
Optional<Book> book = bookRepository.findById(id); Optional<Book> book = bookRepository.findById(id);
if(book.isPresent() && book.get().getGenres().contains(genreRepository.findById(genreId).get())){ if(book.isPresent() && book.get().getGenres().contains(genreService.findGenre(genreId))){
book.get().removeGenre(genreRepository.findById(genreId).get()); book.get().removeGenre(genreService.findGenre(genreId));
} }
bookRepository.save(book.get()); bookRepository.save(book.get());
} }

View File

@ -22,6 +22,7 @@ export default{
authors: [], authors: [],
authorBooks: [], authorBooks: [],
allBooks: [], allBooks: [],
mapAuthorsBooks: new Object(),
bookid: 0, bookid: 0,
URL: "http://localhost:8080/", URL: "http://localhost:8080/",
author: new Author(), author: new Author(),
@ -42,9 +43,11 @@ export default{
await this.toBase64(); await this.toBase64();
console.log(this.author); console.log(this.author);
axios.post(this.URL + "author", this.author) axios.post(this.URL + "author", this.author)
.then(() => { .then((response) => {
this.getAuthors(); this.getAuthors();
this.closeModal(); this.closeModal();
this.author = response.data;
this.openManyToManyModal();
}) })
.catch(error => { .catch(error => {
console.log(error); console.log(error);
@ -72,13 +75,19 @@ export default{
this.getAllBooks(); this.getAllBooks();
document.getElementById("manyToManyModal").style.display = "block"; document.getElementById("manyToManyModal").style.display = "block";
}, },
openAuthorsBooksModal(){
this.getAuthorsBooks();
document.getElementById("authorsBooksModal").style.display = "block";
},
closeModal() { closeModal() {
document.getElementById("editModal").style.display = "none"; document.getElementById("editModal").style.display = "none";
this.author = new Object();
}, },
closeManyToManyModal() { closeManyToManyModal() {
document.getElementById("manyToManyModal").style.display = "none"; document.getElementById("manyToManyModal").style.display = "none";
}, },
closeAuthorsBooksModal(){
document.getElementById("authorsBooksModal").style.display = "none";
},
async toBase64(){ async toBase64(){
var file = document.getElementById("photo").files[0]; var file = document.getElementById("photo").files[0];
var reader = new FileReader(); var reader = new FileReader();
@ -117,6 +126,7 @@ export default{
}); });
}, },
addBook(){ addBook(){
console.log(this.bookid + " " + this.author.id);
axios.post(this.URL + `author/${this.author.id}/Book/${this.bookid}`) axios.post(this.URL + `author/${this.author.id}/Book/${this.bookid}`)
.then(() => { .then(() => {
this.getAuthorBooks(); this.getAuthorBooks();
@ -130,6 +140,17 @@ export default{
.then(() =>{ .then(() =>{
this.getAuthorBooks(); 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{
<div class="container mt-4"> <div class="container mt-4">
<h1 class="text-center mb-4">Author Table:</h1> <h1 class="text-center mb-4">Author Table:</h1>
<button class="btn btn-success mr-2" @click="openModal(); author = new Object(); author.status = `create`">Добавить</button> <button class="btn btn-success mr-2" @click="openModal(); author = new Object(); author.status = `create`">Добавить</button>
<button class="btn btn-primary mr-2" @click="openAuthorsBooksModal();">Посмотреть весь список</button>
<table class="table table-striped"> <table class="table table-striped">
<thead> <thead>
<tr> <tr>
@ -236,6 +258,40 @@ export default{
</div> </div>
</div> </div>
</div> </div>
<div class="modal" tabindex="-1" id="authorsBooksModal">
<div class="modal-dialog modal-xl">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">All Books of Authors</h5>
</div>
<div class="modal-body">
<form>
<table class="table table-striped">
<thead>
<tr>
<th>Фамилия автора:</th>
<th>Название книг:</th>
</tr>
</thead>
<tbody>
<tr v-for="[key, value] in Object.entries(mapAuthorsBooks)" :key="key">
<td>{{ key }}</td>
<td>
<ul>
<li v-for="book in value" :key="book">{{ book }}</li>
</ul>
</td>
</tr>
</tbody>
</table>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="editModal" @click="closeAuthorsBooksModal()">Закрыть</button>
</div>
</div>
</div>
</div>
<Footer></Footer> <Footer></Footer>
</template> </template>

View File

@ -42,9 +42,11 @@ export default{
await this.toBase64(); await this.toBase64();
console.log(this.book); console.log(this.book);
axios.post(this.URL + "book", this.book) axios.post(this.URL + "book", this.book)
.then(() => { .then((response) => {
this.getBooks(); this.getBooks();
this.closeModal(); this.closeModal();
this.book = response.data;
this.openManyToManyModal();
}) })
.catch(error => { .catch(error => {
console.log(error); console.log(error);