This commit is contained in:
maxnes3 2023-04-18 11:31:48 +04:00
parent 29b448f959
commit f7ad06d262
6 changed files with 1256 additions and 14 deletions

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -4,14 +4,16 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import ru.ip.labworks.labworks.bookshop.model.*;
import org.hibernate.query.Query;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import javax.persistence.Tuple;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
public class AuthorService {
@ -87,12 +89,17 @@ public class AuthorService {
}
@Transactional
public Map<Author, List<Book>> AllAuthorsAndBooks(){
Map<Author, List<Book>> result = new HashMap<>();
List<Author> tempList = findAllAuthors();
for (Author author : tempList){
result.put(author, author.getBooks());
}
return result;
public Map<String, List<String>> AllAuthorsAndBooks(){
List<Object[]> temp = entityManager.createQuery("select a.lastname as author, b.name as book " +
"from Author a " +
"join a.books b " +
"group by a.id").getResultList();
return temp.stream()
.collect(
Collectors.groupingBy(
o -> (String) o[0],
Collectors.mapping( o -> (String) o[1], Collectors.toList() )
)
);
}
}

View File

@ -8,13 +8,12 @@ import ru.ip.labworks.labworks.bookshop.model.Author;
import ru.ip.labworks.labworks.bookshop.model.Book;
import ru.ip.labworks.labworks.bookshop.service.AuthorService;
import ru.ip.labworks.labworks.bookshop.service.BookService;
import javax.imageio.ImageIO;
import javax.persistence.EntityNotFoundException;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SpringBootTest
public class JpaAuthorTests {
@ -107,6 +106,11 @@ public class JpaAuthorTests {
Author author = authorService.addAuthor("Test", "Test", new File("D:\\Учёба\\Интернет программирование\\PIbd-22_Bondarenko_M.S._IP\\src\\main\\resources\\SK.jpg"));
final Book book = bookService.addBook("Test2", "03.04.2023", new File("D:\\Учёба\\Интернет программирование\\PIbd-22_Bondarenko_M.S._IP\\src\\main\\resources\\glow.png"));
authorService.addBookToAuthor(author.getId(), book);
Map<String, List<String>> result = new HashMap<>();
List<String> temp = new ArrayList<>();
temp.add(book.getName());
result.put(author.getLastName(), temp);
Assertions.assertEquals(authorService.AllAuthorsAndBooks(), result);
Assertions.assertNotNull(authorService.AllAuthorsAndBooks());
}
}

View File

@ -89,7 +89,7 @@ public class JpaBookTests {
}
@Test
void TestRemoveBookFromAuthor(){
void TestRemoveGenreFromBook(){
bookService.deleteAllBooks();
genreService.deleteAllGenres();
Book book = bookService.addBook("Test", "03.04.2023", new File("D:\\Учёба\\Интернет программирование\\PIbd-22_Bondarenko_M.S._IP\\src\\main\\resources\\glow.png"));

View File

@ -4,7 +4,6 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import ru.ip.labworks.labworks.bookshop.model.Book;
import ru.ip.labworks.labworks.bookshop.model.Genre;
import ru.ip.labworks.labworks.bookshop.service.GenreService;