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

JDBC Intro Solution #30

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

Conversation

knyazkyylubomir
Copy link

No description provided.

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);

Choose a reason for hiding this comment

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

Suggested change
Book create(Book book);
Optional<Book> findById(Long id);
List<Book> findAll();
Book update(Book book);
boolean deleteById(Long id);
Book create(Book book);
Optional<Book> findById(Long id);
List<Book> findAll();
Book update(Book book);
boolean deleteById(Long id);


@Override
public Book create(Book book) {
if (book.getTitle() == null || book.getPrice() == null) {

Choose a reason for hiding this comment

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

optional validation in this case
and as a rule, validation is not done at the dao level

Comment on lines 46 to 48
if (affectedRows < LESS_THAN_ONE) {
throw new RuntimeException(AT_LEAST_ONE_ROW_EXCEPTION);
}

Choose a reason for hiding this comment

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

Suggested change
if (affectedRows < LESS_THAN_ONE) {
throw new RuntimeException(AT_LEAST_ONE_ROW_EXCEPTION);
}

public class BookDaoImpl implements BookDao {
private static final String AT_LEAST_ONE_ROW_EXCEPTION
= "Expected to insert at least one row, but inserted 0 rows";
private static final String ADD_BOOK_EXCEPTION = "Cannot add new book: ";

Choose a reason for hiding this comment

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

If the text for each error is different, I would not take it out as constants.

Comment on lines 62 to 64
if (id < LESS_THAN_ZERO) {
throw new RuntimeException(NEGATIVE_ID_EXCEPTION);
}

Choose a reason for hiding this comment

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

Suggested change
if (id < LESS_THAN_ZERO) {
throw new RuntimeException(NEGATIVE_ID_EXCEPTION);
}

BigDecimal price = resultSet.getObject("price", BigDecimal.class);
return Optional.of(new Book(id, model, price));
}
throw new RuntimeException(NO_BOOK_EXCEPTION + id);

Choose a reason for hiding this comment

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

Suggested change
throw new RuntimeException(NO_BOOK_EXCEPTION + id);

if (resultSet.next()) {
String model = resultSet.getString("title");
BigDecimal price = resultSet.getObject("price", BigDecimal.class);
return Optional.of(new Book(id, model, price));

Choose a reason for hiding this comment

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

You can take out the book variable before the try block, then use book = new Book(your parsed fields) in the try block, and return optional.ofNullable(book) after the catch block

Comment on lines 94 to 96
if (books.size() == ZERO) {
throw new RuntimeException(NO_RECORDS_EXCEPTION);
}

Choose a reason for hiding this comment

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

Suggested change
if (books.size() == ZERO) {
throw new RuntimeException(NO_RECORDS_EXCEPTION);
}

Comment on lines 89 to 91
Long id = resultSet.getObject("id", Long.class);
String model = resultSet.getString("title");
BigDecimal price = resultSet.getObject("price", BigDecimal.class);

Choose a reason for hiding this comment

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

You use this logic to parse book fields multiple times. Put it in a separate private method, something like
private Book parseBook(ResultSet resultSet)
return new Book(fields...)

Comment on lines 105 to 107
if (book.getTitle() == null || book.getPrice() == null) {
throw new RuntimeException(NULL_FIELDS_EXCEPTION);
}

Choose a reason for hiding this comment

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

Suggested change
if (book.getTitle() == null || book.getPrice() == null) {
throw new RuntimeException(NULL_FIELDS_EXCEPTION);
}

Comment on lines +19 to +33
// Book bookUpdated = bookDao.create(marquezBook);
// Book bookUpdated2 = bookDao.create(rowlingBook);
// System.out.println(bookUpdated);
// System.out.println(bookUpdated2);
//
// Optional<Book> bookById = bookDao.findById(3L);
// System.out.println(bookById);
//
// List<Book> allBooks = bookDao.findAll();
// for (Book element : allBooks) {
// System.out.println(element);
// }
//
// Book updateBook = bookDao.update(rowlingBook);
// System.out.println(updateBook);

Choose a reason for hiding this comment

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

?

Copy link
Author

Choose a reason for hiding this comment

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

I just forgot to uncomment

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.

2 participants