UpdateServiceUseCase.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Resources.SystemResources;
  3. namespace RackPeek.Domain.Resources.Services.UseCases;
  4. public class UpdateServiceUseCase(IServiceRepository repository, ISystemRepository systemRepo) : IUseCase
  5. {
  6. public async Task ExecuteAsync(
  7. string name,
  8. string? ip = null,
  9. int? port = null,
  10. string? protocol = null,
  11. string? url = null,
  12. string? runsOn = null,
  13. string? notes = null
  14. )
  15. {
  16. // ToDo pass in properties as inputs, construct the entity in the usecase, ensure optional inputs are nullable
  17. // ToDo validate / normalize all inputs
  18. name = Normalize.ServiceName(name);
  19. ThrowIfInvalid.ResourceName(name);
  20. var service = await repository.GetByNameAsync(name);
  21. if (service is null)
  22. throw new NotFoundException($"Service '{name}' not found.");
  23. if (!string.IsNullOrWhiteSpace(ip))
  24. {
  25. service.Network ??= new Network();
  26. service.Network.Ip = ip;
  27. }
  28. if (!string.IsNullOrWhiteSpace(protocol))
  29. {
  30. service.Network ??= new Network();
  31. service.Network.Protocol = protocol;
  32. }
  33. if (!string.IsNullOrWhiteSpace(url))
  34. {
  35. service.Network ??= new Network();
  36. service.Network.Url = url;
  37. }
  38. if (port.HasValue)
  39. {
  40. service.Network ??= new Network();
  41. service.Network.Port = port.Value;
  42. }
  43. if (!string.IsNullOrWhiteSpace(runsOn))
  44. {
  45. ThrowIfInvalid.ResourceName(runsOn);
  46. var parentSystem = await systemRepo.GetByNameAsync(runsOn);
  47. if (parentSystem == null) throw new NotFoundException($"Parent system '{runsOn}' not found.");
  48. service.RunsOn = runsOn;
  49. }
  50. if (notes != null)
  51. {
  52. service.Notes = notes;
  53. }
  54. await repository.UpdateAsync(service);
  55. }
  56. }