UpdateGpuUseCase.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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.Gpus;
  6. public interface IUpdateGpuUseCase<T> : IResourceUseCase<T>
  7. where T : Resource
  8. {
  9. public Task ExecuteAsync(
  10. string name,
  11. int index,
  12. string? model,
  13. int? vram);
  14. }
  15. public class UpdateGpuUseCase<T>(IResourceCollection repository) : IUpdateGpuUseCase<T> where T : Resource
  16. {
  17. public async Task ExecuteAsync(
  18. string name,
  19. int index,
  20. string? model,
  21. int? vram)
  22. {
  23. // ToDo pass in properties as inputs, construct the entity in the usecase, ensure optional inputs are nullable
  24. // ToDo validate / normalize all inputs
  25. name = Normalize.HardwareName(name);
  26. ThrowIfInvalid.ResourceName(name);
  27. var resource = await repository.GetByNameAsync<T>(name) ??
  28. throw new NotFoundException($"Resource '{name}' not found.");
  29. if (resource is not IGpuResource gr) throw new NotFoundException($"Resource '{name}' not found.");
  30. if (gr.Gpus == null || index < 0 || index >= gr.Gpus.Count)
  31. throw new NotFoundException($"GPU index {index} not found on '{name}'.");
  32. var gpu = gr.Gpus[index];
  33. gpu.Model = model;
  34. gpu.Vram = vram;
  35. await repository.UpdateAsync(resource);
  36. }
  37. }