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

Hw jv jdbc intro #39

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

Conversation

Denis-Balako
Copy link

No description provided.

Copy link

@ivan0dyatlyukkk ivan0dyatlyukkk left a comment

Choose a reason for hiding this comment

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

Excellent job 💯 ! There are some suggestions :)

pom.xml Outdated
<version>8.1.0</version>
</dependency>
</dependencies>

Choose a reason for hiding this comment

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

Let's remove this redundant line, if you don't mind :)

Comment on lines 35 to 50
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Book book = (Book) o;
return Objects.equals(id, book.id) && Objects.equals(title, book.title) && Objects.equals(price, book.price);
}

@Override
public int hashCode() {
return Objects.hash(id, title, price);
}

Choose a reason for hiding this comment

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

I think it's off the map in this task

Copy link

@andrii-hoienko andrii-hoienko left a comment

Choose a reason for hiding this comment

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

Good, but let`s improve your solution

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

Choose a reason for hiding this comment

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

What about naming constants rules?

public static void main(String[] args) {
BookDao bookDao = (BookDao) injector.getInstance(BookDao.class);

Book EffectiveJava = new Book();

Choose a reason for hiding this comment

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

when we start naming variables from upper case letter?

}

private void checkInsertedRowsAmount(int affectedRows) {
if (affectedRows < 1) {

Choose a reason for hiding this comment

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

magic number

Book book = new Book();
book.setId(resultSet.getObject("id", Long.class));
book.setTitle(resultSet.getString("title"));
book.setPrice(resultSet.getBigDecimal("price"));

Choose a reason for hiding this comment

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

What is price is not defined in db?

String query = "DELETE FROM books WHERE id = ?";
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
statement.setLong(1, id);

Choose a reason for hiding this comment

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

magic number (check others as well)

book.setId(id);
}
} catch (SQLException e) {
throw new DataProcessingException("Can't add new book: " + book, e);

Choose a reason for hiding this comment

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

Suggested change
throw new DataProcessingException("Can't add new book: " + book, e);
throw new DataProcessingException("Can't create new book: " + book, e);

import mate.academy.lib.Dao;
import mate.academy.model.Book;
import mate.academy.services.ConnectionUtil;
import java.sql.*;

Choose a reason for hiding this comment

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

try to avoid using asterix in imports


ResultSet generatedKeys = statement.getGeneratedKeys();
if(generatedKeys.next()) {
Long id = generatedKeys.getObject(1, Long.class);

Choose a reason for hiding this comment

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

whink which data should be returned the one you provide in request or the one which is actually saved?

Choose a reason for hiding this comment

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

This is not resolved. Here I wish to see that you populating book with data that actually in data base. Because you could do some manipulation with data and then save it, but in your implementation you will return not actual data

}

private BigDecimal getBookPrice(ResultSet resultSet) throws SQLException {
return resultSet.getBigDecimal("price") != null

Choose a reason for hiding this comment

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

It is not require to create new method to hadle it, just do tha same as you did for id


ResultSet generatedKeys = statement.getGeneratedKeys();
if(generatedKeys.next()) {
Long id = generatedKeys.getObject(1, Long.class);

Choose a reason for hiding this comment

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

This is not resolved. Here I wish to see that you populating book with data that actually in data base. Because you could do some manipulation with data and then save it, but in your implementation you will return not actual data

Copy link

@andrii-hoienko andrii-hoienko left a comment

Choose a reason for hiding this comment

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

Much better, but still some comments to resolve

Copy link

@andrii-hoienko andrii-hoienko left a comment

Choose a reason for hiding this comment

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

Good job!

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.

3 participants