UpdateDriveUseCase.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Persistence;
  3. using RackPeek.Domain.Resources;
  4. using RackPeek.Domain.Resources.Models;
  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(name) ?? throw new NotFoundException($"Resource '{name}' not found.");
  20. if (resource is not IDriveResource dr)
  21. {
  22. throw new NotFoundException($"Resource '{name}' not found.");
  23. }
  24. if (dr.Drives == null || index < 0 || index >= dr.Drives.Count)
  25. throw new NotFoundException($"Drive index {index} not found on '{name}'.");
  26. var drive = dr.Drives[index];
  27. drive.Type = type;
  28. drive.Size = size;
  29. await repository.UpdateAsync(resource);
  30. }
  31. }