Доп задания сделаны
This commit is contained in:
parent
10ba7def55
commit
29b448f959
@ -15,6 +15,9 @@ repositories {
|
|||||||
dependencies {
|
dependencies {
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||||
developmentOnly 'org.springframework.boot:spring-boot-devtools'
|
developmentOnly 'org.springframework.boot:spring-boot-devtools'
|
||||||
|
implementation 'com.h2database:h2:2.1.210'
|
||||||
|
implementation 'org.hibernate.validator:hibernate-validator:7.0.1.Final'
|
||||||
|
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5'
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||||
}
|
}
|
||||||
|
BIN
data.mv.db
Normal file
BIN
data.mv.db
Normal file
Binary file not shown.
58
data.trace.db
Normal file
58
data.trace.db
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
2023-04-02 22:35:27 jdbc[13]: exception
|
||||||
|
org.h2.jdbc.JdbcSQLSyntaxErrorException: Неверное количество столбцов
|
||||||
|
Column count does not match; SQL statement:
|
||||||
|
INSERT INTO genre VALUES('Триллер') [21002-210]
|
||||||
|
at org.h2.message.DbException.getJdbcSQLException(DbException.java:521)
|
||||||
|
at org.h2.message.DbException.getJdbcSQLException(DbException.java:496)
|
||||||
|
at org.h2.message.DbException.get(DbException.java:227)
|
||||||
|
at org.h2.message.DbException.get(DbException.java:203)
|
||||||
|
at org.h2.message.DbException.get(DbException.java:192)
|
||||||
|
at org.h2.command.dml.Insert.prepare(Insert.java:295)
|
||||||
|
at org.h2.command.Parser.prepareCommand(Parser.java:557)
|
||||||
|
at org.h2.engine.SessionLocal.prepareLocal(SessionLocal.java:615)
|
||||||
|
at org.h2.engine.SessionLocal.prepareCommand(SessionLocal.java:553)
|
||||||
|
at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1116)
|
||||||
|
at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:237)
|
||||||
|
at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:223)
|
||||||
|
at org.h2.server.web.WebApp.getResult(WebApp.java:1339)
|
||||||
|
at org.h2.server.web.WebApp.query(WebApp.java:1137)
|
||||||
|
at org.h2.server.web.WebApp.query(WebApp.java:1113)
|
||||||
|
at org.h2.server.web.WebApp.process(WebApp.java:244)
|
||||||
|
at org.h2.server.web.WebApp.processRequest(WebApp.java:176)
|
||||||
|
at org.h2.server.web.WebServlet.doGet(WebServlet.java:129)
|
||||||
|
at org.h2.server.web.WebServlet.doPost(WebServlet.java:166)
|
||||||
|
at javax.servlet.http.HttpServlet.service(HttpServlet.java:696)
|
||||||
|
at javax.servlet.http.HttpServlet.service(HttpServlet.java:779)
|
||||||
|
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
|
||||||
|
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
|
||||||
|
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
|
||||||
|
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
|
||||||
|
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
|
||||||
|
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
|
||||||
|
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
|
||||||
|
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
|
||||||
|
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
|
||||||
|
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
|
||||||
|
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
|
||||||
|
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
|
||||||
|
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
|
||||||
|
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
|
||||||
|
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
|
||||||
|
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
|
||||||
|
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
|
||||||
|
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:177)
|
||||||
|
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
|
||||||
|
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
|
||||||
|
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
|
||||||
|
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
|
||||||
|
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
|
||||||
|
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360)
|
||||||
|
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)
|
||||||
|
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
|
||||||
|
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:891)
|
||||||
|
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1784)
|
||||||
|
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
|
||||||
|
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
|
||||||
|
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
|
||||||
|
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
|
||||||
|
at java.base/java.lang.Thread.run(Thread.java:1589)
|
@ -2,8 +2,10 @@ package ru.ip.labworks.labworks;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
@RestController
|
||||||
public class LabworksApplication {
|
public class LabworksApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -0,0 +1,4 @@
|
|||||||
|
package ru.ip.labworks.labworks.bookshop.controller;
|
||||||
|
|
||||||
|
public class AuthorController {
|
||||||
|
}
|
@ -0,0 +1,70 @@
|
|||||||
|
package ru.ip.labworks.labworks.bookshop.model;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Author {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
private String firstname;
|
||||||
|
private String lastname;
|
||||||
|
@Lob
|
||||||
|
private byte[] photo;
|
||||||
|
@ManyToMany(fetch = FetchType.EAGER)
|
||||||
|
@JoinTable(name = "authors_books",
|
||||||
|
joinColumns = @JoinColumn(name = "author_fk"),
|
||||||
|
inverseJoinColumns = @JoinColumn(name = "book_fk"))
|
||||||
|
private List<Book> books;
|
||||||
|
|
||||||
|
public Author(){}
|
||||||
|
public Author(String firstname, String lastname, byte[] photo){
|
||||||
|
this.firstname = firstname;
|
||||||
|
this.lastname = lastname;
|
||||||
|
this.photo = photo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId(){return id;}
|
||||||
|
|
||||||
|
public String getFirstnameName(){return firstname;}
|
||||||
|
public void setFirstnameName(String firstname){this.firstname = firstname;}
|
||||||
|
|
||||||
|
public String getLastName(){return lastname;}
|
||||||
|
public void setLastName(String lastname){this.lastname = lastname;}
|
||||||
|
|
||||||
|
public byte[] getPhoto(){return photo;}
|
||||||
|
public void setPhoto(byte[] photo){this.photo = photo;}
|
||||||
|
|
||||||
|
public List<Book> getBooks(){return books;}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
Author author = (Author) o;
|
||||||
|
return Objects.equals(id, author.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Author{" +
|
||||||
|
"id=" + id +
|
||||||
|
", firstname='" + firstname + '\'' +
|
||||||
|
", lastname='" + lastname + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addBook(Book book) {
|
||||||
|
books.add(book);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeBook(Book book) {
|
||||||
|
books.remove(book);
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,8 @@
|
|||||||
package ru.ip.labworks.labworks.bookshop.model;
|
package ru.ip.labworks.labworks.bookshop.model;
|
||||||
|
|
||||||
|
import org.hibernate.annotations.LazyCollection;
|
||||||
|
import org.hibernate.annotations.LazyCollectionOption;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@ -10,16 +13,23 @@ public class Book {
|
|||||||
private Long id;
|
private Long id;
|
||||||
private String name;
|
private String name;
|
||||||
private Date release;
|
private Date release;
|
||||||
|
@Lob
|
||||||
|
private byte[] cover;
|
||||||
@ManyToMany
|
@ManyToMany
|
||||||
|
@LazyCollection(LazyCollectionOption.FALSE)
|
||||||
@JoinTable(name = "books_genres",
|
@JoinTable(name = "books_genres",
|
||||||
joinColumns = @JoinColumn(name = "book_fk"),
|
joinColumns = @JoinColumn(name = "book_fk"),
|
||||||
inverseJoinColumns = @JoinColumn(name = "genre_fk"))
|
inverseJoinColumns = @JoinColumn(name = "genre_fk"))
|
||||||
private List<Genre> genres;
|
private List<Genre> genres;
|
||||||
|
|
||||||
|
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "books")
|
||||||
|
private List<Author> authors;
|
||||||
|
|
||||||
public Book(){}
|
public Book(){}
|
||||||
public Book(String name,Date release){
|
public Book(String name,Date release, byte[] cover){
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.release = release;
|
this.release = release;
|
||||||
|
this.cover = cover;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long getId(){return id;}
|
public Long getId(){return id;}
|
||||||
@ -30,6 +40,11 @@ public class Book {
|
|||||||
public Date getRelease(){return release;}
|
public Date getRelease(){return release;}
|
||||||
public void setRelease(Date release){this.release = release;}
|
public void setRelease(Date release){this.release = release;}
|
||||||
|
|
||||||
|
public byte[] getCover(){return cover;}
|
||||||
|
public void setCover(byte[] cover){this.cover = cover;}
|
||||||
|
|
||||||
|
public List<Genre> getGenres(){return genres;}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
@ -51,4 +66,12 @@ public class Book {
|
|||||||
", release='" + release + '\'' +
|
", release='" + release + '\'' +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addGenre(Genre genre) {
|
||||||
|
genres.add(genre);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeGenre(Genre genre) {
|
||||||
|
genres.remove(genre);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,8 +9,6 @@ public class Genre {
|
|||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
private Long id;
|
private Long id;
|
||||||
private String name;
|
private String name;
|
||||||
@ManyToMany(mappedBy = "genres")
|
|
||||||
private List<Book> books;
|
|
||||||
|
|
||||||
public Genre(){}
|
public Genre(){}
|
||||||
public Genre(String name){
|
public Genre(String name){
|
||||||
|
@ -0,0 +1,98 @@
|
|||||||
|
package ru.ip.labworks.labworks.bookshop.service;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
import ru.ip.labworks.labworks.bookshop.model.*;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.EntityNotFoundException;
|
||||||
|
import javax.persistence.PersistenceContext;
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class AuthorService {
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager entityManager;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Author addAuthor(String firstname, String lastname, File photo){
|
||||||
|
if (!StringUtils.hasText(firstname) || !StringUtils.hasText(lastname)) {
|
||||||
|
throw new IllegalArgumentException("Author firstname and lastname is null or empty");
|
||||||
|
}
|
||||||
|
final Author author = new Author(firstname, lastname, ImageHelper.ImageToByte(photo));
|
||||||
|
entityManager.persist(author);
|
||||||
|
return author;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public Author findAuthor(Long id) {
|
||||||
|
final Author author = entityManager.find(Author.class, id);
|
||||||
|
if (author == null) {
|
||||||
|
throw new EntityNotFoundException(String.format("Author with id [%s] is not found", id));
|
||||||
|
}
|
||||||
|
return author;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<Author> findAllAuthors() {
|
||||||
|
return entityManager.createQuery("select s from Author s", Author.class).getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Author updateAuthor(Long id, String firstname, String lastname, File photo){
|
||||||
|
if (!StringUtils.hasText(firstname) || !StringUtils.hasText(lastname)) {
|
||||||
|
throw new IllegalArgumentException("Author firstname and lastname is null or empty");
|
||||||
|
}
|
||||||
|
final Author currentAuthor = findAuthor(id);
|
||||||
|
currentAuthor.setFirstnameName(firstname);
|
||||||
|
currentAuthor.setLastName(lastname);
|
||||||
|
currentAuthor.setPhoto(ImageHelper.ImageToByte(photo));
|
||||||
|
return entityManager.merge(currentAuthor);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Author deleteAuthor(Long id) {
|
||||||
|
final Author currentAuthor = findAuthor(id);
|
||||||
|
entityManager.remove(currentAuthor);
|
||||||
|
return currentAuthor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteAllAuthors() {
|
||||||
|
entityManager.createQuery("delete from Author").executeUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void addBookToAuthor(Long id, Book book){
|
||||||
|
final Author author = findAuthor(id);
|
||||||
|
if(author == null){
|
||||||
|
throw new IllegalArgumentException("Author with id " + id + " not found");
|
||||||
|
}
|
||||||
|
author.addBook(entityManager.find(book.getClass(), book.getId()));
|
||||||
|
entityManager.merge(author);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void removeBookFromAuthor(Long id, Book book){
|
||||||
|
final Author author = findAuthor(id);
|
||||||
|
if(author == null){
|
||||||
|
throw new IllegalArgumentException("Author with id " + id + " not found");
|
||||||
|
}
|
||||||
|
author.removeBook(entityManager.find(book.getClass(), book.getId()));
|
||||||
|
entityManager.merge(author);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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;
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,101 @@
|
|||||||
package ru.ip.labworks.labworks.bookshop.service;
|
package ru.ip.labworks.labworks.bookshop.service;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
import ru.ip.labworks.labworks.bookshop.model.Book;
|
||||||
|
import ru.ip.labworks.labworks.bookshop.model.Genre;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.EntityNotFoundException;
|
||||||
|
import javax.persistence.PersistenceContext;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
@Service
|
||||||
public class BookService {
|
public class BookService {
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager entityManager;
|
||||||
|
|
||||||
|
private Date ParseToDate(String s){
|
||||||
|
Date release;
|
||||||
|
try {
|
||||||
|
release = new SimpleDateFormat("dd.MM.yyyy").parse(s);
|
||||||
|
}
|
||||||
|
catch (ParseException ex){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return release;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Book addBook(String name, String release, File cover){
|
||||||
|
if (!StringUtils.hasText(name) || !StringUtils.hasText(release)) {
|
||||||
|
throw new IllegalArgumentException("Book name and release is null or empty");
|
||||||
|
}
|
||||||
|
final Book book = new Book(name, ParseToDate(release), ImageHelper.ImageToByte(cover));
|
||||||
|
entityManager.persist(book);
|
||||||
|
return book;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public Book findBook(Long id) {
|
||||||
|
final Book book = entityManager.find(Book.class, id);
|
||||||
|
if (book == null) {
|
||||||
|
throw new EntityNotFoundException(String.format("Book with id [%s] is not found", id));
|
||||||
|
}
|
||||||
|
return book;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<Book> findAllBooks() {
|
||||||
|
return entityManager.createQuery("select s from Book s", Book.class).getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Book updateBook(Long id, String name, String release, File cover) {
|
||||||
|
if (!StringUtils.hasText(name) || !StringUtils.hasText(release)) {
|
||||||
|
throw new IllegalArgumentException("Book name and release is null or empty");
|
||||||
|
}
|
||||||
|
final Book currentBook = findBook(id);
|
||||||
|
currentBook.setName(name);
|
||||||
|
currentBook.setRelease(ParseToDate(release));
|
||||||
|
currentBook.setCover(ImageHelper.ImageToByte(cover));
|
||||||
|
return entityManager.merge(currentBook);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Book deleteBook(Long id) {
|
||||||
|
final Book currentBook = findBook(id);
|
||||||
|
entityManager.remove(currentBook);
|
||||||
|
return currentBook;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteAllBooks() {
|
||||||
|
entityManager.createQuery("delete from Book").executeUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void addGenreToBook(Long id, Genre genre){
|
||||||
|
final Book book = findBook(id);
|
||||||
|
if(book == null){
|
||||||
|
throw new IllegalArgumentException("Book with id " + id + " not found");
|
||||||
|
}
|
||||||
|
book.addGenre(entityManager.find(genre.getClass(), genre.getId()));
|
||||||
|
entityManager.merge(book);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void removeGenreFromBook(Long id, Genre genre){
|
||||||
|
final Book book = findBook(id);
|
||||||
|
if(book == null){
|
||||||
|
throw new IllegalArgumentException("Book with id " + id + " not found");
|
||||||
|
}
|
||||||
|
book.removeGenre(entityManager.find(genre.getClass(), genre.getId()));
|
||||||
|
entityManager.merge(book);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,63 @@
|
|||||||
package ru.ip.labworks.labworks.bookshop.service;
|
package ru.ip.labworks.labworks.bookshop.service;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
import ru.ip.labworks.labworks.bookshop.model.Genre;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.EntityNotFoundException;
|
||||||
|
import javax.persistence.PersistenceContext;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
public class GenreService {
|
public class GenreService {
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager entityManager;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Genre addGenre(String name){
|
||||||
|
if (!StringUtils.hasText(name)) {
|
||||||
|
throw new IllegalArgumentException("Genre name is null or empty");
|
||||||
|
}
|
||||||
|
final Genre genre = new Genre(name);
|
||||||
|
entityManager.persist(genre);
|
||||||
|
return genre;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public Genre findGenre(Long id) {
|
||||||
|
final Genre genre = entityManager.find(Genre.class, id);
|
||||||
|
if (genre == null) {
|
||||||
|
throw new EntityNotFoundException(String.format("Genre with id [%s] is not found", id));
|
||||||
|
}
|
||||||
|
return genre;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<Genre> findAllGenres() {
|
||||||
|
return entityManager.createQuery("select s from Genre s", Genre.class).getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Genre updateGenre(Long id, String name) {
|
||||||
|
if (!StringUtils.hasText(name)) {
|
||||||
|
throw new IllegalArgumentException("Genre name is null or empty");
|
||||||
|
}
|
||||||
|
final Genre currentGenre = findGenre(id);
|
||||||
|
currentGenre.setName(name);
|
||||||
|
return entityManager.merge(currentGenre);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Genre deleteGenre(Long id) {
|
||||||
|
final Genre currentGenre = findGenre(id);
|
||||||
|
entityManager.remove(currentGenre);
|
||||||
|
return currentGenre;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteAllGenres() {
|
||||||
|
entityManager.createQuery("delete from Genre").executeUpdate();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,20 @@
|
|||||||
|
package ru.ip.labworks.labworks.bookshop.service;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public class ImageHelper {
|
||||||
|
public static byte[] ImageToByte(File image){
|
||||||
|
try {
|
||||||
|
BufferedImage bufferedImage = ImageIO.read(image);
|
||||||
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||||
|
ImageIO.write(bufferedImage, "jpg", byteArrayOutputStream);
|
||||||
|
return byteArrayOutputStream.toByteArray();
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1 +1,11 @@
|
|||||||
|
spring.main.banner-mode=off
|
||||||
|
#server.port=8080
|
||||||
|
spring.datasource.url=jdbc:h2:file:./data
|
||||||
|
spring.datasource.driverClassName=org.h2.Driver
|
||||||
|
spring.datasource.username=sa
|
||||||
|
spring.datasource.password=password
|
||||||
|
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
|
||||||
|
spring.jpa.hibernate.ddl-auto=update
|
||||||
|
spring.h2.console.enabled=true
|
||||||
|
spring.h2.console.settings.trace=false
|
||||||
|
spring.h2.console.settings.web-allow-others=false
|
||||||
|
112
src/test/java/ru/ip/labworks/labworks/JpaAuthorTests.java
Normal file
112
src/test/java/ru/ip/labworks/labworks/JpaAuthorTests.java
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
package ru.ip.labworks.labworks;
|
||||||
|
|
||||||
|
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.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.List;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
public class JpaAuthorTests {
|
||||||
|
@Autowired
|
||||||
|
AuthorService authorService;
|
||||||
|
@Autowired
|
||||||
|
BookService bookService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestAddAuthor(){
|
||||||
|
authorService.deleteAllAuthors();
|
||||||
|
final Author author = authorService.addAuthor("test", "test", new File("D:\\Учёба\\Интернет программирование\\PIbd-22_Bondarenko_M.S._IP\\src\\main\\resources\\SK.jpg"));
|
||||||
|
Assertions.assertNotNull(author.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestFindAuthor(){
|
||||||
|
authorService.deleteAllAuthors();
|
||||||
|
final Author author = authorService.addAuthor("test", "test", new File("D:\\Учёба\\Интернет программирование\\PIbd-22_Bondarenko_M.S._IP\\src\\main\\resources\\SK.jpg"));
|
||||||
|
final Author findAuthor = authorService.findAuthor(author.getId());
|
||||||
|
Assertions.assertEquals(author, findAuthor);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestAuthorReadNotFound(){
|
||||||
|
authorService.deleteAllAuthors();
|
||||||
|
Assertions.assertThrows(EntityNotFoundException.class, () -> authorService.findAuthor(-1L));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestFindAllAuthor(){
|
||||||
|
authorService.deleteAllAuthors();
|
||||||
|
final Author author1 = authorService.addAuthor("Test1","Test1", new File("D:\\Учёба\\Интернет программирование\\PIbd-22_Bondarenko_M.S._IP\\src\\main\\resources\\SK.jpg"));
|
||||||
|
final Author author2 = authorService.addAuthor("Test2","Test2", new File("D:\\Учёба\\Интернет программирование\\PIbd-22_Bondarenko_M.S._IP\\src\\main\\resources\\SK.jpg"));
|
||||||
|
final List<Author> authors = authorService.findAllAuthors();
|
||||||
|
Assertions.assertEquals(authors.size(), 2);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
void TestAuthorReadAllEmpty() {
|
||||||
|
authorService.deleteAllAuthors();
|
||||||
|
final List<Author> authors = authorService.findAllAuthors();
|
||||||
|
Assertions.assertEquals(authors.size(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestAuthorUpdate(){
|
||||||
|
authorService.deleteAllAuthors();
|
||||||
|
Author author = authorService.addAuthor("Test1","Test1", new File("D:\\Учёба\\Интернет программирование\\PIbd-22_Bondarenko_M.S._IP\\src\\main\\resources\\SK.jpg"));
|
||||||
|
author = authorService.updateAuthor(author.getId(), "Test2", "Test2", new File("D:\\Учёба\\Интернет программирование\\PIbd-22_Bondarenko_M.S._IP\\src\\main\\resources\\SK.jpg"));
|
||||||
|
Assertions.assertEquals(author.getFirstnameName(), "Test2");
|
||||||
|
Assertions.assertEquals(author.getLastName(), "Test2");
|
||||||
|
Assertions.assertNotNull(author.getPhoto());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestDeleteAuthor(){
|
||||||
|
authorService.deleteAllAuthors();
|
||||||
|
final Author author = authorService.addAuthor("Test","Test", new File("D:\\Учёба\\Интернет программирование\\PIbd-22_Bondarenko_M.S._IP\\src\\main\\resources\\SK.jpg"));
|
||||||
|
authorService.deleteAuthor(author.getId());
|
||||||
|
Assertions.assertThrows(EntityNotFoundException.class, () -> authorService.findAuthor(1L));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestAddBookToAuthor(){
|
||||||
|
authorService.deleteAllAuthors();
|
||||||
|
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);
|
||||||
|
author = authorService.findAuthor(author.getId());
|
||||||
|
Assertions.assertEquals(author.getBooks().size(), 1);
|
||||||
|
bookService.deleteAllBooks();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestRemoveBookFromAuthor(){
|
||||||
|
authorService.deleteAllAuthors();
|
||||||
|
bookService.deleteAllBooks();
|
||||||
|
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);
|
||||||
|
authorService.removeBookFromAuthor(author.getId(), book);
|
||||||
|
author = authorService.findAuthor(author.getId());
|
||||||
|
Assertions.assertEquals(author.getBooks().size(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestAllAB(){
|
||||||
|
authorService.deleteAllAuthors();
|
||||||
|
bookService.deleteAllBooks();
|
||||||
|
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);
|
||||||
|
Assertions.assertNotNull(authorService.AllAuthorsAndBooks());
|
||||||
|
}
|
||||||
|
}
|
102
src/test/java/ru/ip/labworks/labworks/JpaBookTests.java
Normal file
102
src/test/java/ru/ip/labworks/labworks/JpaBookTests.java
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
package ru.ip.labworks.labworks;
|
||||||
|
|
||||||
|
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.BookService;
|
||||||
|
import ru.ip.labworks.labworks.bookshop.service.GenreService;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import javax.persistence.EntityNotFoundException;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
public class JpaBookTests {
|
||||||
|
@Autowired
|
||||||
|
BookService bookService;
|
||||||
|
@Autowired
|
||||||
|
GenreService genreService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestAddBook(){
|
||||||
|
bookService.deleteAllBooks();
|
||||||
|
final Book book = bookService.addBook("test", "03.04.2023", new File("D:\\Учёба\\Интернет программирование\\PIbd-22_Bondarenko_M.S._IP\\src\\main\\resources\\glow.png"));
|
||||||
|
Assertions.assertNotNull(book.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestFindBook(){
|
||||||
|
bookService.deleteAllBooks();
|
||||||
|
final Book book = bookService.addBook("Test","03.04.2023", new File("D:\\Учёба\\Интернет программирование\\PIbd-22_Bondarenko_M.S._IP\\src\\main\\resources\\glow.png"));
|
||||||
|
final Book findBook = bookService.findBook(book.getId());
|
||||||
|
Assertions.assertEquals(book, findBook);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestBookReadNotFound(){
|
||||||
|
bookService.deleteAllBooks();
|
||||||
|
Assertions.assertThrows(EntityNotFoundException.class, () -> bookService.findBook(-1L));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestFindAllBook(){
|
||||||
|
bookService.deleteAllBooks();
|
||||||
|
final Book book1 = bookService.addBook("Test1","03.04.2023", new File("D:\\Учёба\\Интернет программирование\\PIbd-22_Bondarenko_M.S._IP\\src\\main\\resources\\glow.png"));
|
||||||
|
final Book book2 = bookService.addBook("Test2","03.04.2023", new File("D:\\Учёба\\Интернет программирование\\PIbd-22_Bondarenko_M.S._IP\\src\\main\\resources\\glow.png"));
|
||||||
|
final List<Book> books = bookService.findAllBooks();
|
||||||
|
Assertions.assertEquals(books.size(), 2);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
void TestBookReadAllEmpty() {
|
||||||
|
bookService.deleteAllBooks();
|
||||||
|
final List<Book> books = bookService.findAllBooks();
|
||||||
|
Assertions.assertEquals(books.size(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestBookUpdate(){
|
||||||
|
bookService.deleteAllBooks();
|
||||||
|
Book book = bookService.addBook("Test1","03.04.2023", new File("D:\\Учёба\\Интернет программирование\\PIbd-22_Bondarenko_M.S._IP\\src\\main\\resources\\glow.png"));
|
||||||
|
book = bookService.updateBook(book.getId(), "Test2", "02.03.2023", new File("D:\\Учёба\\Интернет программирование\\PIbd-22_Bondarenko_M.S._IP\\src\\main\\resources\\glow.png"));
|
||||||
|
Assertions.assertEquals(book.getName(), "Test2");
|
||||||
|
Assertions.assertEquals(book.getRelease().toString(), "Thu Mar 02 00:00:00 GMT+04:00 2023");
|
||||||
|
Assertions.assertNotNull(book.getCover());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestDeleteBook(){
|
||||||
|
bookService.deleteAllBooks();
|
||||||
|
final Book book = bookService.addBook("Test","03.04.2023", new File("D:\\Учёба\\Интернет программирование\\PIbd-22_Bondarenko_M.S._IP\\src\\main\\resources\\glow.png"));
|
||||||
|
bookService.deleteBook(book.getId());
|
||||||
|
Assertions.assertThrows(EntityNotFoundException.class, () -> bookService.findBook(1L));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestAddGenreToBook(){
|
||||||
|
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"));
|
||||||
|
final Genre genre = genreService.addGenre("Test2");
|
||||||
|
bookService.addGenreToBook(book.getId(), genre);
|
||||||
|
book = bookService.findBook(book.getId());
|
||||||
|
Assertions.assertEquals(book.getGenres().size(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestRemoveBookFromAuthor(){
|
||||||
|
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"));
|
||||||
|
final Genre genre = genreService.addGenre("Test2");
|
||||||
|
bookService.addGenreToBook(book.getId(), genre);
|
||||||
|
bookService.removeGenreFromBook(book.getId(), genre);
|
||||||
|
book = bookService.findBook(book.getId());
|
||||||
|
Assertions.assertEquals(book.getGenres().size(), 0);
|
||||||
|
}
|
||||||
|
}
|
70
src/test/java/ru/ip/labworks/labworks/JpaGenreTests.java
Normal file
70
src/test/java/ru/ip/labworks/labworks/JpaGenreTests.java
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
package ru.ip.labworks.labworks;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
import javax.persistence.EntityNotFoundException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
public class JpaGenreTests {
|
||||||
|
@Autowired
|
||||||
|
GenreService genreService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestAddGenre(){
|
||||||
|
genreService.deleteAllGenres();
|
||||||
|
final Genre genre = genreService.addGenre("test");
|
||||||
|
Assertions.assertNotNull(genre.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestFindGenre(){
|
||||||
|
genreService.deleteAllGenres();
|
||||||
|
final Genre genre = genreService.addGenre("Test");
|
||||||
|
final Genre findGenre = genreService.findGenre(genre.getId());
|
||||||
|
Assertions.assertEquals(genre, findGenre);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestGenreReadNotFound(){
|
||||||
|
genreService.deleteAllGenres();
|
||||||
|
Assertions.assertThrows(EntityNotFoundException.class, () -> genreService.findGenre(-1L));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestFindAllGenre(){
|
||||||
|
genreService.deleteAllGenres();
|
||||||
|
final Genre genre1 = genreService.addGenre("Test1");
|
||||||
|
final Genre genre2 = genreService.addGenre("Test2");
|
||||||
|
final List<Genre> genres = genreService.findAllGenres();
|
||||||
|
Assertions.assertEquals(genres.size(), 2);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
void TestGenreReadAllEmpty() {
|
||||||
|
genreService.deleteAllGenres();
|
||||||
|
final List<Genre> genres = genreService.findAllGenres();
|
||||||
|
Assertions.assertEquals(genres.size(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestGenreUpdate(){
|
||||||
|
genreService.deleteAllGenres();
|
||||||
|
Genre genre = genreService.addGenre("Test1");
|
||||||
|
genre = genreService.updateGenre(genre.getId(), "Test2");
|
||||||
|
Assertions.assertEquals(genre.getName(), "Test2");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestDeleteGenre(){
|
||||||
|
genreService.deleteAllGenres();
|
||||||
|
final Genre genre = genreService.addGenre("Test");
|
||||||
|
genreService.deleteGenre(genre.getId());
|
||||||
|
Assertions.assertThrows(EntityNotFoundException.class, () -> genreService.findGenre(1L));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user