UpdateSystemDriveUseCase.cs 1.3 KB

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