4
0

UpdateServiceUseCase.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. )
  14. {
  15. // ToDo pass in properties as inputs, construct the entity in the usecase, ensure optional inputs are nullable
  16. // ToDo validate / normalize all inputs
  17. name = Normalize.ServiceName(name);
  18. ThrowIfInvalid.ResourceName(name);
  19. var service = await repository.GetByNameAsync(name);
  20. if (service is null)
  21. throw new NotFoundException($"Service '{name}' not found.");
  22. if (!string.IsNullOrWhiteSpace(ip))
  23. {
  24. service.Network ??= new Network();
  25. service.Network.Ip = ip;
  26. }
  27. if (!string.IsNullOrWhiteSpace(protocol))
  28. {
  29. service.Network ??= new Network();
  30. service.Network.Protocol = protocol;
  31. }
  32. if (!string.IsNullOrWhiteSpace(url))
  33. {
  34. service.Network ??= new Network();
  35. service.Network.Url = url;
  36. }
  37. if (port.HasValue)
  38. {
  39. service.Network ??= new Network();
  40. service.Network.Port = port.Value;
  41. }
  42. if (!string.IsNullOrWhiteSpace(runsOn))
  43. {
  44. ThrowIfInvalid.ResourceName(runsOn);
  45. var parentSystem = await systemRepo.GetByNameAsync(runsOn);
  46. if (parentSystem == null) throw new NotFoundException($"Parent system '{runsOn}' not found.");
  47. service.RunsOn = runsOn;
  48. }
  49. await repository.UpdateAsync(service);
  50. }
  51. }