UpdateDesktopDriveUseCase.cs 1.0 KB

12345678910111213141516171819202122232425262728
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Resources.Models;
  3. namespace RackPeek.Domain.Resources.Hardware.Laptops.Drives;
  4. public class UpdateLaptopDriveUseCase(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 laptop = await repository.GetByNameAsync(name) as Laptop
  13. ?? throw new NotFoundException($"Laptop '{name}' not found.");
  14. if (laptop.Drives == null || index < 0 || index >= laptop.Drives.Count)
  15. throw new NotFoundException($"Drive index {index} not found on Laptop '{name}'.");
  16. var drive = laptop.Drives[index];
  17. drive.Type = type;
  18. drive.Size = size;
  19. await repository.UpdateAsync(laptop);
  20. }
  21. }