-
Notifications
You must be signed in to change notification settings - Fork 438
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
base: main
Are you sure you want to change the base?
Conversation
Book create(Book book); | ||
Optional<Book> findById(Long id); | ||
List<Book> findAll(); | ||
Book update(Book book); | ||
boolean deleteById(Long id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) { |
There was a problem hiding this comment.
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
if (affectedRows < LESS_THAN_ONE) { | ||
throw new RuntimeException(AT_LEAST_ONE_ROW_EXCEPTION); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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: "; |
There was a problem hiding this comment.
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.
if (id < LESS_THAN_ZERO) { | ||
throw new RuntimeException(NEGATIVE_ID_EXCEPTION); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)); |
There was a problem hiding this comment.
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
if (books.size() == ZERO) { | ||
throw new RuntimeException(NO_RECORDS_EXCEPTION); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (books.size() == ZERO) { | |
throw new RuntimeException(NO_RECORDS_EXCEPTION); | |
} |
Long id = resultSet.getObject("id", Long.class); | ||
String model = resultSet.getString("title"); | ||
BigDecimal price = resultSet.getObject("price", BigDecimal.class); |
There was a problem hiding this comment.
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...)
if (book.getTitle() == null || book.getPrice() == null) { | ||
throw new RuntimeException(NULL_FIELDS_EXCEPTION); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (book.getTitle() == null || book.getPrice() == null) { | |
throw new RuntimeException(NULL_FIELDS_EXCEPTION); | |
} |
…tro into jv-jdbc-intro
// 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
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
No description provided.