Skip to content

Commit

Permalink
fix: changes after comments
Browse files Browse the repository at this point in the history
  • Loading branch information
artemklishch committed May 29, 2024
1 parent 9d55c92 commit 5cc8d02
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 22 deletions.
7 changes: 5 additions & 2 deletions src/main/java/mate/academy/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
import mate.academy.dao.BookDao;
import mate.academy.lib.Injector;
import mate.academy.model.Book;
import mate.academy.util.ConnectionUtil;

public class Main {
private static final Injector INJECTOR = Injector.getInstance("mate.academy");

public static void main(String[] args) {
ConnectionUtil.initializeTable();

BookDao bookDao = (BookDao) INJECTOR.getInstance(BookDao.class);
Book book1 = new Book("Book of Jungles", new BigDecimal(100));
bookDao.create(book1);
Book book = new Book("Book of Jungles", new BigDecimal(100));
bookDao.create(book);

Optional<Book> bookById = bookDao.findById(1L);
System.out.println(bookById);
Expand Down
27 changes: 13 additions & 14 deletions src/main/java/mate/academy/dao/BookDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import mate.academy.ConnectionUtil;
import mate.academy.exceptions.DataProcessingException;
import mate.academy.lib.Dao;
import mate.academy.model.Book;
import mate.academy.util.ConnectionUtil;

@Dao
public class BookDaoImpl implements BookDao {
@Override
public Book create(Book book) {
String sql = "INSERT INTO books (title, price) values (?, ?)";
String query = "INSERT INTO books (title, price) values (?, ?)";
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(
sql, Statement.RETURN_GENERATED_KEYS
query, Statement.RETURN_GENERATED_KEYS
);
) {
statement.setString(1, book.getTitle());
statement.setBigDecimal(2, book.getPrice());
int affectedRows = statement.executeUpdate();
if (affectedRows < 1) {
throw new RuntimeException(
throw new DataProcessingException(
"Expected to insert at least one row, but inserted 0 rows."
);
}
Expand All @@ -45,9 +45,9 @@ public Book create(Book book) {

@Override
public Optional<Book> findById(Long id) {
String sql = "SELECT * FROM books WHERE id = ?";
String query = "SELECT * FROM books WHERE id = ?";
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(sql);
PreparedStatement statement = connection.prepareStatement(query);
) {
statement.setLong(1, id);
ResultSet resultSet = statement.executeQuery();
Expand All @@ -62,9 +62,9 @@ public Optional<Book> findById(Long id) {

@Override
public List<Book> findAll() {
String sql = "SELECT * FROM books";
String query = "SELECT * FROM books";
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(sql);
PreparedStatement statement = connection.prepareStatement(query);
) {
ResultSet resultSet = statement.executeQuery();
List<Book> books = new ArrayList<>();
Expand All @@ -79,9 +79,9 @@ public List<Book> findAll() {

@Override
public Book update(Book book) {
String sql = "UPDATE books SET title = ?, price = ? WHERE id = ?";
String query = "UPDATE books SET title = ?, price = ? WHERE id = ?";
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(sql);
PreparedStatement statement = connection.prepareStatement(query);
) {
statement.setString(1, book.getTitle());
statement.setBigDecimal(2, book.getPrice());
Expand Down Expand Up @@ -110,13 +110,12 @@ public boolean deleteById(Long id) {
);
}

String sql = "DELETE FROM books WHERE id = ?";
String query = "DELETE FROM books WHERE id = ?";
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(sql);
PreparedStatement statement = connection.prepareStatement(query);
) {
statement.setLong(1, id);
int affectedRows = statement.executeUpdate();
return affectedRows > 1;
return statement.executeUpdate() > 0;
} catch (SQLException e) {
throw new DataProcessingException(
"Can not delete the book with the provided ID: " + id
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/mate/academy/model/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

public class Book {
private Long id;
private final String title;
private final BigDecimal price;
private String title;
private BigDecimal price;

public Book(String title, BigDecimal price) {
this.title = title;
Expand Down Expand Up @@ -33,4 +33,13 @@ public String getTitle() {
public BigDecimal getPrice() {
return price;
}

@Override
public String toString() {
return "Book{"
+ "id=" + id
+ ", title='" + title + '\''
+ ", price=" + price
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mate.academy;
package mate.academy.util;

import java.io.BufferedReader;
import java.io.FileReader;
Expand All @@ -25,7 +25,6 @@ public class ConnectionUtil {

try {
Class.forName("com.mysql.cj.jdbc.Driver");
initializeTable();
} catch (ClassNotFoundException e) {
throw new DataProcessingException("Can not load JDBC driver");
}
Expand All @@ -35,7 +34,7 @@ public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(DB_URL, DB_PROPERTIES);
}

private static void initializeTable() {
public static void initializeTable() {
try (Connection connection = getConnection()) {
DatabaseMetaData databaseMetaData = connection.getMetaData();
ResultSet resultSet = databaseMetaData.getTables(
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/init_db.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE TABLE IF NOT EXISTS books (
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
id BIGINT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
price DECIMAL
);

0 comments on commit 5cc8d02

Please sign in to comment.