uto_manager/lib/domain/repositories/unit_repository.dart

65 lines
2.4 KiB
Dart

import 'package:uto_manager/domain/entities/echelon.dart';
import 'package:uto_manager/domain/entities/unit_node.dart';
/// Abstract repository interface for [UnitNode] persistence operations.
///
/// Implementations are provided by the infrastructure layer (Drift/SQLite
/// for Windows/Android, IndexedDB for Web). The domain layer depends only
/// on this interface, never on concrete implementations.
abstract interface class UnitRepository {
/// Returns all unit nodes in the database.
Future<List<UnitNode>> getAll();
/// Returns the unit node with the given [unitId], or null if not found.
Future<UnitNode?> getById(String unitId);
/// Returns all direct children of the unit with [parentUnitId].
Future<List<UnitNode>> getChildren(String parentUnitId);
/// Returns all root units (units with no parent).
Future<List<UnitNode>> getRoots();
/// Returns all units at the given [echelon] level.
Future<List<UnitNode>> getByEchelon(Echelon echelon);
/// Returns the full subtree rooted at [unitId] (inclusive).
Future<List<UnitNode>> getSubtree(String unitId);
/// Inserts a new [unit] into the repository.
///
/// Throws [StateError] if a unit with the same ID already exists.
Future<void> insert(UnitNode unit);
/// Inserts multiple [units] in a single atomic transaction.
Future<void> insertAll(List<UnitNode> units);
/// Updates an existing [unit] in the repository.
///
/// Throws [StateError] if no unit with the given ID exists.
Future<void> update(UnitNode unit);
/// Deletes the unit with [unitId] from the repository.
///
/// Does not cascade-delete children; caller is responsible for
/// re-parenting or deleting child units first.
Future<void> delete(String unitId);
/// Deletes the unit with [unitId] and all its descendants.
Future<void> deleteSubtree(String unitId);
/// Re-parents the unit with [unitId] to the unit with [newParentId].
///
/// Validates that the new parent echelon is compatible before executing.
/// Throws [ArgumentError] if the re-parenting would violate doctrine.
Future<void> reparent(String unitId, String newParentId);
/// Returns a stream that emits the full unit list whenever it changes.
Stream<List<UnitNode>> watchAll();
/// Returns a stream that emits the subtree rooted at [unitId] on changes.
Stream<List<UnitNode>> watchSubtree(String unitId);
/// Deletes all units from the repository (used for template reset).
Future<void> deleteAll();
}