UpdateServiceUseCase.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using RackPeek.Domain.Helpers;
  2. namespace RackPeek.Domain.Resources.Services.UseCases;
  3. public class UpdateServiceUseCase(IServiceRepository repository) : IUseCase
  4. {
  5. public async Task ExecuteAsync(
  6. string name,
  7. string? ip = null,
  8. int? port = null,
  9. string? protocol = null,
  10. string? url = null,
  11. string? runsOn = null
  12. )
  13. {
  14. ThrowIfInvalid.ResourceName(name);
  15. var service = await repository.GetByNameAsync(name);
  16. if (service is null)
  17. throw new InvalidOperationException($"Service '{name}' not found.");
  18. if (!string.IsNullOrWhiteSpace(ip))
  19. {
  20. service.Network ??= new Network();
  21. service.Network.Ip = ip;
  22. }
  23. if (!string.IsNullOrWhiteSpace(protocol))
  24. {
  25. service.Network ??= new Network();
  26. service.Network.Protocol = protocol;
  27. }
  28. if (!string.IsNullOrWhiteSpace(url))
  29. {
  30. service.Network ??= new Network();
  31. service.Network.Url = url;
  32. }
  33. if (port.HasValue)
  34. {
  35. service.Network ??= new Network();
  36. service.Network.Port = port.Value;
  37. }
  38. if (!string.IsNullOrWhiteSpace(runsOn))
  39. service.RunsOn = runsOn;
  40. await repository.UpdateAsync(service);
  41. }
  42. }