-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bookDao methods implemented and tested
- Loading branch information
1 parent
bc1d800
commit 3b22a03
Showing
9 changed files
with
276 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package mate.academy.lib.dao; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import mate.academy.lib.model.Book; | ||
|
||
public interface BookDao { | ||
Book create(Book book); | ||
|
||
Optional<Book> findById(Long id); | ||
|
||
List<Book> findAll(); | ||
|
||
Book update(Book book); | ||
|
||
boolean deleteById(Long id); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package mate.academy.lib.dao; | ||
|
||
import java.math.BigDecimal; | ||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import mate.academy.lib.Dao; | ||
import mate.academy.lib.exception.DataProcessingException; | ||
import mate.academy.lib.model.Book; | ||
import mate.academy.lib.service.ConnectionUtil; | ||
|
||
@Dao | ||
public class BookDaoImpl implements BookDao { | ||
private static final String ID = "id"; | ||
private static final String TITLE = "title"; | ||
private static final String PRICE = "price"; | ||
|
||
@Override | ||
public Book create(Book book) { | ||
String sql = "INSERT INTO books (title, price) VALUES (?, ?)"; | ||
try (Connection connection = ConnectionUtil.getConnection(); | ||
PreparedStatement statement = connection.prepareStatement(sql, | ||
Statement.RETURN_GENERATED_KEYS)) { | ||
statement.setString(1, book.getTitle()); | ||
statement.setBigDecimal(2, book.getPrice()); | ||
|
||
int affectedRows = statement.executeUpdate(); | ||
if (affectedRows < 1) { | ||
throw new DataProcessingException("Expected to insert at least 1 row," | ||
+ " inserted 0 rows", null); | ||
} | ||
|
||
ResultSet generatedKeys = statement.getGeneratedKeys(); | ||
if (generatedKeys.next()) { | ||
long id = generatedKeys.getObject(1, Long.class); | ||
book.setId(id); | ||
} | ||
} catch (SQLException e) { | ||
throw new DataProcessingException("Failed to add new book: " + book, e); | ||
} | ||
return book; | ||
} | ||
|
||
@Override | ||
public Optional<Book> findById(Long id) { | ||
String sql = "SELECT * FROM books WHERE id = ?"; | ||
try (Connection connection = ConnectionUtil.getConnection(); | ||
PreparedStatement statement = connection.prepareStatement(sql)) { | ||
statement.setLong(1, id); | ||
try (ResultSet resultSet = statement.executeQuery()) { | ||
if (resultSet.next()) { | ||
long bookId = resultSet.getObject(ID, Long.class); | ||
String title = resultSet.getString(TITLE); | ||
BigDecimal price = resultSet.getBigDecimal(PRICE); | ||
Book book = new Book(bookId, title, price); | ||
return Optional.of(book); | ||
} else { | ||
return Optional.empty(); | ||
} | ||
} | ||
} catch (SQLException e) { | ||
throw new DataProcessingException("Failed to find a book with id: " + id, e); | ||
} | ||
} | ||
|
||
@Override | ||
public List<Book> findAll() { | ||
String sql = "SELECT * FROM books"; | ||
List<Book> books = new ArrayList<>(); | ||
try (Connection connection = ConnectionUtil.getConnection(); | ||
PreparedStatement statement = connection.prepareStatement(sql)) { | ||
ResultSet resultSet = statement.executeQuery(); | ||
|
||
while (resultSet.next()) { | ||
long id = resultSet.getLong(ID); | ||
String title = resultSet.getString(TITLE); | ||
BigDecimal price = resultSet.getBigDecimal(PRICE); | ||
Book book = new Book(id, title, price); | ||
books.add(book); | ||
} | ||
} catch (SQLException e) { | ||
throw new DataProcessingException("Failed to retrieve books from the database", e); | ||
} | ||
return books; | ||
} | ||
|
||
@Override | ||
public Book update(Book book) { | ||
String sql = "UPDATE books SET title = ?, price = ? WHERE id = ?"; | ||
try (Connection connection = ConnectionUtil.getConnection(); | ||
PreparedStatement statement = connection.prepareStatement(sql)) { | ||
statement.setString(1, book.getTitle()); | ||
statement.setBigDecimal(2, book.getPrice()); | ||
statement.setLong(3, book.getId()); | ||
int affectedRows = statement.executeUpdate(); | ||
if (affectedRows < 1) { | ||
throw new DataProcessingException("Expected to insert at least 1 row," | ||
+ " inserted 0 rows", null); | ||
} | ||
} catch (SQLException e) { | ||
throw new DataProcessingException("Failed to add new book: " + book, e); | ||
} | ||
return book; | ||
} | ||
|
||
@Override | ||
public boolean deleteById(Long id) { | ||
String sql = "DELETE FROM books WHERE id = ?"; | ||
try (Connection connection = ConnectionUtil.getConnection(); | ||
PreparedStatement statement = connection.prepareStatement(sql)) { | ||
statement.setLong(1, id); | ||
|
||
int affectedRows = statement.executeUpdate(); | ||
if (affectedRows < 1) { | ||
return false; | ||
} | ||
} catch (SQLException e) { | ||
throw new DataProcessingException("Failed to delete a book with id: " + id, e); | ||
} | ||
return true; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/mate/academy/lib/exception/DataProcessingException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package mate.academy.lib.exception; | ||
|
||
public class DataProcessingException extends RuntimeException { | ||
public DataProcessingException(String message, Throwable ex) { | ||
super(message, ex); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package mate.academy.lib.model; | ||
|
||
import java.math.BigDecimal; | ||
|
||
public class Book { | ||
private Long id; | ||
private String title; | ||
private BigDecimal price; | ||
|
||
public Book() { | ||
} | ||
|
||
public Book(String title, BigDecimal price) { | ||
this.title = title; | ||
this.price = price; | ||
} | ||
|
||
public Book(Long id, String title, BigDecimal price) { | ||
this.id = id; | ||
this.title = title; | ||
this.price = price; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public BigDecimal getPrice() { | ||
return price; | ||
} | ||
|
||
public void setPrice(BigDecimal price) { | ||
this.price = price; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Book{" | ||
+ "id=" + id | ||
+ ", title='" + title + '\'' | ||
+ ", price=" + price | ||
+ '}'; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/mate/academy/lib/service/ConnectionUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package mate.academy.lib.service; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.SQLException; | ||
import java.util.Properties; | ||
|
||
public class ConnectionUtil { | ||
private static final String DB_URL = "jdbc:mysql://localhost:3306/mate"; | ||
private static final String DB_DRIVER = "com.mysql.cj.jdbc.Driver"; | ||
private static final String DB_USERNAME = "root"; | ||
private static final String DB_PASSWORD = "MySQL1914#"; | ||
private static final Properties DB_PROPERTIES; | ||
|
||
static { | ||
DB_PROPERTIES = new Properties(); | ||
DB_PROPERTIES.put("user", DB_USERNAME); | ||
DB_PROPERTIES.put("password", DB_PASSWORD); | ||
|
||
try { | ||
Class.forName(DB_DRIVER); | ||
} catch (ClassNotFoundException e) { | ||
throw new RuntimeException("Can't load JDBC driver : " + DB_DRIVER, e); | ||
} | ||
} | ||
|
||
public static Connection getConnection() throws SQLException { | ||
return DriverManager.getConnection(DB_URL, DB_PROPERTIES); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package mate.academy.lib.service; | ||
|
||
import java.math.BigDecimal; | ||
import mate.academy.lib.Injector; | ||
import mate.academy.lib.dao.BookDao; | ||
import mate.academy.lib.model.Book; | ||
|
||
public class Main { | ||
private static final Injector injector = Injector.getInstance("mate.academy.lib"); | ||
|
||
public static void main(String[] args) { | ||
BookDao bookDao = (BookDao) injector.getInstance(BookDao.class); | ||
|
||
Book airport = new Book("Airport", BigDecimal.valueOf(29.00)); | ||
bookDao.create(airport); | ||
System.out.println(airport); | ||
|
||
System.out.println(bookDao.findById(2L)); | ||
|
||
airport.setTitle("Waterport"); | ||
System.out.println(bookDao.update(airport)); | ||
|
||
bookDao.deleteById(1L); | ||
|
||
System.out.println(bookDao.findAll()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
CREATE TABLE `books` ( | ||
`id` BIGINT AUO_INCREMENT PRIMARY KEY, | ||
`title` VARCHAR(255), | ||
`price` DECIMAL(10, 2) | ||
); |