feat: show Open Library ratings on book cards #2

Merged
chris-paganon merged 4 commits from feat/open-library-ratings into master 2026-11-14 17:38:34 +00:00

Implements the "Open Library reviews" idea from docs/EXPLORATION_IDEAS.md: book cards show the Open Library star average and rating count, only when ratings exist.

Changes

  • Normalization (server/utils/open-library.ts, shared/utils/book-search.ts): ratings_average and ratings_count are added to OPEN_LIBRARY_SEARCH_FIELDS and normalized into an optional rating: { average, count } | null on BookSearchResult. A rating is set only when count ≥ 1 and the average is between 1 and 5. Malformed rating values become null (.catch(null)) and the work is kept.
  • No extra upstream requests. Ratings come with the existing /search.json calls, so search, author bibliography and import matching (which reuses the same field list) all get them. Trending recommendations pick up ratings too, if that payload includes them.
  • Persistence (library_book.rating jsonb, nullable, migration 0007): saved books store a snapshot of the rating, so the library view shows it without hitting Open Library. Re-saving a book overwrites the snapshot. Goodreads import merges keep the freshest upstream value (metadata.rating ?? existing.rating). saveBookPayloadSchema accepts an optional strict rating object.
  • UI (BookSearchCard.vue): a lucide Star (fill-primary text-primary), the average to one decimal, then the count, e.g. ★ 4.5 (168). It sits in the existing language · Published year metadata row with the same · separator. A screen-reader label and title read "Rated 4.5 out of 5 by 168 Open Library readers". Nothing renders without ratings. This card is used for search, library, author bibliography, import review and recommendation resolution, so all of them show the rating.

Decisions

  • I persisted the rating instead of showing it only on live results, so saved books don't lose it. It is a snapshot, refreshed whenever the book is saved or re-imported. There is no background refresh, to avoid extra requests.
  • The series page (/series/{id}.json) uses a different upstream payload with no rating fields, so it is unchanged.

Verification

  • curl 'https://openlibrary.org/search.json?q=the+way+of+kings&fields=key,title,ratings_average,ratings_count' returns e.g. ratings_average: 4.51, ratings_count: 168 and omits the fields for unrated works.
  • pnpm lint-full (nuxt typecheck + eslint) passes at each commit.
  • The migration was generated with drizzle-kit generate (ALTER TABLE "library_book" ADD COLUMN "rating" jsonb;).
  • I have not checked the UI in a browser; the dev server was not started, as instructed.
Implements the "Open Library reviews" idea from `docs/EXPLORATION_IDEAS.md`: book cards show the Open Library star average and rating count, only when ratings exist. ## Changes - **Normalization** (`server/utils/open-library.ts`, `shared/utils/book-search.ts`): `ratings_average` and `ratings_count` are added to `OPEN_LIBRARY_SEARCH_FIELDS` and normalized into an optional `rating: { average, count } | null` on `BookSearchResult`. A rating is set only when count ≥ 1 and the average is between 1 and 5. Malformed rating values become `null` (`.catch(null)`) and the work is kept. - **No extra upstream requests.** Ratings come with the existing `/search.json` calls, so search, author bibliography and import matching (which reuses the same field list) all get them. Trending recommendations pick up ratings too, if that payload includes them. - **Persistence** (`library_book.rating` jsonb, nullable, migration `0007`): saved books store a snapshot of the rating, so the library view shows it without hitting Open Library. Re-saving a book overwrites the snapshot. Goodreads import merges keep the freshest upstream value (`metadata.rating ?? existing.rating`). `saveBookPayloadSchema` accepts an optional strict `rating` object. - **UI** (`BookSearchCard.vue`): a lucide `Star` (`fill-primary text-primary`), the average to one decimal, then the count, e.g. `★ 4.5 (168)`. It sits in the existing `language · Published year` metadata row with the same `·` separator. A screen-reader label and title read "Rated 4.5 out of 5 by 168 Open Library readers". Nothing renders without ratings. This card is used for search, library, author bibliography, import review and recommendation resolution, so all of them show the rating. ## Decisions - I persisted the rating instead of showing it only on live results, so saved books don't lose it. It is a snapshot, refreshed whenever the book is saved or re-imported. There is no background refresh, to avoid extra requests. - The series page (`/series/{id}.json`) uses a different upstream payload with no rating fields, so it is unchanged. ## Verification - `curl 'https://openlibrary.org/search.json?q=the+way+of+kings&fields=key,title,ratings_average,ratings_count'` returns e.g. `ratings_average: 4.51, ratings_count: 168` and omits the fields for unrated works. - `pnpm lint-full` (nuxt typecheck + eslint) passes at each commit. - The migration was generated with `drizzle-kit generate` (`ALTER TABLE "library_book" ADD COLUMN "rating" jsonb;`). - I have not checked the UI in a browser; the dev server was not started, as instructed.
Author
Owner

