UpdateDriveUseCase.cs 1.0 KB

123456789101112131415161718192021222324252627282930
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Resources.Models;
  3. namespace RackPeek.Domain.Resources.Hardware.Servers.Drives;
  4. public class UpdateDriveUseCase(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 hardware = await repository.GetByNameAsync(name);
  13. if (hardware is not Server server)
  14. throw new NotFoundException($"Server '{name}' not found.");
  15. server.Drives ??= [];
  16. if (index < 0 || index >= server.Drives.Count)
  17. throw new ArgumentOutOfRangeException(nameof(index), "Drive index out of range.");
  18. var drive = server.Drives[index];
  19. drive.Type = type;
  20. drive.Size = size;
  21. await repository.UpdateAsync(server);
  22. }
  23. }