Skip to content
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

added BookDao and connected project to db #53

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Someboty
Copy link

No description provided.

Copy link

@irynamekh irynamekh left a comment

Choose a reason for hiding this comment

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

there are extra spaces between the imports, but overall I have no complaints, great job!

src/main/java/mate/academy/Main.java Show resolved Hide resolved
Copy link

@nacenik nacenik left a comment

Choose a reason for hiding this comment

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

Good solution, let's improve it

Comment on lines 8 to 12
Book create(Book book);
Optional<Book> findById(Long id);
List<Book> findAll();
Book update(Book book);
boolean deleteById(Long id);
Copy link

Choose a reason for hiding this comment

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

add spaces between methods

Comment on lines 26 to 30
private static final String CREATE_QUERY = "INSERT INTO books (id, title, price) VALUES (?, ?, ?)";
private static final String FIND_BY_ID_QUERY = "SELECT * FROM books WHERE id = ?";
private static final String FIND_ALL_QUERY = "SELECT * FROM books";
private static final String UPDATE_QUERY = "UPDATE books SET title = ?, price = ? WHERE id = ?";
private static final String DELETE_QUERY = "DELETE FROM books WHERE id = ?";
Copy link

Choose a reason for hiding this comment

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

Create a query in the method where you will use it. Like method variable

private static final String ID_COLUMN = "books.id";
private static final String TITLE_COLUMN = "books.title";
private static final String PRICE_COLUMN = "books.price";
private static final String NO_UPDATES_EXCEPTION_TEXT = "Expected to insert at least 1 row, but inserted 0 rows.";
Copy link

Choose a reason for hiding this comment

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

are you inserting rows during the update? Or do you just update values?

private static final int TITLE_INDEX_FOR_UPDATE = 1;
private static final int PRICE_INDEX_FOR_UPDATE = 2;
private static final int ID_INDEX_FOR_UPDATE = 3;
private static final String CREATE_QUERY = "INSERT INTO books (id, title, price) VALUES (?, ?, ?)";
Copy link

Choose a reason for hiding this comment

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

The id is auto-generated. you don't need to set it up by your hand.

Copy link
Author

Choose a reason for hiding this comment

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

It's not for updating “id” value, it's index of “id” column for update operation (“…WHERE id = ?”). But I'll rename it for better readability.

}
}

private Book formBookFromQuery(ResultSet resultSet) throws SQLException{
Copy link

Choose a reason for hiding this comment

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

bad method name

Suggested change
private Book formBookFromQuery(ResultSet resultSet) throws SQLException{
private Book parseBookFromQuery(ResultSet resultSet) throws SQLException{

public DataProcessingException(String message, Throwable ex) {
super(message, ex);
}

Copy link

Choose a reason for hiding this comment

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

Suggested change

@@ -0,0 +1,3 @@
CREATE DATABASE book_schema;
USE book_schema;
CREATE TABLE books (`id` BIGINT NOT NULL AUTO_INCREMENT, `title` VARCHAR(255), `price` DECIMAL, PRIMARY KEY (`id`));
Copy link

Choose a reason for hiding this comment

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

I'd recommend you put this query in separate lines. It's hard to read it.

… to methods variables, renamed update index's constants for better readability, renamed "formBookFromQuery()" into "parseBookFromQuery", removed redundant line in DataProcessingException, put query at init_db.sql in separate lines for better readability, changed NO_UPDATES_EXCEPTION_TEXT for greater correctness of the message
@Someboty Someboty requested a review from nacenik August 14, 2023 09:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants