-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Book entity with associated repository and service layers.
- Loading branch information
1 parent
c823456
commit ca41bc0
Showing
10 changed files
with
211 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/mate/academy/bookstore/BookStoreApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,35 @@ | ||
package mate.academy.bookstore; | ||
|
||
import java.math.BigDecimal; | ||
import mate.academy.bookstore.model.Book; | ||
import mate.academy.bookstore.service.BookService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
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 BookStoreApplication { | ||
@Autowired | ||
private BookService bookService; | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(BookStoreApplication.class, args); | ||
} | ||
|
||
@Bean | ||
public CommandLineRunner commandLineRunner() { | ||
return args -> { | ||
Book book = new Book(); | ||
book.setTitle("Kobzar"); | ||
book.setAuthor("Taras Shevchenko"); | ||
book.setDescription("Description"); | ||
book.setIsbn("ISBN 123456"); | ||
book.setPrice(BigDecimal.TEN); | ||
|
||
bookService.save(book); | ||
bookService.findAll().forEach(System.out::println); | ||
}; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/mate/academy/bookstore/exception/DataProcessingException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package mate.academy.bookstore.exception; | ||
|
||
public class DataProcessingException extends RuntimeException { | ||
public DataProcessingException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package mate.academy.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; | ||
|
||
@Entity | ||
@Data | ||
@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; | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/mate/academy/bookstore/repository/BookRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package mate.academy.bookstore.repository; | ||
|
||
import java.util.List; | ||
import mate.academy.bookstore.model.Book; | ||
|
||
public interface BookRepository { | ||
Book save(Book book); | ||
|
||
List<Book> findAll(); | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/mate/academy/bookstore/repository/impl/BookRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package mate.academy.bookstore.repository.impl; | ||
|
||
import jakarta.persistence.criteria.CriteriaQuery; | ||
import java.util.List; | ||
import mate.academy.bookstore.exception.DataProcessingException; | ||
import mate.academy.bookstore.model.Book; | ||
import mate.academy.bookstore.repository.BookRepository; | ||
import org.hibernate.Session; | ||
import org.hibernate.SessionFactory; | ||
import org.hibernate.Transaction; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public class BookRepositoryImpl implements BookRepository { | ||
private final SessionFactory sessionFactory; | ||
|
||
@Autowired | ||
public BookRepositoryImpl(SessionFactory sessionFactory) { | ||
this.sessionFactory = sessionFactory; | ||
} | ||
|
||
@Override | ||
public Book save(Book book) { | ||
Session session = null; | ||
Transaction transaction = null; | ||
try { | ||
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 into DB: " + book, e); | ||
} finally { | ||
if (session != null) { | ||
session.close(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public List<Book> findAll() { | ||
try (Session session = sessionFactory.openSession()) { | ||
CriteriaQuery<Book> criteriaQuery = session | ||
.getCriteriaBuilder().createQuery(Book.class); | ||
criteriaQuery.from(Book.class); | ||
return session.createQuery(criteriaQuery).getResultList(); | ||
} catch (Exception e) { | ||
throw new DataProcessingException("Can't find all books", e); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/mate/academy/bookstore/service/BookService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package mate.academy.bookstore.service; | ||
|
||
import java.util.List; | ||
import mate.academy.bookstore.model.Book; | ||
|
||
public interface BookService { | ||
Book save(Book book); | ||
|
||
List<Book> findAll(); | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/mate/academy/bookstore/service/impl/BookServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package mate.academy.bookstore.service.impl; | ||
|
||
import java.util.List; | ||
import mate.academy.bookstore.model.Book; | ||
import mate.academy.bookstore.repository.BookRepository; | ||
import mate.academy.bookstore.service.BookService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class BookServiceImpl implements BookService { | ||
private final BookRepository bookRepository; | ||
|
||
@Autowired | ||
public BookServiceImpl(BookRepository bookRepository) { | ||
this.bookRepository = bookRepository; | ||
} | ||
|
||
@Override | ||
public Book save(Book book) { | ||
return bookRepository.save(book); | ||
} | ||
|
||
@Override | ||
public List<Book> findAll() { | ||
return bookRepository.findAll(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,9 @@ | ||
spring.application.name=book-store | ||
spring.datasource.url=jdbc:mysql://localhost:3306/bookstore?serverTimezone=UTC | ||
spring.datasource.username=root | ||
spring.datasource.password=password | ||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver | ||
|
||
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect | ||
spring.jpa.hibernate.ddl-auto=create-drop | ||
spring.jpa.show-sql=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |