UpdateDesktopDriveUseCase.cs 1.1 KB

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