InventoryReadEndpoints.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System.ComponentModel.DataAnnotations;
  2. using RackPeek.Domain.Api;
  3. using RackPeek.Domain.Helpers;
  4. using RackPeek.Domain.Persistence;
  5. using RackPeek.Domain.Resources;
  6. using RackPeek.Domain.Resources.Hardware;
  7. using RackPeek.Domain.Resources.Services.UseCases;
  8. using RackPeek.Domain.Resources.SystemResources.UseCases;
  9. using RackPeek.Domain.Search;
  10. using RackPeek.Mcp.Tools;
  11. namespace RackPeek.Web.Api;
  12. /// <summary>
  13. /// Read-only counterparts to POST /api/inventory, gated by the same API key.
  14. /// The full document endpoint is the workhorse: a client fetches it once and
  15. /// renders lists, details and trees from it. Summary, search and tree mirror the
  16. /// MCP query tools and reuse their response records so the two surfaces cannot drift.
  17. /// </summary>
  18. public static class InventoryReadEndpoints {
  19. public static void MapInventoryReadApi(this WebApplication app) {
  20. RouteGroupBuilder group = app.MapGroup("/api/inventory")
  21. .AddEndpointFilter<ApiKeyEndpointFilter>()
  22. .DisableAntiforgery();
  23. group.MapGet("", async (IResourceCollection repo) => {
  24. try {
  25. return Results.Json(await InventoryDocumentJson.BuildDocumentAsync(repo),
  26. InventoryDocumentJson.Options);
  27. }
  28. catch (Exception ex) {
  29. return ReadError(ex);
  30. }
  31. });
  32. group.MapGet("summary", async (
  33. IResourceCollection repo,
  34. GetHardwareUseCaseSummary hardwareSummary,
  35. GetSystemSummaryUseCase systemsSummary,
  36. GetServiceSummaryUseCase servicesSummary) => {
  37. try {
  38. var result = new InfrastructureSummary(
  39. await hardwareSummary.ExecuteAsync(),
  40. await systemsSummary.ExecuteAsync(),
  41. await servicesSummary.ExecuteAsync(),
  42. await repo.GetTagsAsync(),
  43. await repo.GetLabelsAsync());
  44. return Results.Ok(result);
  45. }
  46. catch (Exception ex) {
  47. return ReadError(ex);
  48. }
  49. });
  50. group.MapGet("search", async (string q, int? max, IResourceCollection repo) => {
  51. try {
  52. IReadOnlyList<Resource> all = await repo.GetAllOfTypeAsync<Resource>();
  53. return Results.Ok(new SearchResults(
  54. GlobalSearchService.Search(all, q, max ?? 8)));
  55. }
  56. catch (Exception ex) {
  57. return ReadError(ex);
  58. }
  59. });
  60. group.MapGet("tree", async (IHardwareRepository repo) => {
  61. try {
  62. return Results.Ok(new TreeResult(await repo.GetTreeAsync()));
  63. }
  64. catch (Exception ex) {
  65. return ReadError(ex);
  66. }
  67. });
  68. }
  69. private static IResult ReadError(Exception ex) => ex switch {
  70. NotFoundException => Results.NotFound(new { error = ex.Message }),
  71. ValidationException => Results.BadRequest(new { error = ex.Message }),
  72. _ => Results.Problem(ex.Message)
  73. };
  74. }