Skip to content

Conversation

@NFUChen
Copy link
Contributor

@NFUChen NFUChen commented Jan 3, 2025

Description

This pull request introduces dynamic RESTful API support for PySpringModel entities, significantly simplifying CRUD operations and resource management. By incorporating a PySpringModelRestController and a PySpringModelRestService, the framework now provides an extensible mechanism to handle model-based API endpoints dynamically.

Key Changes

  1. Controller:

    • Added PySpringModelRestController, which dynamically registers CRUD routes for all models registered with the framework.
    • Supports standard operations: GET, POST, PUT, and DELETE for single and multiple records.
  2. Service:

    • Implemented PySpringModelRestService to encapsulate database operations for PySpringModel entities.
    • Handles logic for fetching, creating, updating, and deleting records.
  3. Provider Update:

    • Enhanced PySpringModelProvider to include PySpringModelRestController and PySpringModelRestService as injectable components.
  4. Dynamic Routing:

    • Routes are dynamically registered based on the available models, reducing boilerplate code and simplifying API management.

Motivation

The new REST capabilities enhance the framework's usability, allowing developers to quickly expose APIs for their models without repetitive manual configuration. This change aligns with the framework's goal of providing seamless and intuitive integrations.

Example Usage

Once a model is registered with the framework, the REST controller will automatically expose endpoints such as:

  • GET /{model_name}/{id}: Fetch a single record by ID.
  • POST /{model_name}: Create a new record.
  • PUT /{model_name}/{id}: Update an existing record.
  • DELETE /{model_name}/{id}: Delete a record by ID.

How to Test

  1. Register a PySpringModel entity in the framework.
  2. Start the application and access the generated API endpoints via the provided routes.
  3. Perform CRUD operations and ensure responses match the expected behavior.

Checklist

  • Added PySpringModelRestController for dynamic route registration.
  • Implemented PySpringModelRestService for CRUD logic.
  • Updated PySpringModelProvider to inject new components.
  • Tested the endpoints for a registered model.

Related Issues

None.

Additional Notes

This update sets the foundation for further extensions, such as advanced query support, custom validation, and role-based access control for model APIs.

- Controller: Added `PySpringModelRestController` to dynamically handle RESTful CRUD operations for registered models. - Service: Implemented `PySpringModelRestService` to manage database interactions and CRUD logic for `PySpringModel` entities. - Provider Update: Enhanced `PySpringModelProvider` to include `PySpringModelRestController` and `PySpringModelRestService` as injectable components. - Dynamic Routing: Routes are registered dynamically for all models, simplifying resource management. This addition streamlines the creation of RESTful APIs for PySpringModel entities with minimal configuration.
@NFUChen NFUChen self-assigned this Jan 3, 2025
@NFUChen NFUChen added the enhancement New feature or request label Jan 3, 2025
- Added `get_primary_key_columns` method in `PySpringModel` to retrieve primary key columns dynamically. - Updated `PySpringModelRestService` to skip primary keys during model updates. - Removed unnecessary `breakpoint` in `PySpringModelRestController.put` method.
- Introduced `CrudRepositoryImplementationService` to dynamically generate and implement query methods for `CrudRepository` inheritors. - Added logic to parse method names (e.g., `find_by`, `get_by`) and build corresponding SQL queries dynamically. - Implemented `find_by` method for flexible query execution with support for logical operators (`_and_`, `_or_`). - Created `MethodQueryBuilder` to parse method names and construct query objects (`Query`). - Updated `provide_py_spring_model` to include `CrudRepositoryImplementationService` in the component registration.
…nService - Replaced the `conditions` and `is_one_result` parameters with a `Query` object in `find_by` method. - Updated the `MetodQueryBuilder` to correctly parse method names into `raw_query_list`, `notations`, and `required_fields`. - Improved handling of logical operators (`_and_`, `_or_`) in query construction.
…y service - Updated `CrudRepositoryImplementationService` to enhance method query parsing and wrapper creation logic. - Renamed `MetodQueryBuilder` to `_MetodQueryBuilder` and `Query` to `_Query` for improved encapsulation. - Refactored `_implemenmt_query` to remove redundant checks and streamline the wrapper binding process. - Simplified `find_by` query execution with a conditional expression. - Improved validation for required fields and argument matching in dynamically generated query methods. - Enhanced logging for query execution and method binding.
- Added validation for method annotations: - Ensured required fields match method annotations in `_implemenmt_query`. - Used `copy.deepcopy` to handle annotation validation safely. - Raised an exception if annotation keys and required fields mismatch. - Improved query execution wrapper: - Checked the presence of required fields using `set` operations for accuracy. - Improved logging for query execution, including parameter details. - Enhanced `find_by` documentation: - Included a detailed algorithm for constructing filter conditions and combining them with parsed query notations. - Code cleanup: - Moved initialization and validation steps into logical order. - Removed redundant logic to streamline execution.
This commit enhances the code documentation by adding detailed docstrings to various classes and methods within the `py_spring_model` module. The added docstrings provide clear descriptions of the purpose and functionality of each class and method, making the codebase more understandable and maintainable. Changes include: - Added docstrings to `ApplicationFileGroups` and `PySpringModelProperties` in `commons.py`. - Enhanced the docstring for the `PySpringModel` class in `model.py`. - Added a comprehensive docstring to the `PySpringSession` class in `py_spring_session.py`. - Documented the `PySpringModelProvider` class in `py_spring_model_provider.py`. - Improved the docstring for `CrudRepositoryImplementationService` in `crud_repository_implementation_service.py`. - Clarified the `parse_query` method in `method_query_builder.py`. - Added a detailed docstring to `PySpringModelRestService` in `py_spring_model_rest_service.py`. These improvements aim to provide better insights into the code's functionality and usage, facilitating easier onboarding and contribution for new developers.
@NFUChen NFUChen merged commit 10fab85 into main Jan 6, 2025
@NFUChen NFUChen deleted the py-spring-data-rest branch January 7, 2025 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

3 participants