AddGpuUseCase.cs 950 B

12345678910111213141516171819202122232425262728293031323334
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Resources.Hardware.Models;
  3. namespace RackPeek.Domain.Resources.Hardware.Servers.Gpus;
  4. public class AddGpuUseCase(IHardwareRepository repository) : IUseCase
  5. {
  6. public async Task ExecuteAsync(
  7. string name,
  8. string? model,
  9. int? vram)
  10. {
  11. // ToDo pass in properties as inputs, construct the entity in the usecase, ensure optional inputs are nullable
  12. // ToDo validate / normalize all inputs
  13. name = Normalize.HardwareName(name);
  14. ThrowIfInvalid.ResourceName(name);
  15. var hardware = await repository.GetByNameAsync(name);
  16. if (hardware is not Server server)
  17. throw new NotFoundException($"Server '{name}' not found.");
  18. server.Gpus ??= [];
  19. server.Gpus.Add(new Gpu
  20. {
  21. Model = model,
  22. Vram = vram
  23. });
  24. await repository.UpdateAsync(server);
  25. }
  26. }