RemovePortUseCase.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Persistence;
  3. using RackPeek.Domain.Resources;
  4. using RackPeek.Domain.Resources.Connections;
  5. using RackPeek.Domain.Resources.Servers;
  6. namespace RackPeek.Domain.UseCases.Ports;
  7. public interface IRemovePortUseCase<T> : IResourceUseCase<T>
  8. where T : Resource
  9. {
  10. Task ExecuteAsync(string name, int index);
  11. }
  12. public class RemovePortUseCase<T>(IResourceCollection repository)
  13. : IRemovePortUseCase<T> where T : Resource
  14. {
  15. public async Task ExecuteAsync(string name, int index)
  16. {
  17. name = Normalize.HardwareName(name);
  18. ThrowIfInvalid.ResourceName(name);
  19. T resource = await repository.GetByNameAsync<T>(name)
  20. ?? throw new NotFoundException($"Resource '{name}' not found.");
  21. if (resource is not IPortResource pr)
  22. throw new NotFoundException($"Resource '{name}' not found.");
  23. if (pr.Ports == null || index < 0 || index >= pr.Ports.Count)
  24. throw new NotFoundException($"Port index {index} not found on '{name}'.");
  25. IReadOnlyList<Connection> connections =
  26. await repository.GetConnectionsForResourceAsync(name);
  27. var toRemove = new List<Connection>();
  28. var toAdd = new List<Connection>();
  29. foreach (Connection connection in connections)
  30. {
  31. var changed = false;
  32. PortReference a = connection.A;
  33. PortReference b = connection.B;
  34. // handle A side
  35. if (a.Resource.Equals(name, StringComparison.OrdinalIgnoreCase))
  36. {
  37. if (a.PortGroup == index)
  38. {
  39. toRemove.Add(connection);
  40. continue;
  41. }
  42. if (a.PortGroup > index)
  43. {
  44. a = new PortReference
  45. {
  46. Resource = a.Resource,
  47. PortGroup = a.PortGroup - 1,
  48. PortIndex = a.PortIndex
  49. };
  50. changed = true;
  51. }
  52. }
  53. // handle B side
  54. if (b.Resource.Equals(name, StringComparison.OrdinalIgnoreCase))
  55. {
  56. if (b.PortGroup == index)
  57. {
  58. toRemove.Add(connection);
  59. continue;
  60. }
  61. if (b.PortGroup > index)
  62. {
  63. b = new PortReference
  64. {
  65. Resource = b.Resource,
  66. PortGroup = b.PortGroup - 1,
  67. PortIndex = b.PortIndex
  68. };
  69. changed = true;
  70. }
  71. }
  72. if (changed)
  73. {
  74. toRemove.Add(connection);
  75. toAdd.Add(new Connection
  76. {
  77. A = a,
  78. B = b,
  79. Label = connection.Label,
  80. Notes = connection.Notes
  81. });
  82. }
  83. }
  84. foreach (Connection connection in toRemove)
  85. await repository.RemoveConnectionAsync(connection);
  86. foreach (Connection connection in toAdd)
  87. await repository.AddConnectionAsync(connection);
  88. pr.Ports.RemoveAt(index);
  89. await repository.UpdateAsync(resource);
  90. }
  91. }