UpdateDriveUseCase.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Persistence;
  3. using RackPeek.Domain.Resources;
  4. using RackPeek.Domain.Resources.Servers;
  5. namespace RackPeek.Domain.UseCases.Drives;
  6. public interface IUpdateDriveUseCase<T> : IResourceUseCase<T>
  7. where T : Resource
  8. {
  9. public Task ExecuteAsync(string name, int index, string? type, int? size);
  10. }
  11. public class UpdateDriveUseCase<T>(IResourceCollection repository) : IUpdateDriveUseCase<T> where T : Resource
  12. {
  13. public async Task ExecuteAsync(string name, int index, string? type, int? size)
  14. {
  15. // ToDo pass in properties as inputs, construct the entity in the usecase, ensure optional inputs are nullable
  16. // ToDo validate / normalize all inputs
  17. name = Normalize.HardwareName(name);
  18. ThrowIfInvalid.ResourceName(name);
  19. var resource = await repository.GetByNameAsync<T>(name) ??
  20. throw new NotFoundException($"Resource '{name}' not found.");
  21. if (resource is not IDriveResource dr) throw new NotFoundException($"Resource '{name}' not found.");
  22. if (dr.Drives == null || index < 0 || index >= dr.Drives.Count)
  23. throw new NotFoundException($"Drive index {index} not found on '{name}'.");
  24. var drive = dr.Drives[index];
  25. drive.Type = type;
  26. drive.Size = size;
  27. await repository.UpdateAsync(resource);
  28. }
  29. }