Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Book model, repository & service layers #1

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,33 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/bookstore/JvBookStoreApplication.java
Original file line number Diff line number Diff line change
@@ -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);
};
}
}
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);
}
}
29 changes: 29 additions & 0 deletions src/main/java/org/bookstore/model/Book.java
Original file line number Diff line number Diff line change
@@ -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;
}
10 changes: 10 additions & 0 deletions src/main/java/org/bookstore/repository/BookRepository.java
Original file line number Diff line number Diff line change
@@ -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<Book> findAll();
}
Original file line number Diff line number Diff line change
@@ -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<Book> 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);
}
}
}
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();
}
26 changes: 26 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,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<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
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
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
Loading