Query the Catalog
By the end of this lesson, you'll be able to:
- Write SOQL queries inside LibraryService to answer real catalog questions
- Query only available (not checked out) books
- Return query results in a form useful to a caller
Prerequisites: "Build the Core Classes"
Listing every book
public List<Book__c> getAllBooks() {
return [SELECT Id, Title__c, Author__c, Is_Checked_Out__c FROM Book__c ORDER BY Title__c];
}
A straightforward query, sorted alphabetically by title (Module 20's ORDER BY) — the simplest catalog-browsing method, and the foundation the next two build on.
Filtering to available books only
public List<Book__c> getAvailableBooks() {
return [
SELECT Id, Title__c, Author__c
FROM Book__c
WHERE Is_Checked_Out__c = false
ORDER BY Title__c
];
}
This is Module 20's WHERE clause answering a genuinely useful librarian question directly — "what can a member actually borrow right now?" — without pulling every book and filtering in Apex afterward.
Finding books by a specific author
public List<Book__c> getBooksByAuthor(String authorName) {
return [
SELECT Id, Title__c, Author__c
FROM Book__c
WHERE Author__c = :authorName
ORDER BY Title__c
];
}
:authorName is Module 20's bind variable pattern — the value passed into the method flows straight into the query safely, without any raw string concatenation.
Exercise
Add a getCheckedOutBooks() method to LibraryService querying Book__c where Is_Checked_Out__c is true.
Show hint
WHERE Is_Checked_Out__c = true
Query the Catalog Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
This lesson adds catalog-browsing methods to LibraryService, using the SOQL patterns from Module 20 to answer real questions like "which books are available right now?"