Skip to content

Commit

Permalink
Merge pull request #381 from openstax/fix_search_dupes
Browse files Browse the repository at this point in the history
Return only the latest version of each book in the books endpoint
  • Loading branch information
Dantemss authored Sep 7, 2022
2 parents d5d5048 + ab00e88 commit f6c2d76
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions app/controllers/api/v1/books_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ class BooksController < OpenStax::Api::V1::ApiController
def index
OSU::AccessPolicy.require_action_allowed! :index, current_api_user, OpenStax::Content::Book

books = abl.approved_books(archive: archive).map do |book|
books = abl.approved_books(archive: archive).group_by(&:uuid).map do |uuid, books|
latest_book = books.sort_by(&:committed_at).last

# We use book.slug.titleize here to avoid fetching all book ToCs,
# because the actual book title is not in the ABL
{ uuid: book.uuid, version: book.version, title: book.slug.titleize, slug: book.slug }
{
uuid: latest_book.uuid,
version: latest_book.version,
title: latest_book.slug.titleize,
slug: latest_book.slug
}
end

render json: books
Expand Down

0 comments on commit f6c2d76

Please sign in to comment.