Skip to content

Commit

Permalink
Readd the ability to search just by author
Browse files Browse the repository at this point in the history
  • Loading branch information
ahobsonsayers committed Dec 27, 2024
1 parent 10dc12e commit 11577c0
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions goodreads/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,25 @@ var (
// Returns the first 10 pages of books.
// See: https://www.goodreads.com/api/index#search.books
func (c *Client) SearchBooks(ctx context.Context, title string, author *string) ([]Book, error) {
if author == nil || *author == "" {
// If author is not set, search for books by title
return c.searchBooksByTitle(ctx, title)
// Normalise title and author to make searching more consistent
normalisedTitle := normaliseString(title)
normalisedAuthor := normaliseString(lo.FromPtr(author))

switch {
case normalisedTitle != "" && normalisedAuthor != "":
// If both title and author are set
return c.searchBooksByTitleAndAuthor(ctx, title, normalisedAuthor)

case normalisedTitle != "":
// If only title is set
return c.searchBooksByTitle(ctx, normalisedTitle)

case normalisedAuthor != "":
// If only author is set
return c.searchBooksByAuthor(ctx, normalisedAuthor)
}

return c.searchBooksByTitleAndAuthor(ctx, title, *author)
return nil, nil
}

func (c *Client) searchBooksByTitle(ctx context.Context, title string) ([]Book, error) {
Expand Down

0 comments on commit 11577c0

Please sign in to comment.