+ existsBook

+ getCategories
This commit is contained in:
Johannes Theiner 2019-04-30 15:53:50 +02:00
parent 027ebd2a46
commit b265adec2c
2 changed files with 82 additions and 43 deletions

View File

@ -193,6 +193,34 @@ public class Database {
return category;
}
@Nullable
public List<Category> getCategories() {
List<Long> ids = getIds(category);
List<Category> categories = new ArrayList<>();
for (long id : ids) {
categories.add(getCategory(id));
}
return categories;
}
private List<Long> getIds(String table) {
Map.Entry<ResultSet, Connection> entry = getResultSets(table, "id");
assert entry != null;
List<Long> ids = new ArrayList<>();
try {
ResultSet rs = entry.getKey();
while (rs.next()) {
ids.add(rs.getLong("id"));
}
entry.getValue().close();
} catch (SQLException ex) {
ex.printStackTrace();
}
return ids;
}
/**
* gets publisher by id.
*
@ -275,14 +303,30 @@ public class Database {
public Book getBook(long id) {
Map.Entry<ResultSet, Connection> entry = getResultSet(id, book);
assert entry != null;
return getBook(entry);
}
private Book getBook(@NotNull Map.Entry<ResultSet, Connection> entry) {
ResultSet rs = entry.getKey();
Book book = null;
try {
book = buildBook(rs);
entry.getValue().close();
} catch (SQLException e) {
e.printStackTrace();
}
return book;
}
@Nullable
private Book buildBook(ResultSet rs) {
Book book = null;
try {
Category category = getCategory(rs.getLong(this.category));
Publisher publisher = getPublisher(rs.getLong(this.publisher));
List<Integer> authorIds = getAuthors(id);
List<Integer> authorIds = getAuthors(rs.getLong("id"));
assert category != null;
assert publisher != null;
assert authorIds != null;
@ -296,7 +340,7 @@ public class Database {
book = new Book(rs.getLong("id"), rs.getLong("isbn"), authors, publisher, category, rs.getString("title"),
Year.of(rs.getInt("year")), rs.getInt("price"), rs.getString("description"),
rs.getString("image"));
entry.getValue().close();
} catch (SQLException e) {
e.printStackTrace();
}
@ -310,19 +354,7 @@ public class Database {
*/
@Nullable
public List<Book> getBooks() {
Map.Entry<ResultSet, Connection> entry = getResultSets(book, "id");
assert entry != null;
List<Long> ids = new ArrayList<>();
try {
ResultSet rs = entry.getKey();
while (rs.next()) {
ids.add(rs.getLong("id"));
}
entry.getValue().close();
} catch (SQLException ex) {
ex.printStackTrace();
}
List<Long> ids = getIds(book);
List<Book> books = new ArrayList<>();
for (long id : ids) {
books.add(getBook(id));
@ -359,6 +391,19 @@ public class Database {
return books;
}
public boolean existsBook(long isbn) {
Map.Entry<ResultSet, Connection> entry = getResultSet(book, "isbn", isbn);
try {
if (entry != null && entry.getKey() != null) {
entry.getValue().close();
return true;
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return false;
}
/**
* get books by category.
*
@ -621,11 +666,16 @@ public class Database {
*/
@Nullable
private Map.Entry<ResultSet, Connection> getResultSet(long id, String table) {
return getResultSet(table, "id", id);
}
@Nullable
private Map.Entry<ResultSet, Connection> getResultSet(String table, String column, long value) {
Connection connection = getConnection();
try {
assert connection != null;
PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + table + " WHERE id = ?");
statement.setLong(1, id);
PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + table + " WHERE " + column + " = ?");
statement.setLong(1, value);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
return new AbstractMap.SimpleEntry<>(resultSet, connection);

View File

@ -74,20 +74,9 @@ public class BookList extends HttpServlet {
String image = req.getParameter("image");
//check isbn
List<Book> books = db.getBooks();
boolean found = false;
if (books != null) {
for(Book b : books){
if(b.getIsbn() == isbn){
found = true;
break;
}
}
}
if (found) {
if (db.existsBook(isbn)) {
out.println("<aside class =\"m-note m-danger\">");
out.println("<h3>Es gibt breits ein buch mit dieser ISBN </h3>");
out.println("<h3>Es gibt bereits ein Buch mit dieser ISBN </h3>");
out.println("</aside>");
} else {
ArrayList<Author> authors = new ArrayList<>();