AddGpuUseCase.cs 1.3 KB

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