UpdateSystemDriveUseCase.cs 1.1 KB

123456789101112131415161718192021222324252627282930
  1. using RackPeek.Domain.Helpers;
  2. namespace RackPeek.Domain.Resources.SystemResources.UseCases;
  3. public class UpdateSystemDriveUseCase(ISystemRepository repository) : IUseCase
  4. {
  5. public async Task ExecuteAsync(string systemName, int index, string driveType, int size)
  6. {
  7. // ToDo pass in properties as inputs, construct the entity in the usecase, ensure optional inputs are nullable
  8. // ToDo validate / normalize all inputs
  9. ThrowIfInvalid.ResourceName(systemName);
  10. var driveTypeNormalized = Normalize.DriveType(driveType);
  11. ThrowIfInvalid.DriveType(driveTypeNormalized);
  12. ThrowIfInvalid.DriveSize(size);
  13. var system = await repository.GetByNameAsync(systemName) ??
  14. throw new NotFoundException($"System '{systemName}' not found.");
  15. if (system.Drives == null || index < 0 || index >= system.Drives.Count)
  16. throw new NotFoundException($"Drive index {index} not found on system '{systemName}'.");
  17. var drive = system.Drives[index];
  18. drive.Type = driveTypeNormalized;
  19. drive.Size = size;
  20. await repository.UpdateAsync(system);
  21. }
  22. }