Implement quote management features: delete, get by ID, and update#6
Implement quote management features: delete, get by ID, and update#6WillianSilva51 merged 3 commits intomainfrom
Conversation
…vice and repository methods
…schema validation
There was a problem hiding this comment.
Pull request overview
This PR extends the quotes API to support retrieving and deleting quotes by ID, while tightening request/response validation and updating caching/serialization to handle non-list responses.
Changes:
- Added
GET /v1/quotes/{id}andDELETE /v1/quotes/{id}endpoints plus corresponding service/repository methods. - Introduced
UpdateQuoteRequestand expanded quote schemas/models to includeupdated_at, with stricter field stripping/empty checks. - Updated Redis caching to serialize both list and single-model responses; expanded service-layer tests for get/delete-by-id flows.
Reviewed changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| api/routers/quotes.py | Adds get/delete-by-id endpoints and standardizes responses to QuoteResponse. |
| api/services/quote_service.py | Implements service logic for get/delete-by-id and not-found behavior. |
| api/repositories/quote_repository.py | Adds repository methods for get/delete-by-id; scaffolds (but does not implement) update. |
| api/schemas/quote_schema.py | Adds UpdateQuoteRequest, field validators, and updated_at on response. |
| api/models/quote.py | Adds updated_at field to the Quote document model. |
| api/core/cache.py | Improves caching serialization to support non-list return types. |
| api/tests/test_quote_service.py | Adds tests for get/delete-by-id and today quote behavior; includes a placeholder update test. |
| api/tests/conftest.py | Minor formatting change. |
| api/schemas/pagination.py | Import formatting only. |
| api/schemas/error_schema.py | Import formatting only. |
| api/main.py | Import ordering only. |
| api/core/security.py | Import ordering only. |
| api/core/database.py | Import ordering only. |
| api/FinFrases/Quotes/Get Quote By ID.yml | Adds HTTP client definition for GET by ID. |
| api/FinFrases/Quotes/Delete Quote By ID.yml | Adds HTTP client definition for DELETE by ID. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| async def update_quote( | ||
| self, id: str, quote_data: UpdateQuoteRequest | ||
| ) -> Quote | None: | ||
| pass |
There was a problem hiding this comment.
update_quote() is currently a stub (pass) but the repository exports it and the PR title/description mention update support. This will silently do nothing if invoked and makes the update feature incomplete; implement the update logic (including setting updated_at) or remove the method and any related schemas/tests until it’s supported.
|
|
||
|
|
||
| class TestUpdateQuoteById: | ||
| @pytest.mark.asyncio | ||
| async def test_update_quote_by_id_sucess(self, service, mock_repo, quote): | ||
| pass |
There was a problem hiding this comment.
This test currently contains only pass and will always succeed, which can mask missing update functionality. Either implement the update-by-id behavior assertions (and corresponding service/repo functionality) or remove this placeholder test; also fix the typo in the test name (sucess -> success).
| class TestUpdateQuoteById: | |
| @pytest.mark.asyncio | |
| async def test_update_quote_by_id_sucess(self, service, mock_repo, quote): | |
| pass |
| async def get_quote_by_id( | ||
| id: str, | ||
| service: QuoteService = Depends(), | ||
| repo: QuoteRepository = Depends(), | ||
| ) -> QuoteResponse: | ||
| quote = await service.get_quote_by_id(id=id, repo=repo) | ||
| return QuoteResponse.model_validate(quote.model_dump()) |
There was a problem hiding this comment.
id is typed as str here, but the repository converts it to PydanticObjectId without handling invalid values. A non-ObjectId string will raise and be caught by the global exception handler as a 500; prefer validating at the API boundary (e.g., type id as PydanticObjectId or explicitly validate/convert and raise a 422/400) to avoid internal-server errors for bad input.
| async def delete_quote( | ||
| id: str, | ||
| service: QuoteService = Depends(), | ||
| repo: QuoteRepository = Depends(), | ||
| _: str = Depends(verify_api_key), | ||
| ) -> None: | ||
| await service.delete_quote_by_id(id=id, repo=repo) |
There was a problem hiding this comment.
Same as the GET endpoint: id is accepted as str but converted to PydanticObjectId later without input validation, so malformed IDs can trigger a 500. Validate/convert id in the router layer (e.g., use PydanticObjectId as the param type) so invalid IDs return a 422/400 instead of an internal error.
| updated_at: datetime = Field( | ||
| default_factory=lambda: datetime.now(timezone.utc), | ||
| description="A data e hora da última atualização da citação", | ||
| ) | ||
|
|
There was a problem hiding this comment.
UpdateQuoteRequest includes an updated_at field with a default value. For an update request payload this allows clients to set a server-managed auditing field (and may conflict with the value the server should enforce). Consider removing updated_at from the request schema and setting it in the service/repository when persisting the update.
| updated_at: datetime = Field( | |
| default_factory=lambda: datetime.now(timezone.utc), | |
| description="A data e hora da última atualização da citação", | |
| ) |
This pull request introduces significant enhancements to the quote management API, focusing on adding support for retrieving and deleting quotes by ID, improving schema validation, and expanding test coverage. The changes include new endpoints, repository and service methods, stricter request validation, and comprehensive tests for the new functionalities.
API Enhancements:
GET /{id}) and delete (DELETE /{id}) quotes by their unique ID inapi/routers/quotes.py, with corresponding OpenAPI YAML definitions for both actions. [1] [2] [3] [4]QuoteResponse, ensuring proper serialization and validation of API responses. [1] [2] [3] [4] [5]Repository and Service Layer Improvements:
get_quote_by_idanddelete_quote_by_idmethods inQuoteRepository, and corresponding logic inQuoteServiceto handle not-found cases and logging. [1] [2]update_quotemethod in the repository (not yet implemented).Schema and Model Validation:
UpdateQuoteRequestschema and added stricter validation forcontent,author, andsourcefields usingfield_validatorinapi/schemas/quote_schema.py.updated_atfield to both the model and response schema to track quote modifications. [1] [2]Testing Improvements:
test_quote_service.pyto cover getting and deleting quotes by ID, as well as edge cases like not-found errors, ensuring robust error handling and correct repository/service interactions.General Code Quality and Refactoring:
These changes collectively improve the API's functionality, reliability, and maintainability, especially around quote retrieval, deletion, and validation.