Reviewed master...ccb5104 (all 3 commits), including search normalization, save/import paths, card rendering, migration and request reuse, following the repository's full-code-review skill and project direction.

Important — 0

No important findings or must-fix issues before merge identified. Ratings piggyback on existing /search.json requests; no additional upstream calls are introduced. Saved cards use the persisted snapshot.

Nit picks — 1

Optional, not a merge blocker: avoid rounding the average twice. server/utils/open-library.ts:40 rounds the upstream average to two decimals before app/components/book/BookSearchCard.vue:13 formats it to one decimal. A valid upstream average of 4.9496 becomes 4.95, then displays as 5.0; formatting the original average gives 4.9. Preserve the original average in normalization and round only for display to avoid overstating ratings around rounding boundaries. Confirmed with a direct JavaScript evaluation.

Pre-existing — 0

No pre-existing findings raised in this review.

Security — 0

No security findings identified. The new payload fields are numerically validated, writes remain scoped to the authenticated user's library, and rating text uses Vue's escaped interpolation.

Validation: pnpm lint-full passed (typecheck and ESLint; Node emitted a deprecation warning). Migration snapshot matches the preceding snapshot apart from its identifiers and the intended nullable rating column. No repository files changed, no tests written, and no dev server or Docker started. Browser rendering and applying the migration to a database were not exercised.

Reviewed `master...ccb5104` (all 3 commits), including search normalization, save/import paths, card rendering, migration and request reuse, following the repository's full-code-review skill and project direction. ### Important — 0 No important findings or must-fix issues before merge identified. Ratings piggyback on existing `/search.json` requests; no additional upstream calls are introduced. Saved cards use the persisted snapshot. ### Nit picks — 1 **Optional, not a merge blocker: avoid rounding the average twice.** [server/utils/open-library.ts:40](https://forgejo.chrispaganon.com/chris-paganon/chrisp-books/src/commit/ccb5104/server/utils/open-library.ts#L40) rounds the upstream average to two decimals before [app/components/book/BookSearchCard.vue:13](https://forgejo.chrispaganon.com/chris-paganon/chrisp-books/src/commit/ccb5104/app/components/book/BookSearchCard.vue#L13) formats it to one decimal. A valid upstream average of `4.9496` becomes `4.95`, then displays as `5.0`; formatting the original average gives `4.9`. Preserve the original average in normalization and round only for display to avoid overstating ratings around rounding boundaries. Confirmed with a direct JavaScript evaluation. ### Pre-existing — 0 No pre-existing findings raised in this review. ### Security — 0 No security findings identified. The new payload fields are numerically validated, writes remain scoped to the authenticated user's library, and rating text uses Vue's escaped interpolation. Validation: `pnpm lint-full` passed (typecheck and ESLint; Node emitted a deprecation warning). Migration snapshot matches the preceding snapshot apart from its identifiers and the intended nullable `rating` column. No repository files changed, no tests written, and no dev server or Docker started. Browser rendering and applying the migration to a database were not exercised.
Author
Owner

Fixed the double-rounding nit in d2b6357: normalization now keeps the raw upstream average and only the card rounds it (toFixed(1)), so 4.9496 displays as 4.9. lint-full passes.

Fixed the double-rounding nit in d2b6357: normalization now keeps the raw upstream average and only the card rounds it (toFixed(1)), so 4.9496 displays as 4.9. lint-full passes.
chris-paganon deleted branch feat/open-library-ratings 2026-11-14 17:38:34 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
chris-paganon/chrisp-books!2
No description provided.