Skip to content
Open
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
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand All @@ -67,6 +73,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class SpringBootBookShopApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootBookShopApplication.class, args);
}

@Bean
public CommandLineRunner commandLineRunner() {
return new CommandLineRunner() {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/example/exception/DataProcessingException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.example.exception;

public class DataProcessingException extends RuntimeException {
public DataProcessingException(String message, Exception e) {
super(message);
}

public DataProcessingException(String message) {
super(message);
}
}
26 changes: 26 additions & 0 deletions src/main/java/org/example/model/Book.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,30 @@
package org.example.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 = "book")
public class Book {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove empty line at the start of the class implementation

@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;
}
6 changes: 6 additions & 0 deletions src/main/java/org/example/repository/BookRepository.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
package org.example.repository;

import java.util.List;
import org.example.model.Book;

public interface BookRepository {
Book save(Book book);

List<Book> findAll();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,54 @@
package org.example.repository.impl;

public class BookRepositoryImpl {
import jakarta.persistence.criteria.CriteriaQuery;
import java.util.List;
import org.example.exception.DataProcessingException;
import org.example.model.Book;
import org.example.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;
Session session = 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 insert book " + 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 get all books", e);
}
}
}
6 changes: 6 additions & 0 deletions src/main/java/org/example/service/BookService.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
package org.example.service;

import java.util.List;
import org.example.model.Book;

public interface BookService {
Book save(Book book);

List<Book> findAll();
}
24 changes: 23 additions & 1 deletion src/main/java/org/example/service/impl/BookServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
package org.example.service.impl;

public class BookServiceImpl {
import java.util.List;
import org.example.model.Book;
import org.example.repository.BookRepository;
import org.example.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();
}
}
5 changes: 3 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
spring.datasource.url=jdbc:mysql://localhost:3306/svitlana?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=Wetuop34!
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.show-sql=true
5 changes: 3 additions & 2 deletions src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
spring.datasource.url=jdbc:mysql://localhost:3306/svitlana?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=Wetuop34!
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.show-sql=true