-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Book model, repository & service layers
- Loading branch information
1 parent
d836419
commit 12cbfff
Showing
11 changed files
with
309 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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,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); | ||
}; | ||
} | ||
} |
This file contains 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,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; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/org/bookstore/exceptions/DataProcessingException.java
This file contains 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.bookstore.exceptions; | ||
|
||
public class DataProcessingException extends RuntimeException { | ||
public DataProcessingException(String message) { | ||
super(message); | ||
} | ||
|
||
public DataProcessingException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
This file contains 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,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
11
src/main/java/org/bookstore/repository/BookRepository.java
This file contains 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.bookstore.repository; | ||
|
||
import java.util.List; | ||
import org.bookstore.model.Book; | ||
|
||
public interface BookRepository { | ||
|
||
Book save(Book book); | ||
|
||
List<Book> findAll(); | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/org/bookstore/repository/impl/BookRepositoryImpl.java
This file contains 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,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); | ||
} | ||
} | ||
} |
This file contains 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,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
29
src/main/java/org/bookstore/service/impl/BookServiceImpl.java
This file contains 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,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(); | ||
} | ||
} |
This file contains 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 +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 |
This file contains 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,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 |