azizjon-aliev / python_clean_architecture

Python Inspiration: Clean Architecture Project Pattern hon development.
MIT License
6 stars 1 forks source link

BaseAPIView for Views to create a CRUD #42

Closed Azimjonm2333 closed 1 month ago

Azimjonm2333 commented 1 month ago

Example Usage:

Now, creating a new CRUD API view is straightforward. Here's how you can define CurrencyAPIView using the newly added BaseAPIView:

from src.application.currency.commands.add_currency.add_currency_command import AddCurrencyCommand
from src.application.currency.commands.edit_currency.edit_currency_command import EditCurrencyCommand
from src.application.currency.commands.remove_currency.remove_currency_command import RemoveCurrencyCommand
from src.application.currency.queries.get_currencies_list.get_currencies_list_query import GetCurrenciesListQuery
from src.application.currency.queries.get_currency_detail.get_currency_detail_query import GetCurrencyDetailQuery
from src.domain.value_objects import CurrencyId  # CurrencyId = int
from src.presentation.rest_api.apps.currency.serializers import (
    CreateCurrencyRequestSerializer,
    DetailCurrencyResponseSerializer,
    ListCurrencyResponseSerializer,
    UpdateCurrencyRequestSerializer,
)
from src.presentation.rest_api.apps.common.base_api_view import BaseAPIView

class CurrencyAPIView(BaseAPIView[
    ListCurrencyResponseSerializer,
    DetailCurrencyResponseSerializer,
    CreateCurrencyRequestSerializer,
    UpdateCurrencyRequestSerializer,
    GetCurrenciesListQuery,
    GetCurrencyDetailQuery,
    AddCurrencyCommand,
    EditCurrencyCommand,
    RemoveCurrencyCommand,
    CurrencyId,  # PKType
]):
    authentication_classes = ()
    list_serializer = ListCurrencyResponseSerializer
    detail_serializer = DetailCurrencyResponseSerializer
    create_serializer = CreateCurrencyRequestSerializer
    update_serializer = UpdateCurrencyRequestSerializer
    list_query = GetCurrenciesListQuery
    detail_query = GetCurrencyDetailQuery
    create_command = AddCurrencyCommand
    update_command = EditCurrencyCommand
    remove_command = RemoveCurrencyCommand
    pk_type = CurrencyId  # CurrencyId = int

Benefits:


Summary:

This Pull Request enhances the API development workflow by introducing a reusable BaseAPIView and an ExtendSchemaMeta metaclass. These additions streamline the creation of CRUD endpoints, ensure consistent OpenAPI documentation, and improve overall code maintainability.