Skip to content

Commit

Permalink
Added Book model, repository & service layers
Browse files Browse the repository at this point in the history
  • Loading branch information
viacheslav-torbin committed Oct 14, 2023
1 parent d836419 commit 12cbfff
Show file tree
Hide file tree
Showing 11 changed files with 309 additions and 3 deletions.
36 changes: 34 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,46 @@
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.3.1.Final</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.2.224</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>6.0.11</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.33</version>
</dependency>

</dependencies>

<build>
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/org/bookstore/JvBookStoreApplication.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
package org.bookstore;

import java.math.BigDecimal;
import org.bookstore.model.Book;
import org.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 JvBookStoreApplication {

@Autowired
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);
};
}
}
43 changes: 43 additions & 0 deletions src/main/java/org/bookstore/config/AppConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.bookstore.config;

import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;

@Configuration
@ComponentScan(basePackages = "org.bookstore")
@PropertySource("classpath:application.properties")
public class AppConfig {
@Autowired
private Environment environment;

@Bean
public DataSource getDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(environment.getProperty("db.driver-class-name"));
dataSource.setUrl(environment.getProperty("db.url"));
dataSource.setUsername(environment.getProperty("db.username"));
dataSource.setPassword(environment.getProperty("db.password"));
return dataSource;
}

@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
sessionFactoryBean.setDataSource(getDataSource());
sessionFactoryBean.setPackagesToScan("org.bookstore");
Properties properties = new Properties();
properties.put("show_sql", "true");
properties.put("hibernate.hbm2ddl.auto", "create-drop");
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
sessionFactoryBean.setHibernateProperties(properties);
return sessionFactoryBean;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
92 changes: 92 additions & 0 deletions src/main/java/org/bookstore/model/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
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 java.math.BigDecimal;

@Entity
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;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getIsbn() {
return isbn;
}

public void setIsbn(String isbn) {
this.isbn = isbn;
}

public BigDecimal getPrice() {
return price;
}

public void setPrice(BigDecimal price) {
this.price = price;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getCoverImage() {
return coverImage;
}

public void setCoverImage(String coverImage) {
this.coverImage = coverImage;
}

@Override
public String toString() {
return "Book{"
+ "id=" + id
+ ", title='" + title + '\''
+ ", author='" + author + '\''
+ ", isbn='" + isbn + '\''
+ ", price=" + price
+ '}';
}
}
11 changes: 11 additions & 0 deletions src/main/java/org/bookstore/repository/BookRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.bookstore.repository;

import java.util.List;
import org.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,46 @@
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.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) {
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<Book> findAll() {
try (Session session = sessionFactory.openSession()) {
return session.createQuery("from Book b", Book.class).getResultList();
} catch (Exception e) {
throw new DataProcessingException("Can't get all books", e);
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/org/bookstore/service/BookService.java
Original file line number Diff line number Diff line change
@@ -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<Book> findAll();
}
29 changes: 29 additions & 0 deletions src/main/java/org/bookstore/service/impl/BookServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
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();
}
}
7 changes: 6 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@

spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
db.driver-class-name=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/db
db.username=slava
db.password=12345
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 12cbfff

Please sign in to comment.