DescribeSwitchUseCase.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Resources.Models;
  3. namespace RackPeek.Domain.Resources.Hardware.Switches;
  4. public record SwitchDescription(
  5. string Name,
  6. string? Model,
  7. bool? Managed,
  8. bool? Poe,
  9. int TotalPorts,
  10. double TotalSpeedGb,
  11. string PortSummary
  12. );
  13. public class DescribeSwitchUseCase(IHardwareRepository repository) : IUseCase
  14. {
  15. public async Task<SwitchDescription> ExecuteAsync(string name)
  16. {
  17. name = Normalize.HardwareName(name);
  18. ThrowIfInvalid.ResourceName(name);
  19. var switchResource = await repository.GetByNameAsync(name) as Switch;
  20. if (switchResource == null)
  21. throw new NotFoundException($"Switch '{name}' not found.");
  22. // If no ports exist, return defaults
  23. var ports = switchResource.Ports ?? new List<Port>();
  24. // Total ports count
  25. var totalPorts = ports.Sum(p => p.Count ?? 0);
  26. // Total speed (sum of each port speed * count)
  27. var totalSpeedGb = ports.Sum(p => (p.Speed ?? 0) * (p.Count ?? 0));
  28. // Build a port summary string
  29. var portGroups = ports
  30. .GroupBy(p => p.Type ?? "Unknown")
  31. .Select(g =>
  32. {
  33. var count = g.Sum(x => x.Count ?? 0);
  34. var speed = g.Sum(x => (x.Speed ?? 0) * (x.Count ?? 0));
  35. return $"{g.Key}: {count} ports ({speed} Gb total)";
  36. });
  37. var portSummary = string.Join(", ", portGroups);
  38. return new SwitchDescription(
  39. switchResource.Name,
  40. switchResource.Model,
  41. switchResource.Managed,
  42. switchResource.Poe,
  43. totalPorts,
  44. totalSpeedGb,
  45. portSummary
  46. );
  47. }
  48. }