Skip to content

Conversation

@codesungrape
Copy link
Collaborator

Description

Trello ticket: https://trello.com/c/O9jraVyM
This is the first of four planned PRs to migrate the API from an in-memory datastore to MongoDB. This PR specifically covers the GET /books endpoint.

  • GET /books <-- this PR is related to this
  • GET /books/id
  • PUT /books/id
  • DELETE /books/id

This change finalizes the transition of the GET /books endpoint to a full MongoDB-backed implementation. It includes a comprehensive test suite to achieve 100% test coverage for the new logic.

Key changes include:

  • Service Layer: Replaced the in-memory data store with a service layer that calls the database via find_books.
  • Data Formatting: Implemented format_books_for_api to handle _id to id conversion, remove internal fields (state), and build absolute URLs for HATEOAS links.
  • Error Handling: Added graceful error handling for database connection failures, now returning a specific 503 Service Unavailable instead of a generic 500.
  • Testing Overhaul: Tests now mocks the low-level find_books fro assertions and added pure unit tests for the find_books helper function itself.
  • Doc update Updated openapi.yml to reflect 503 error

Type of change

Please delete options that are not relevant.

  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update
  • Code refactor (improving code quality without changing functionality)

How Has This Been Tested?

Automated testing (pytest), manual testing (cURL), CI/CD with 100% pylint error coverage and 100% test coverage

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • My individual commit messages are descriptive and follow our commit guidelines
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR completes the MongoDB integration for the GET /books endpoint, transitioning from an in-memory datastore to a full database-backed implementation. The change introduces proper service layer architecture, comprehensive error handling, and maintains 100% test coverage.

Key changes include:

  • Service Layer Introduction: New book_service.py module with fetch_active_books() and format_books_for_api() functions to handle database operations and data formatting
  • Database Integration: Added find_books() helper function in mongo_helper.py to wrap MongoDB query operations with filtering, projection, and limit capabilities
  • Enhanced Error Handling: Improved database connection error handling with specific 503 Service Unavailable responses instead of generic 500 errors

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
app/services/book_service.py New service layer module containing business logic for fetching and formatting books from MongoDB
app/datastore/mongo_helper.py Added find_books() helper function to wrap MongoDB find operations with optional filtering and limiting
app/routes.py Refactored GET /books endpoint to use new service layer and improved error handling for database failures
tests/test_mongo_helper.py Added comprehensive unit tests for the new find_books() helper function
tests/test_app.py Updated integration tests to mock the new service layer and test database error scenarios
openapi.yml Added 503 Service Unavailable response specification for database connection failures
Comments suppressed due to low confidence (1)

app/services/book_service.py:45

  • [nitpick] The variable name 'msg_lines_list' is redundant since 'list' is already implied by the type. Consider renaming to 'msg_lines' or 'message_lines'.
        msg_lines_list = ["Missing required fields:"]

codesungrape and others added 2 commits July 24, 2025 15:24
Fix docstring to reflect parameter name, copilot review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot review- fix spelling error

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link

@Lakorthus Lakorthus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 👍

@codesungrape codesungrape merged commit 1f8c0e4 into main Jul 25, 2025
2 checks passed
@codesungrape codesungrape deleted the Complete-MongoDB-Integration-for-Books-Resource-(GET) branch July 25, 2025 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants