-
Notifications
You must be signed in to change notification settings - Fork 1
created book entity #2
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
Open
dedis34
wants to merge
1
commit into
master
Choose a base branch
from
develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
11 changes: 11 additions & 0 deletions
11
src/main/java/org/example/exception/DataProcessingException.java
This file contains hidden or 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,11 @@ | ||
| package org.example.exception; | ||
|
|
||
| public class DataProcessingException extends RuntimeException { | ||
| public DataProcessingException(String message, Exception e) { | ||
| super(message); | ||
| } | ||
|
|
||
| public DataProcessingException(String message) { | ||
| super(message); | ||
| } | ||
| } |
This file contains hidden or 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,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 { | ||
|
|
||
| @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; | ||
| } | ||
This file contains hidden or 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,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(); | ||
| } |
52 changes: 51 additions & 1 deletion
52
src/main/java/org/example/repository/impl/BookRepositoryImpl.java
This file contains hidden or 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,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); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,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
24
src/main/java/org/example/service/impl/BookServiceImpl.java
This file contains hidden or 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,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(); | ||
| } | ||
| } |
This file contains hidden or 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,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 |
This file contains hidden or 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,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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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