UpdateDesktopDriveUseCase.cs 1.0 KB

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