UpdateDriveUseCase.cs 1.1 KB

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