Skip to content

Commit

Permalink
Added Book entity with associated repository and service layers.
Browse files Browse the repository at this point in the history
  • Loading branch information
nklimovych committed Apr 17, 2024
1 parent c823456 commit ca41bc0
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 2 deletions.
38 changes: 36 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.2.7.Final</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand All @@ -36,6 +40,30 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.3.0</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.7.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -71,8 +99,14 @@
<linkXRef>false</linkXRef>
</configuration>
</plugin>

</plugins>
</build>
<repositories>
<repository>
<id>maven_central</id>
<name>Maven Central</name>
<url>https://repo.maven.apache.org/maven2/</url>
</repository>
</repositories>

</project>
22 changes: 22 additions & 0 deletions src/main/java/mate/academy/bookstore/BookStoreApplication.java
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);
};
}
}
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);
}
}
29 changes: 29 additions & 0 deletions src/main/java/mate/academy/bookstore/model/Book.java
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;
}
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();
}
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 src/main/java/mate/academy/bookstore/service/BookService.java
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();
}
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();
}
}
8 changes: 8 additions & 0 deletions src/main/resources/application.properties
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
5 changes: 5 additions & 0 deletions src/test/resources/application.properties
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

0 comments on commit ca41bc0

Please sign in to comment.