Сдал
This commit is contained in:
parent
29b448f959
commit
f7ad06d262
BIN
data.mv.db
BIN
data.mv.db
Binary file not shown.
1232
data.trace.db
1232
data.trace.db
File diff suppressed because it is too large
Load Diff
@ -4,14 +4,16 @@ import org.springframework.stereotype.Service;
|
|||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import ru.ip.labworks.labworks.bookshop.model.*;
|
import ru.ip.labworks.labworks.bookshop.model.*;
|
||||||
|
import org.hibernate.query.Query;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
import javax.persistence.EntityNotFoundException;
|
import javax.persistence.EntityNotFoundException;
|
||||||
import javax.persistence.PersistenceContext;
|
import javax.persistence.PersistenceContext;
|
||||||
|
import javax.persistence.Tuple;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class AuthorService {
|
public class AuthorService {
|
||||||
@ -87,12 +89,17 @@ public class AuthorService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Map<Author, List<Book>> AllAuthorsAndBooks(){
|
public Map<String, List<String>> AllAuthorsAndBooks(){
|
||||||
Map<Author, List<Book>> result = new HashMap<>();
|
List<Object[]> temp = entityManager.createQuery("select a.lastname as author, b.name as book " +
|
||||||
List<Author> tempList = findAllAuthors();
|
"from Author a " +
|
||||||
for (Author author : tempList){
|
"join a.books b " +
|
||||||
result.put(author, author.getBooks());
|
"group by a.id").getResultList();
|
||||||
}
|
return temp.stream()
|
||||||
return result;
|
.collect(
|
||||||
|
Collectors.groupingBy(
|
||||||
|
o -> (String) o[0],
|
||||||
|
Collectors.mapping( o -> (String) o[1], Collectors.toList() )
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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.model.Book;
|
||||||
import ru.ip.labworks.labworks.bookshop.service.AuthorService;
|
import ru.ip.labworks.labworks.bookshop.service.AuthorService;
|
||||||
import ru.ip.labworks.labworks.bookshop.service.BookService;
|
import ru.ip.labworks.labworks.bookshop.service.BookService;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
|
||||||
import javax.persistence.EntityNotFoundException;
|
import javax.persistence.EntityNotFoundException;
|
||||||
import java.awt.image.BufferedImage;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
public class JpaAuthorTests {
|
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"));
|
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"));
|
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);
|
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());
|
Assertions.assertNotNull(authorService.AllAuthorsAndBooks());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -89,7 +89,7 @@ public class JpaBookTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void TestRemoveBookFromAuthor(){
|
void TestRemoveGenreFromBook(){
|
||||||
bookService.deleteAllBooks();
|
bookService.deleteAllBooks();
|
||||||
genreService.deleteAllGenres();
|
genreService.deleteAllGenres();
|
||||||
Book book = bookService.addBook("Test", "03.04.2023", new File("D:\\Учёба\\Интернет программирование\\PIbd-22_Bondarenko_M.S._IP\\src\\main\\resources\\glow.png"));
|
Book book = bookService.addBook("Test", "03.04.2023", new File("D:\\Учёба\\Интернет программирование\\PIbd-22_Bondarenko_M.S._IP\\src\\main\\resources\\glow.png"));
|
||||||
|
@ -4,7 +4,6 @@ import org.junit.jupiter.api.Assertions;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
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.model.Genre;
|
||||||
import ru.ip.labworks.labworks.bookshop.service.GenreService;
|
import ru.ip.labworks.labworks.bookshop.service.GenreService;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user