From d652469748420d221fa7ae2b73b36a11f7a3e4b8 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sun, 26 Jan 2025 14:44:38 +0200 Subject: [PATCH] Update to refactor the delete action. --- .../application/commands/__init__.py | 3 ++- .../commands/remove_backup_plan_command.py | 18 ++++++++++++++++++ .../repository/backup_plan_repository.py | 14 ++++---------- 3 files changed, 24 insertions(+), 11 deletions(-) create mode 100644 tui_rsync/core/components/backup_plan/application/commands/remove_backup_plan_command.py diff --git a/tui_rsync/core/components/backup_plan/application/commands/__init__.py b/tui_rsync/core/components/backup_plan/application/commands/__init__.py index f8095d5..7b568a9 100644 --- a/tui_rsync/core/components/backup_plan/application/commands/__init__.py +++ b/tui_rsync/core/components/backup_plan/application/commands/__init__.py @@ -1,4 +1,5 @@ from .remove_all_backup_plans_command import RemoveAllBackupBackupPlansCommand from .remove_backup_plan_destinations_command import RemoveBackupBackupPlanDestinationsCommand +from .remove_backup_plan_command import RemoveBackupBackupPlanCommand -__all__ = ['RemoveAllBackupBackupPlansCommand', 'RemoveBackupBackupPlanDestinationsCommand'] +__all__ = ['RemoveAllBackupBackupPlansCommand', 'RemoveBackupBackupPlanCommand', 'RemoveBackupBackupPlanDestinationsCommand'] diff --git a/tui_rsync/core/components/backup_plan/application/commands/remove_backup_plan_command.py b/tui_rsync/core/components/backup_plan/application/commands/remove_backup_plan_command.py new file mode 100644 index 0000000..baf70e1 --- /dev/null +++ b/tui_rsync/core/components/backup_plan/application/commands/remove_backup_plan_command.py @@ -0,0 +1,18 @@ +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 import CommandException +from tui_rsync.infrastructure.orm.models import DestinationModel, BackupPlanModel + + +class RemoveBackupBackupPlanCommand: + def __init__(self, database_manager: DatabaseManagerPort): + self.databaseManager = database_manager + + def execute(self, uuid: UUID) -> bool: + deleted = BackupPlanModel.delete().where(BackupPlanModel.id == uuid.id).execute() + deleted = DestinationModel.delete().where(DestinationModel.source == uuid.id).execute() | deleted + + if deleted == 0: + raise CommandException("Failed to delete the backup plan, because it doesn't exist.") + + return True 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 abb3bbe..e0b8cb1 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 @@ -1,15 +1,14 @@ from typing import Optional from tui_rsync.core.ports.orm import DatabaseManagerPort -from tui_rsync.core.shared_kernel.ports.Exceptions import CommandException from tui_rsync.infrastructure.orm.dto.dtos import BackupPlanDTO from .backup_plan_repository_port import BackupPlanRepositoryPort from tui_rsync.core.components.backup_plan.domain import BackupPlan from tui_rsync.core.shared_kernel.components.common import UUID -from tui_rsync.infrastructure.orm.models import BackupPlanModel, DestinationModel -from ..commands import RemoveBackupBackupPlanDestinationsCommand +from tui_rsync.infrastructure.orm.models import BackupPlanModel +from ..commands import RemoveBackupBackupPlanDestinationsCommand, RemoveBackupBackupPlanCommand class BackupPlanRepository(BackupPlanRepositoryPort): @@ -46,10 +45,5 @@ class BackupPlanRepository(BackupPlanRepositoryPort): destination.save(force_insert=True) def delete(self, uuid: UUID) -> bool: - deleted = BackupPlanModel.delete().where(BackupPlanModel.id == uuid.id).execute() - deleted = DestinationModel.delete().where(DestinationModel.source == uuid.id).execute() | deleted - - if deleted == 0: - raise CommandException("Failed to delete the backup plan, because it doesn't exist.") - - return True + command = RemoveBackupBackupPlanCommand(self.databaseManager) + return command.execute(uuid)