From 0abefda95a5de09f41e6da7b9d53b18fa3b2c682 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Tue, 7 Jan 2025 21:57:06 +0200 Subject: [PATCH] Add the repository for the backup plan. Only add, get_by_id and delete are implemented. --- .../application/repository/__init__.py | 0 .../repository/backup_plan_repository.py | 37 +++++++++++++++++++ .../repository/backup_plan_repository_port.py | 23 ++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 tui_rsync/core/components/backup_plan/application/repository/__init__.py create mode 100644 tui_rsync/core/components/backup_plan/application/repository/backup_plan_repository.py create mode 100644 tui_rsync/core/components/backup_plan/application/repository/backup_plan_repository_port.py diff --git a/tui_rsync/core/components/backup_plan/application/repository/__init__.py b/tui_rsync/core/components/backup_plan/application/repository/__init__.py new file mode 100644 index 0000000..e69de29 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 new file mode 100644 index 0000000..9e2c5d0 --- /dev/null +++ b/tui_rsync/core/components/backup_plan/application/repository/backup_plan_repository.py @@ -0,0 +1,37 @@ +from typing import Optional + +from tui_rsync.core.ports.orm import DatabaseManagerPort +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 + + +class BackupPlanRepository(BackupPlanRepositoryPort): + def __init__(self, database_manager: DatabaseManagerPort): + self.databaseManager = database_manager + + def add(self, backup_plan: BackupPlan): + model = BackupPlanDTO.to_model(backup_plan) + + model.save(force_insert=True) + 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 update(self, backup_plan: BackupPlan): + pass + + def delete(self, uuid: UUID) -> bool: + deleted = BackupPlanModel.delete().where(BackupPlanModel.id == uuid.id).execute() + return bool(deleted) diff --git a/tui_rsync/core/components/backup_plan/application/repository/backup_plan_repository_port.py b/tui_rsync/core/components/backup_plan/application/repository/backup_plan_repository_port.py new file mode 100644 index 0000000..112e3aa --- /dev/null +++ b/tui_rsync/core/components/backup_plan/application/repository/backup_plan_repository_port.py @@ -0,0 +1,23 @@ +from abc import ABC, abstractmethod +from typing import Optional + +from tui_rsync.core.components.backup_plan.domain import BackupPlan +from tui_rsync.core.shared_kernel.components.common import UUID + + +class BackupPlanRepositoryPort(ABC): + @abstractmethod + def add(self, backup_plan: BackupPlan): + pass + + @abstractmethod + def get_by_id(self, uuid: UUID) -> Optional[BackupPlan]: + pass + + @abstractmethod + def update(self, backup_plan: BackupPlan): + pass + + @abstractmethod + def delete(self, uuid: UUID) -> bool: + pass