diff --git a/tui_rsync/core/components/backup_plan/application/queries/__init__.py b/tui_rsync/core/components/backup_plan/application/queries/__init__.py index aa5c7b0..c822363 100644 --- a/tui_rsync/core/components/backup_plan/application/queries/__init__.py +++ b/tui_rsync/core/components/backup_plan/application/queries/__init__.py @@ -1,3 +1,4 @@ from .get_all_backup_plans_query import GetAllBackupBackupPlansQuery +from .get_backup_plan_by_id_query import GetBackupBackupPlanByIdQuery -__all__ = ['GetAllBackupBackupPlansQuery'] +__all__ = ['GetAllBackupBackupPlansQuery', 'GetBackupBackupPlanByIdQuery'] diff --git a/tui_rsync/core/components/backup_plan/application/queries/get_backup_plan_by_id_query.py b/tui_rsync/core/components/backup_plan/application/queries/get_backup_plan_by_id_query.py new file mode 100644 index 0000000..d452a39 --- /dev/null +++ b/tui_rsync/core/components/backup_plan/application/queries/get_backup_plan_by_id_query.py @@ -0,0 +1,20 @@ +from tui_rsync.core.components.backup_plan.domain import BackupPlan +from tui_rsync.core.ports.orm import DatabaseManagerPort +from tui_rsync.core.shared_kernel.components.common import UUID +from tui_rsync.core.shared_kernel.ports.Exceptions.query_exception import QueryException +from tui_rsync.infrastructure.orm.dto.dtos import BackupPlanDTO + +from tui_rsync.infrastructure.orm.models import BackupPlanModel + + +class GetBackupBackupPlanByIdQuery: + def __init__(self, database_manager: DatabaseManagerPort): + self.databaseManager = database_manager + + def execute(self, uuid: UUID) -> BackupPlan: + model = BackupPlanModel.get_or_none(BackupPlanModel.id == uuid.id) + + if model is None: + raise QueryException("The backup plan was not found.") + + return BackupPlanDTO.to_domain(model) diff --git a/tui_rsync/core/components/backup_plan/application/repository/backup_plan_repository.py b/tui_rsync/core/components/backup_plan/application/repository/backup_plan_repository.py index e0b8cb1..7ac54b5 100644 --- a/tui_rsync/core/components/backup_plan/application/repository/backup_plan_repository.py +++ b/tui_rsync/core/components/backup_plan/application/repository/backup_plan_repository.py @@ -9,6 +9,7 @@ from tui_rsync.core.shared_kernel.components.common import UUID from tui_rsync.infrastructure.orm.models import BackupPlanModel from ..commands import RemoveBackupBackupPlanDestinationsCommand, RemoveBackupBackupPlanCommand +from ..queries import GetBackupBackupPlanByIdQuery class BackupPlanRepository(BackupPlanRepositoryPort): @@ -22,13 +23,9 @@ class BackupPlanRepository(BackupPlanRepositoryPort): for destination in model.destinations: destination.save(force_insert=True) - def get_by_id(self, uuid: UUID) -> Optional[BackupPlan]: - model = BackupPlanModel.get_or_none(BackupPlanModel.id == uuid.id) - - if model is None: - return model - - return BackupPlanDTO.to_domain(model) + def get_by_id(self, uuid: UUID) -> BackupPlan: + query = GetBackupBackupPlanByIdQuery(self.databaseManager) + return query.execute(uuid) def update(self, backup_plan: BackupPlan): updated_model = BackupPlanDTO.to_model(backup_plan) diff --git a/tui_rsync/core/shared_kernel/ports/Exceptions/query_exception.py b/tui_rsync/core/shared_kernel/ports/Exceptions/query_exception.py new file mode 100644 index 0000000..d0ed3de --- /dev/null +++ b/tui_rsync/core/shared_kernel/ports/Exceptions/query_exception.py @@ -0,0 +1,6 @@ +from . import AppException + + +class QueryException(AppException): + """Query failed.""" + pass diff --git a/tui_rsync/user_interface/cli/components/backup_plan/show_backup_plan.py b/tui_rsync/user_interface/cli/components/backup_plan/show_backup_plan.py index 6f3ede4..e322790 100644 --- a/tui_rsync/user_interface/cli/components/backup_plan/show_backup_plan.py +++ b/tui_rsync/user_interface/cli/components/backup_plan/show_backup_plan.py @@ -24,14 +24,17 @@ from tui_rsync.core.components.backup_plan.application.queries import GetAllBack from tui_rsync.core.components.backup_plan.application.services.backup_plan_service import BackupPlanService from tui_rsync.core.shared_kernel.components.common import UUID +from tui_rsync.core.shared_kernel.ports.Exceptions.query_exception import QueryException from tui_rsync.infrastructure.configuration import CurrentConfiguration from tui_rsync.user_interface.cli.components.backup_plan.formating import BackupPlanFormat +from tui_rsync.user_interface.cli.shared_kernel.components.errors.applications.exceptions import catch_exception console = Console() show_backup_plan = typer.Typer() @show_backup_plan.command() +@catch_exception(QueryException, 1) def one( id: str = typer.Option( None, "--id", "-i",