diff --git a/pom.xml b/pom.xml
index d89de51..4552e1d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -22,12 +22,33 @@
org.springframework.boot
spring-boot-starter
-
org.springframework.boot
spring-boot-starter-test
test
+
+ org.springframework.boot
+ spring-boot-starter-data-jpa
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+ com.h2database
+ h2
+ test
+
+
+ mysql
+ mysql-connector-java
+ 8.0.33
+
+
+ org.projectlombok
+ lombok
+
diff --git a/src/main/java/org/bookstore/JvBookStoreApplication.java b/src/main/java/org/bookstore/JvBookStoreApplication.java
index f79229e..03d6399 100644
--- a/src/main/java/org/bookstore/JvBookStoreApplication.java
+++ b/src/main/java/org/bookstore/JvBookStoreApplication.java
@@ -1,13 +1,32 @@
package org.bookstore;
+import java.math.BigDecimal;
+import org.bookstore.model.Book;
+import org.bookstore.service.BookService;
+import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class JvBookStoreApplication {
+ private BookService bookService;
public static void main(String[] args) {
SpringApplication.run(JvBookStoreApplication.class, args);
}
+ @Bean
+ public CommandLineRunner commandLineRunner(BookService bookService) {
+ return args -> {
+ Book book = new Book();
+ book.setAuthor("Author");
+ book.setDescription("Description");
+ book.setPrice(BigDecimal.ZERO);
+ book.setTitle("Title");
+ book.setIsbn("ISBN");
+ bookService.save(book);
+ bookService.findAll().forEach(System.out::println);
+ };
+ }
}
diff --git a/src/main/java/org/bookstore/exceptions/DataProcessingException.java b/src/main/java/org/bookstore/exceptions/DataProcessingException.java
new file mode 100644
index 0000000..92aeb97
--- /dev/null
+++ b/src/main/java/org/bookstore/exceptions/DataProcessingException.java
@@ -0,0 +1,11 @@
+package org.bookstore.exceptions;
+
+public class DataProcessingException extends RuntimeException {
+ public DataProcessingException(String message) {
+ super(message);
+ }
+
+ public DataProcessingException(String message, Throwable cause) {
+ super(message, cause);
+ }
+}
diff --git a/src/main/java/org/bookstore/model/Book.java b/src/main/java/org/bookstore/model/Book.java
new file mode 100644
index 0000000..ac3e716
--- /dev/null
+++ b/src/main/java/org/bookstore/model/Book.java
@@ -0,0 +1,29 @@
+package org.bookstore.model;
+
+import jakarta.persistence.Column;
+import jakarta.persistence.Entity;
+import jakarta.persistence.GeneratedValue;
+import jakarta.persistence.GenerationType;
+import jakarta.persistence.Id;
+import jakarta.persistence.Table;
+import java.math.BigDecimal;
+import lombok.Data;
+
+@Data
+@Entity
+@Table(name = "books")
+public class Book {
+ @Id
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
+ private Long id;
+ @Column(nullable = false)
+ private String title;
+ @Column(nullable = false)
+ private String author;
+ @Column(nullable = false, unique = true)
+ private String isbn;
+ @Column(nullable = false)
+ private BigDecimal price;
+ private String description;
+ private String coverImage;
+}
diff --git a/src/main/java/org/bookstore/repository/BookRepository.java b/src/main/java/org/bookstore/repository/BookRepository.java
new file mode 100644
index 0000000..91c5651
--- /dev/null
+++ b/src/main/java/org/bookstore/repository/BookRepository.java
@@ -0,0 +1,10 @@
+package org.bookstore.repository;
+
+import java.util.List;
+import org.bookstore.model.Book;
+
+public interface BookRepository {
+ Book save(Book book);
+
+ List findAll();
+}
diff --git a/src/main/java/org/bookstore/repository/impl/BookRepositoryImpl.java b/src/main/java/org/bookstore/repository/impl/BookRepositoryImpl.java
new file mode 100644
index 0000000..32a13fe
--- /dev/null
+++ b/src/main/java/org/bookstore/repository/impl/BookRepositoryImpl.java
@@ -0,0 +1,44 @@
+package org.bookstore.repository.impl;
+
+import java.util.List;
+import org.bookstore.exceptions.DataProcessingException;
+import org.bookstore.model.Book;
+import org.bookstore.repository.BookRepository;
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+import org.hibernate.Transaction;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public class BookRepositoryImpl implements BookRepository {
+ private final SessionFactory sessionFactory;
+
+ public BookRepositoryImpl(SessionFactory sessionFactory) {
+ this.sessionFactory = sessionFactory;
+ }
+
+ @Override
+ public Book save(Book book) {
+ Transaction transaction = null;
+ try (Session session = sessionFactory.openSession()) {
+ transaction = session.beginTransaction();
+ session.persist(book);
+ transaction.commit();
+ return book;
+ } catch (Exception e) {
+ if (transaction != null) {
+ transaction.rollback();
+ }
+ throw new DataProcessingException("Can't save book " + book, e);
+ }
+ }
+
+ @Override
+ public List findAll() {
+ try (Session session = sessionFactory.openSession()) {
+ return session.createQuery("from Book", Book.class).getResultList();
+ } catch (Exception e) {
+ throw new DataProcessingException("Can't get all books", e);
+ }
+ }
+}
diff --git a/src/main/java/org/bookstore/service/BookService.java b/src/main/java/org/bookstore/service/BookService.java
new file mode 100644
index 0000000..09e9314
--- /dev/null
+++ b/src/main/java/org/bookstore/service/BookService.java
@@ -0,0 +1,10 @@
+package org.bookstore.service;
+
+import java.util.List;
+import org.bookstore.model.Book;
+
+public interface BookService {
+ Book save(Book book);
+
+ List findAll();
+}
diff --git a/src/main/java/org/bookstore/service/impl/BookServiceImpl.java b/src/main/java/org/bookstore/service/impl/BookServiceImpl.java
new file mode 100644
index 0000000..6c44e2b
--- /dev/null
+++ b/src/main/java/org/bookstore/service/impl/BookServiceImpl.java
@@ -0,0 +1,26 @@
+package org.bookstore.service.impl;
+
+import java.util.List;
+import org.bookstore.model.Book;
+import org.bookstore.repository.BookRepository;
+import org.bookstore.service.BookService;
+import org.springframework.stereotype.Service;
+
+@Service
+public class BookServiceImpl implements BookService {
+ private final BookRepository bookRepository;
+
+ public BookServiceImpl(BookRepository bookRepository) {
+ this.bookRepository = bookRepository;
+ }
+
+ @Override
+ public Book save(Book book) {
+ return bookRepository.save(book);
+ }
+
+ @Override
+ public List findAll() {
+ return bookRepository.findAll();
+ }
+}
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index 8b13789..fb5645a 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -1 +1,6 @@
-
+spring.jpa.hibernate.ddl-auto=create-drop
+spring.jpa.show-sql=true
+spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
+spring.datasource.url=jdbc:mysql://localhost:3306/db
+spring.datasource.username=slava
+spring.datasource.password=12345
diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties
new file mode 100644
index 0000000..bc2fdde
--- /dev/null
+++ b/src/test/resources/application.properties
@@ -0,0 +1,5 @@
+spring.datasource.url=jdbc:h2:mem:testdb
+spring.datasource.driverClassName=org.h2.Driver
+spring.datasource.username=sa
+spring.datasource.password=password
+spring.jpa.database-platform=org.hibernate.dialect.H2Dialect