From 72f650fd649b9588cc163fddda48d4c8c2103bfc Mon Sep 17 00:00:00 2001 From: KKlochko Date: Tue, 7 Jan 2025 21:52:06 +0200 Subject: [PATCH] Add the DTOs to convert the entities to theorm models. --- tui_rsync/infrastructure/orm/dto/__init__.py | 3 ++ tui_rsync/infrastructure/orm/dto/dtos.py | 34 ++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 tui_rsync/infrastructure/orm/dto/__init__.py create mode 100644 tui_rsync/infrastructure/orm/dto/dtos.py diff --git a/tui_rsync/infrastructure/orm/dto/__init__.py b/tui_rsync/infrastructure/orm/dto/__init__.py new file mode 100644 index 0000000..a8ade53 --- /dev/null +++ b/tui_rsync/infrastructure/orm/dto/__init__.py @@ -0,0 +1,3 @@ +from .dtos import BackupPlanDTO + +__all__ = ['BackupPlanDTO'] diff --git a/tui_rsync/infrastructure/orm/dto/dtos.py b/tui_rsync/infrastructure/orm/dto/dtos.py new file mode 100644 index 0000000..3644413 --- /dev/null +++ b/tui_rsync/infrastructure/orm/dto/dtos.py @@ -0,0 +1,34 @@ +from peewee import * + +from tui_rsync.core.components.backup_plan.domain import BackupPlan, Destination, Source, BackupPlanId +from tui_rsync.infrastructure.orm.models import BackupPlanModel, DestinationModel + + +class BackupPlanDTO: + @staticmethod + def to_domain(model: BackupPlanModel) -> BackupPlan: + return BackupPlan( + id=BackupPlanId(model.id), + label=model.label, + source=Source(model.source), + destinations=list(map(lambda destination: Destination(destination.path), model.destinations)), + ) + + @staticmethod + def to_model(entity: BackupPlan) -> BackupPlanModel: + model = BackupPlanModel( + id=entity.id.id, + label=entity.label, + source=entity.source.path, + destinations=[], + ) + + for destination in entity.destinations: + model.destinations.append( + DestinationModel( + source=model, + path=destination.path + ) + ) + + return model