UpdateDriveUseCase.cs 1.4 KB

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