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

Implemented BookDao #416

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

Conversation

andriy-linkov
Copy link

No description provided.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Great job on following the Java interface naming conventions in your BookDao interface and structuring your Book class according to JavaBean conventions! Just remember to keep an eye on the consistency of object creation and exception messages for future enhancements. Keep up the good work! 👍💡

✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨

BookDao bookDao = (BookDao) injector.getInstance(BookDao.class);

Book zapovit = new Book("zapovit", BigDecimal.valueOf(100));
Book newZapiovit = bookDao.create(zapovit);

Choose a reason for hiding this comment

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

The variable 'newZapiovit' is never used. If you don't need the result of 'bookDao.create(zapovit)', you can omit assigning it to a variable.

Book zapovit = new Book("zapovit", BigDecimal.valueOf(100));
Book newZapiovit = bookDao.create(zapovit);

Optional<Book> bookById2 = bookDao.findById(2L);

Choose a reason for hiding this comment

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

The variable 'bookById2' is never used. Make sure to use it or remove it if it's not necessary for the main logic.


Optional<Book> bookById2 = bookDao.findById(2L);

List<Book> allBooks = bookDao.findAll();

Choose a reason for hiding this comment

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

The variable 'allBooks' is never used. Make sure to use it or remove it if it's not necessary for the main logic.


List<Book> allBooks = bookDao.findAll();

Book updateZapovit = bookDao.update(zapovit);

Choose a reason for hiding this comment

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

The variable 'updateZapovit' is never used. If you don't need the result of 'bookDao.update(zapovit)', you can omit assigning it to a variable.


boolean isDeleted = bookDao.deleteById(3L);

Choose a reason for hiding this comment

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

The variable 'isDeleted' is never used. Consider logging the result or handling the deletion result in some way.

Comment on lines 99 to 100
throw new DataProcessingException(
"Expected to update at least one row, but updated 0 rows.");

Choose a reason for hiding this comment

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

Use DataProcessingException for consistency with the rest of the DAO layer.

}
return book;
} catch (SQLException e) {
throw new DataProcessingException("Can not create a connection to the database", e);

Choose a reason for hiding this comment

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

The exception message should be more informative and specific to the operation being performed. For example: 'Can't update book: ' + book


@Override
public boolean deleteById(Long id) {
String sql = "DELETE FROM books WHERE id = ?;";

Choose a reason for hiding this comment

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

Use uppercase for SQL keywords.

PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setLong(1, id);
int affectedRows = statement.executeUpdate();
return affectedRows >= 1;

Choose a reason for hiding this comment

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

The deleteById method should return true only if one or more rows were affected. The current implementation is correct, so no action is needed here.

int affectedRows = statement.executeUpdate();
return affectedRows >= 1;
} catch (SQLException e) {
throw new DataProcessingException("Can not create a connection to the database", e);

Choose a reason for hiding this comment

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

The exception message should be more informative and specific to the operation being performed. For example: 'Can't delete book with id: ' + id

@@ -0,0 +1,34 @@
package mate.academy;

Choose a reason for hiding this comment

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

Suggested change
package mate.academy;
package mate.academy.util;

statement.setLong(1, id);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
String title = resultSet.getString("title");

Choose a reason for hiding this comment

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

create separate methods to map result set to book

PreparedStatement statement = connection.prepareStatement(sql)) {
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
Long id = resultSet.getObject("id", Long.class);

Choose a reason for hiding this comment

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

use separate method from comment above

USE `intro`;

CREATE TABLE `books` (
`id` INT NOT NULL AUTO_INCREMENT,

Choose a reason for hiding this comment

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

Suggested change
`id` INT NOT NULL AUTO_INCREMENT,
`id` BIGINT AUTO_INCREMENT,

Choose a reason for hiding this comment

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

not fixed

CREATE TABLE `books` (
`id` INT NOT NULL AUTO_INCREMENT,
`title` VARCHAR(255),
`price` INT,

Choose a reason for hiding this comment

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

Suggested change
`price` INT,
`price` DECIMAL,

Copy link

@ahoienko ahoienko left a comment

Choose a reason for hiding this comment

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

Nice, left minor comments

Comment on lines 19 to 28
private Book mapToBook(ResultSet resultSet) throws SQLException {
Long id = resultSet.getObject("id", Long.class);
String title = resultSet.getString("title");
BigDecimal price = resultSet.getObject("price", BigDecimal.class);
Book book = new Book();
book.setId(id);
book.setTitle(title);
book.setPrice(price);
return book;
}

Choose a reason for hiding this comment

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

Private methods should be after all public

Comment on lines 98 to 101
if (affectedRows < 1) {
throw new DataProcessingException(
"Expected to update at least one row, but updated 0 rows.");
}

Choose a reason for hiding this comment

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

Code duplication

Comment on lines 94 to 96
statement.setLong(3, book.getId());
statement.setString(1, book.getTitle());
statement.setBigDecimal(2, book.getPrice());

Choose a reason for hiding this comment

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

Suggested change
statement.setLong(3, book.getId());
statement.setString(1, book.getTitle());
statement.setBigDecimal(2, book.getPrice());
statement.setString(1, book.getTitle());
statement.setBigDecimal(2, book.getPrice());
statement.setLong(3, book.getId());

USE `intro`;

CREATE TABLE `books` (
`id` INT NOT NULL AUTO_INCREMENT,

Choose a reason for hiding this comment

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

not fixed

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