DescribeDesktopUseCase.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Persistence;
  3. namespace RackPeek.Domain.Resources.Desktops;
  4. public record DesktopDescription(
  5. string Name,
  6. string? Model,
  7. int CpuCount,
  8. string? RamSummary,
  9. int DriveCount,
  10. int NicCount,
  11. int GpuCount,
  12. Dictionary<string, string> Labels
  13. );
  14. public class DescribeDesktopUseCase(IResourceCollection repository) : IUseCase
  15. {
  16. public async Task<DesktopDescription> ExecuteAsync(string name)
  17. {
  18. name = Normalize.HardwareName(name);
  19. ThrowIfInvalid.ResourceName(name);
  20. var desktop = await repository.GetByNameAsync(name) as Desktop;
  21. if (desktop == null)
  22. throw new NotFoundException($"Desktop '{name}' not found.");
  23. var ramSummary = desktop.Ram == null
  24. ? "None"
  25. : $"{desktop.Ram.Size} GB @ {desktop.Ram.Mts} MT/s";
  26. return new DesktopDescription(
  27. desktop.Name,
  28. desktop.Model,
  29. desktop.Cpus?.Count ?? 0,
  30. ramSummary,
  31. desktop.Drives?.Count ?? 0,
  32. desktop.Nics?.Count ?? 0,
  33. desktop.Gpus?.Count ?? 0,
  34. desktop.Labels
  35. );
  36. }
  37. }