Update the show all plans command to show if there're no plans.

This commit is contained in:
2025-01-27 12:06:58 +02:00
parent b999975695
commit 9729f442c2
15 changed files with 157 additions and 40 deletions
@@ -1,4 +1,5 @@
from .get_all_backup_plans_query import GetAllBackupBackupPlansQuery
from .get_backup_plan_by_id_query import GetBackupBackupPlanByIdQuery
from .get_all_backup_plans_query import GetAllBackupPlansQuery
from .get_backup_plan_by_id_query import GetBackupPlanByIdQuery
from .get_backup_plan_count_query import GetBackupPlanCountQuery
__all__ = ['GetAllBackupBackupPlansQuery', 'GetBackupBackupPlanByIdQuery']
__all__ = ['GetAllBackupPlansQuery', 'GetBackupPlanByIdQuery', 'GetBackupPlanCountQuery']
@@ -0,0 +1,12 @@
from tui_rsync.core.ports.orm import DatabaseManagerPort
from tui_rsync.infrastructure.orm.dto.dtos import BackupPlanDTO
from tui_rsync.infrastructure.orm.models import BackupPlanModel
class GetAllBackupBackupPlansQuery:
def __init__(self, database_manager: DatabaseManagerPort):
self.databaseManager = database_manager
def execute(self):
return (BackupPlanDTO.to_domain(model) for model in BackupPlanModel.select().iterator())
@@ -4,7 +4,7 @@ from tui_rsync.infrastructure.orm.dto.dtos import BackupPlanDTO
from tui_rsync.infrastructure.orm.models import BackupPlanModel
class GetAllBackupBackupPlansQuery:
class GetAllBackupPlansQuery:
def __init__(self, database_manager: DatabaseManagerPort):
self.databaseManager = database_manager
@@ -7,7 +7,7 @@ from tui_rsync.infrastructure.orm.dto.dtos import BackupPlanDTO
from tui_rsync.infrastructure.orm.models import BackupPlanModel
class GetBackupBackupPlanByIdQuery:
class GetBackupPlanByIdQuery:
def __init__(self, database_manager: DatabaseManagerPort):
self.databaseManager = database_manager
@@ -0,0 +1,11 @@
from tui_rsync.core.ports.orm import DatabaseManagerPort
from tui_rsync.infrastructure.orm.models import BackupPlanModel
class GetBackupPlanCountQuery:
def __init__(self, database_manager: DatabaseManagerPort):
self.databaseManager = database_manager
def execute(self):
return BackupPlanModel.select().count()
@@ -1,3 +1,4 @@
from .backup_plan_repository_port import BackupPlanRepositoryPort
from .backup_plan_repository import BackupPlanRepository
__all__ = ['BackupPlanRepositoryPort']
__all__ = ['BackupPlanRepositoryPort', 'BackupPlanRepository']
@@ -9,7 +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
from ..queries import GetBackupPlanByIdQuery
class BackupPlanRepository(BackupPlanRepositoryPort):
@@ -24,7 +24,7 @@ class BackupPlanRepository(BackupPlanRepositoryPort):
destination.save(force_insert=True)
def get_by_id(self, uuid: UUID) -> BackupPlan:
query = GetBackupBackupPlanByIdQuery(self.databaseManager)
query = GetBackupPlanByIdQuery(self.databaseManager)
return query.execute(uuid)
def update(self, backup_plan: BackupPlan):
@@ -0,0 +1,5 @@
from .backup_plan_service import BackupPlanService
from .get_all_backup_plans_service import GetAllBackupPlansService
from .get_backup_plan_count_service import GetBackupPlanCountService
__all__ = ['BackupPlanService', 'GetAllBackupPlansService', 'GetBackupPlanCountService']
@@ -0,0 +1,13 @@
from typing import List
from tui_rsync.core.components.backup_plan.application.queries import GetAllBackupPlansQuery
from tui_rsync.core.components.backup_plan.domain import BackupPlan
class GetAllBackupPlansService:
def __init__(self, get_all_backup_plan_query: GetAllBackupPlansQuery):
self.get_all_backup_plan_query = get_all_backup_plan_query
def get_all(self) -> List[BackupPlan]:
return self.get_all_backup_plan_query.execute()
@@ -0,0 +1,16 @@
from typing import List
from tui_rsync.core.components.backup_plan.application.queries import GetBackupPlanCountQuery
from tui_rsync.core.components.backup_plan.domain import BackupPlan
class GetBackupPlanCountService:
def __init__(self, get_backup_plan_count_query: GetBackupPlanCountQuery):
self.get_backup_plan_count_query = get_backup_plan_count_query
def count(self) -> int:
return self.get_backup_plan_count_query.execute()
def is_empty(self) -> bool:
return self.count() == 0
@@ -1,9 +1,10 @@
from injector import singleton, Module, provider
from tui_rsync.core.components.backup_plan.application.commands import RemoveAllBackupBackupPlansCommand
from tui_rsync.core.components.backup_plan.application.queries import GetAllBackupBackupPlansQuery
from tui_rsync.core.components.backup_plan.application.repository import BackupPlanRepositoryPort
from tui_rsync.core.components.backup_plan.application.repository.backup_plan_repository import BackupPlanRepository
from tui_rsync.core.components.backup_plan.application.queries import GetAllBackupPlansQuery, GetBackupPlanCountQuery
from tui_rsync.core.components.backup_plan.application.repository import BackupPlanRepositoryPort, BackupPlanRepository
from tui_rsync.core.components.backup_plan.application.services import GetBackupPlanCountService, \
GetAllBackupPlansService
from tui_rsync.core.components.backup_plan.application.services.backup_plan_service import BackupPlanService
from tui_rsync.core.ports.configuration import UserDataPathsPort
from tui_rsync.core.ports.orm import DatabaseManagerPort
@@ -38,13 +39,28 @@ class Configuration(Module):
@provider
@singleton
def provide_remove_all_backup_backup_plans_command(self, database_manager: DatabaseManagerPort) -> RemoveAllBackupBackupPlansCommand:
def provide_remove_all_backup_plans_command(self, database_manager: DatabaseManagerPort) -> RemoveAllBackupBackupPlansCommand:
return RemoveAllBackupBackupPlansCommand(database_manager)
@provider
@singleton
def provide_get_all_backup_backup_plans_query(self, database_manager: DatabaseManagerPort) -> GetAllBackupBackupPlansQuery:
return GetAllBackupBackupPlansQuery(database_manager)
def provide_get_all_backup_plans_query(self, database_manager: DatabaseManagerPort) -> GetAllBackupPlansQuery:
return GetAllBackupPlansQuery(database_manager)
@provider
@singleton
def provide_get_backup_plan_count_query(self, database_manager: DatabaseManagerPort) -> GetBackupPlanCountQuery:
return GetBackupPlanCountQuery(database_manager)
@provider
@singleton
def provide_get_backup_plan_count_service(self, query: GetBackupPlanCountQuery) -> GetBackupPlanCountService:
return GetBackupPlanCountService(query)
@provider
@singleton
def provide_get_all_backup_plans_service(self, query: GetAllBackupPlansQuery) -> GetAllBackupPlansService:
return GetAllBackupPlansService(query)
@provider
@singleton
@@ -1,9 +1,10 @@
from injector import singleton, Module, provider
from tui_rsync.core.components.backup_plan.application.commands import RemoveAllBackupBackupPlansCommand
from tui_rsync.core.components.backup_plan.application.queries import GetAllBackupBackupPlansQuery
from tui_rsync.core.components.backup_plan.application.repository import BackupPlanRepositoryPort
from tui_rsync.core.components.backup_plan.application.repository.backup_plan_repository import BackupPlanRepository
from tui_rsync.core.components.backup_plan.application.queries import GetAllBackupPlansQuery, GetBackupPlanCountQuery
from tui_rsync.core.components.backup_plan.application.repository import BackupPlanRepositoryPort, BackupPlanRepository
from tui_rsync.core.components.backup_plan.application.services import GetBackupPlanCountService, \
GetAllBackupPlansService
from tui_rsync.core.components.backup_plan.application.services.backup_plan_service import BackupPlanService
from tui_rsync.core.ports.configuration import UserDataPathsPort
from tui_rsync.core.ports.orm import DatabaseManagerPort
@@ -38,13 +39,28 @@ class TestingConfiguration(Module):
@provider
@singleton
def provide_remove_all_backup_backup_plans_command(self, database_manager: DatabaseManagerPort) -> RemoveAllBackupBackupPlansCommand:
def provide_remove_all_backup_plans_command(self, database_manager: DatabaseManagerPort) -> RemoveAllBackupBackupPlansCommand:
return RemoveAllBackupBackupPlansCommand(database_manager)
@provider
@singleton
def provide_get_all_backup_backup_plans_query(self, database_manager: DatabaseManagerPort) -> GetAllBackupBackupPlansQuery:
return GetAllBackupBackupPlansQuery(database_manager)
def provide_get_all_backup_plans_query(self, database_manager: DatabaseManagerPort) -> GetAllBackupPlansQuery:
return GetAllBackupPlansQuery(database_manager)
@provider
@singleton
def provide_get_backup_plan_count_query(self, database_manager: DatabaseManagerPort) -> GetBackupPlanCountQuery:
return GetBackupPlanCountQuery(database_manager)
@provider
@singleton
def provide_get_backup_plan_count_service(self, query: GetBackupPlanCountQuery) -> GetBackupPlanCountService:
return GetBackupPlanCountService(query)
@provider
@singleton
def provide_get_all_backup_plans_service(self, query: GetAllBackupPlansQuery) -> GetAllBackupPlansService:
return GetAllBackupPlansService(query)
@provider
@singleton
@@ -20,7 +20,8 @@
from rich.console import Console
import typer
from tui_rsync.core.components.backup_plan.application.queries import GetAllBackupBackupPlansQuery
from tui_rsync.core.components.backup_plan.application.services import GetAllBackupPlansService, \
GetBackupPlanCountService
from tui_rsync.core.components.backup_plan.application.services.backup_plan_service import BackupPlanService
from tui_rsync.core.shared_kernel.components.common import UUID
@@ -48,16 +49,8 @@ def one(
service: BackupPlanService = CurrentConfiguration.get(BackupPlanService)
if id is None:
console.print("[red b][ERROR][/] Backup plan does not exists!!!")
return
plan = service.get_by_id(UUID(id))
if plan is None:
console.print("[red b][ERROR][/] Backup plan does not exists!!!")
return
console.print(BackupPlanFormat.format(plan))
@@ -67,7 +60,10 @@ def all():
[green b]Show[/] [yellow]all existing backup plans[/].
"""
query = CurrentConfiguration.get(GetAllBackupBackupPlansQuery)
for backup_plan in query.execute():
console.print(BackupPlanFormat.format(backup_plan))
count_service = CurrentConfiguration.get(GetBackupPlanCountService)
if count_service.is_empty():
console.print("No backup plans.")
else:
service = CurrentConfiguration.get(GetAllBackupPlansService)
for backup_plan in service.get_all():
console.print(BackupPlanFormat.format(backup_plan))