AddGpuUseCase.cs 1.3 KB

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