Skip to content

Commit

Permalink
all fix + add custom exception
Browse files Browse the repository at this point in the history
  • Loading branch information
foxstudent106239 committed Aug 20, 2024
1 parent 17c6ee6 commit ad6dd1e
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mate.academy;
package mate.academy.util;

import java.io.FileInputStream;
import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package mate.academy;
package mate.academy.util;

import java.math.BigDecimal;
import mate.academy.dao.BookDao;
import mate.academy.domain.Book;
import mate.academy.lib.Injector;
import mate.academy.util.dao.BookDao;
import mate.academy.util.domain.Book;
import mate.academy.util.lib.Injector;

public class Main {
private static final Injector injector = Injector.getInstance("mate.academy.dao");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package mate.academy.dao;
package mate.academy.util.dao;

import java.util.List;
import java.util.Optional;
import mate.academy.domain.Book;
import mate.academy.util.domain.Book;

public interface BookDao {

Book create(Book book);

Optional<Book> findById(int id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mate.academy.dao;
package mate.academy.util.dao;

import java.math.BigDecimal;
import java.sql.Connection;
Expand All @@ -9,13 +9,15 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import mate.academy.ConnectionUtil;
import mate.academy.domain.Book;
import mate.academy.lib.Dao;
import mate.academy.util.ConnectionUtil;
import mate.academy.util.domain.Book;
import mate.academy.util.exception.DataProcessingException;
import mate.academy.util.lib.Dao;

@Dao
public class BookDaoImpl implements BookDao {
private static final String CREATE_BOOK_QUERY = "INSERT INTO book (title, price) VALUES (?, ?)";
private static final String CREATE_BOOK_QUERY = "INSERT INTO book (id, title, price) "
+ "VALUES (?, ?, ?)";
private static final String FIND_BOOK_BY_ID_QUERY = "SELECT * FROM book WHERE id = ?";
private static final String FIND_ALL_BOOK_QUERY = "SELECT * FROM book";
private static final String UPDATE_BOOK_QUERY = "UPDATE book SET title = ?, "
Expand All @@ -24,73 +26,73 @@ public class BookDaoImpl implements BookDao {

@Override
public Book create(Book book) {
Book createdBook;
try (Connection connection = ConnectionUtil.connectToDatabase();
PreparedStatement preparedStatement = connection.prepareStatement(CREATE_BOOK_QUERY,
Statement.RETURN_GENERATED_KEYS)) {
preparedStatement.setLong(1, book.getId());
preparedStatement.setString(2, book.getTitle());
preparedStatement.setBigDecimal(3, book.getPrice());
preparedStatement.execute();
try (ResultSet resultSet = preparedStatement.getGeneratedKeys()) {
resultSet.next();
createdBook = new Book(
resultSet.getString("title"),
resultSet.getBigDecimal("price"));
int affectedRows = preparedStatement.executeUpdate();
if (affectedRows < 1) {
throw new DataProcessingException(
"Expected to insert at least 1 row, but was 0");
}
} catch (SQLException throwables) {
throw new RuntimeException(throwables);
try (ResultSet generatedKeys = preparedStatement.getGeneratedKeys()) {
if (generatedKeys.next()) {
Long id = generatedKeys.getObject(1, Long.class);
book.setId(id);
}
}
} catch (SQLException e) {
throw new DataProcessingException("Can't add a new book" + book, e);
}
return createdBook;
return book;
}

@Override
public Optional<Book> findById(int id) {
Book findBookById = null;
Optional<Book> findBookById = Optional.empty();
try (Connection connection = ConnectionUtil.connectToDatabase();
PreparedStatement preparedStatement =
connection.prepareStatement(FIND_BOOK_BY_ID_QUERY,
Statement.RETURN_GENERATED_KEYS)) {
connection.prepareStatement(FIND_BOOK_BY_ID_QUERY)) {
preparedStatement.setLong(1, id);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Long bookId = resultSet.getLong("id");
String title = resultSet.getString("title");
BigDecimal price = resultSet.getBigDecimal("price");
findBookById = new Book(bookId, title, price);
if (resultSet.next()) {
findBookById = Optional.of(mapResutSetToBook(resultSet, id));
}
} catch (SQLException throwables) {
throw new RuntimeException(throwables);
} catch (SQLException e) {
throw new DataProcessingException("Can't find book by id" + id, e);
}
return Optional.ofNullable(findBookById);
return findBookById;
}

@Override
public List<Book> findAll() {
List<Book> foundBooks = new ArrayList<>();
List<Book> books = new ArrayList<>();
try (Connection connection = ConnectionUtil.connectToDatabase();
PreparedStatement preparedStatement =
connection.prepareStatement(FIND_ALL_BOOK_QUERY,
Statement.RETURN_GENERATED_KEYS)) {
connection.prepareStatement(FIND_ALL_BOOK_QUERY)) {

ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Long bookId = resultSet.getLong("id");
String title = resultSet.getString("title");
BigDecimal price = resultSet.getBigDecimal("price");
foundBooks.add(new Book(bookId, title, price));
books.add(mapResutSetToBook(resultSet, bookId));
}
} catch (SQLException throwables) {
throw new RuntimeException(throwables);
} catch (SQLException e) {
throw new DataProcessingException("Can't find all books", e);
}
return foundBooks;
return books;
}

@Override
public Book update(Book book) {
Book updatedBook = book;
try (Connection connection = ConnectionUtil.connectToDatabase();
PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_BOOK_QUERY,
Statement.RETURN_GENERATED_KEYS)) {
try (Connection connection =
ConnectionUtil.connectToDatabase();
PreparedStatement preparedStatement =
connection.prepareStatement(UPDATE_BOOK_QUERY)) {
preparedStatement.setLong(1, book.getId());
preparedStatement.setString(2, book.getTitle());
preparedStatement.setBigDecimal(3, book.getPrice());
ResultSet resultSet = preparedStatement.executeQuery();
Expand All @@ -100,24 +102,34 @@ public Book update(Book book) {
updatedBook.setTitle(title);
updatedBook.setPrice(price);
}
} catch (SQLException throwables) {
throw new RuntimeException(throwables);
} catch (SQLException e) {
throw new DataProcessingException("Can't update book: " + book, e);
}
return updatedBook;
}

@Override
public boolean deleteById(int id) {
boolean isDeleted;
try (Connection connection = ConnectionUtil.connectToDatabase();
PreparedStatement preparedStatement =
connection.prepareStatement(DELETE_BOOK_QUERY)) {
preparedStatement.setLong(1, id);
isDeleted = preparedStatement.execute();
} catch (SQLException throwables) {
throw new RuntimeException(throwables);
return preparedStatement.executeUpdate() > 0;
} catch (SQLException e) {
throw new DataProcessingException("Can't delete book by id: " + id, e);
}
}

private Book mapResutSetToBook(ResultSet resultSet, long id) {
Book book;
try {
String title = resultSet.getString("title");
BigDecimal price = resultSet.getBigDecimal("price");
book = new Book(id, title, price);
} catch (SQLException e) {
throw new DataProcessingException("Can't map result set to book: " + resultSet, e);
}
return isDeleted;
return book;
}
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mate.academy.domain;
package mate.academy.util.domain;

import java.math.BigDecimal;

Expand Down Expand Up @@ -44,4 +44,17 @@ public BigDecimal getPrice() {
public void setPrice(BigDecimal price) {
this.price = price;
}

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

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

public DataProcessingException(String message, Throwable cause) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mate.academy.lib;
package mate.academy.util.lib;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mate.academy.lib;
package mate.academy.util.lib;

import java.io.File;
import java.io.IOException;
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/db.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
db.url=jdbc:mysql://localhost:3307
db.username=postgres
db.password=root
db.url=jdbc:mysql://localhost:3307/test
db.username=
db.password=
2 changes: 1 addition & 1 deletion src/main/resources/init_db.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
create schema if not exists test;

create table if not exists book (
book_id serial primary key,
book_id bigint primary key,
title varchar(20),
price decimal
);

0 comments on commit ad6dd1e

Please sign in to comment.