ResourceKindDispatch.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using Microsoft.Extensions.DependencyInjection;
  2. using RackPeek.Domain.Helpers;
  3. using RackPeek.Domain.Persistence;
  4. using RackPeek.Domain.Resources;
  5. using RackPeek.Domain.Resources.AccessPoints;
  6. using RackPeek.Domain.Resources.Desktops;
  7. using RackPeek.Domain.Resources.Firewalls;
  8. using RackPeek.Domain.Resources.Laptops;
  9. using RackPeek.Domain.Resources.OtherHardware;
  10. using RackPeek.Domain.Resources.Routers;
  11. using RackPeek.Domain.Resources.Servers;
  12. using RackPeek.Domain.Resources.Services;
  13. using RackPeek.Domain.Resources.Switches;
  14. using RackPeek.Domain.Resources.SystemResources;
  15. using RackPeek.Domain.Resources.UpsUnits;
  16. using RackPeek.Domain.UseCases;
  17. namespace RackPeek.Mcp;
  18. /// <summary>
  19. /// Closes the per-kind generic use cases over the concrete resource type at
  20. /// runtime. MCP tools receive a name, not a type — the kind is looked up and the
  21. /// call dispatched so type-sensitive code (the clone's deep copy serialises the
  22. /// real derived type, not the abstract base) runs against the right generic.
  23. /// </summary>
  24. internal static class ResourceKindDispatch {
  25. public static async Task CloneAsync(
  26. IServiceProvider services,
  27. IResourceCollection repo,
  28. string originalName,
  29. string cloneName) {
  30. var kind = await repo.GetKind(originalName)
  31. ?? throw new NotFoundException($"Resource '{originalName}' not found.");
  32. await (kind.Trim().ToLowerInvariant() switch {
  33. "server" => Run<Server>(),
  34. "switch" => Run<Switch>(),
  35. "firewall" => Run<Firewall>(),
  36. "router" => Run<Router>(),
  37. "accesspoint" => Run<AccessPoint>(),
  38. "desktop" => Run<Desktop>(),
  39. "laptop" => Run<Laptop>(),
  40. "ups" => Run<Ups>(),
  41. "other" => Run<Other>(),
  42. "system" => Run<SystemResource>(),
  43. "service" => Run<Service>(),
  44. _ => throw new NotFoundException($"Resource kind '{kind}' cannot be cloned.")
  45. });
  46. Task Run<T>() where T : Resource =>
  47. services.GetRequiredService<ICloneResourceUseCase<T>>()
  48. .ExecuteAsync(originalName, cloneName);
  49. }
  50. }