use of com.example.getstarted.objects.Book in project getting-started-java by GoogleCloudPlatform.
the class FirestoreDao method listBooks.
// [END bookshelf_firestore_documents_to_books]
// [START bookshelf_firestore_list_books]
@Override
public Result<Book> listBooks(String startTitle) {
Query booksQuery = booksCollection.orderBy("title").limit(10);
if (startTitle != null) {
booksQuery = booksQuery.startAfter(startTitle);
}
try {
QuerySnapshot snapshot = booksQuery.get().get();
List<Book> results = documentsToBooks(snapshot.getDocuments());
String newCursor = null;
if (results.size() > 0) {
newCursor = results.get(results.size() - 1).getTitle();
}
return new Result<>(results, newCursor);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return new Result<>(Lists.newArrayList(), null);
}
use of com.example.getstarted.objects.Book in project getting-started-java by GoogleCloudPlatform.
the class ListByUserServlet method doGet.
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
BookDao dao = (BookDao) this.getServletContext().getAttribute("dao");
String startCursor = req.getParameter("cursor");
List<Book> books = null;
String endCursor = null;
try {
Result<Book> result = dao.listBooksByUser((String) req.getSession().getAttribute("userId"), startCursor);
books = result.result;
endCursor = result.cursor;
} catch (Exception e) {
throw new ServletException("Error listing books", e);
}
req.getSession().getServletContext().setAttribute("books", books);
StringBuilder bookNames = new StringBuilder();
for (Book book : books) {
bookNames.append(book.getTitle() + " ");
}
logger.log(Level.INFO, "Loaded books: " + bookNames.toString() + " for user " + (String) req.getSession().getAttribute("userId"));
req.getSession().setAttribute("cursor", endCursor);
req.getSession().setAttribute("page", "list");
req.getRequestDispatcher("/base.jsp").forward(req, resp);
}
Aggregations