MdpIngestDatasetController.Edge.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using Admin.NET.Plugin.AiDOP.DataPlatform.Core;
  2. using Admin.NET.Plugin.AiDOP.DataPlatform.Core.Edge;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.Extensions.DependencyInjection;
  5. namespace Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress;
  6. public sealed partial class MdpIngestDatasetController
  7. {
  8. [HttpGet("{id:long}/edge")]
  9. public Task<IActionResult> ListEdgeDevices(long id, long? factoryId) =>
  10. EdgeAction(id, factoryId, TrialPermission, async (service, scope) =>
  11. Ok(new { devices = await service.ListAsync(scope.TenantId, scope.FactoryId, id), canApprove = await Can(PublishPermission) }));
  12. [HttpPost("{id:long}/edge"), RequestSizeLimit(4096)]
  13. public Task<IActionResult> ApproveEdgeDevice(long id, long? factoryId, MdpEdgeApproveInput input) =>
  14. EdgeAction(id, factoryId, PublishPermission, async (service, scope) =>
  15. Ok(await service.ApproveAsync(scope.TenantId, scope.FactoryId, id, input.SourceId, input.Name, user.UserId)));
  16. [HttpPut("{id:long}/edge/{device:guid}/task"), RequestSizeLimit(98304)]
  17. public Task<IActionResult> ConfigureEdgeDevice(long id, Guid device, long? factoryId, MdpEdgeTaskInput input) =>
  18. EdgeAction(id, factoryId, PublishPermission, async (service, scope) =>
  19. {
  20. await service.ConfigureAsync(scope.TenantId, scope.FactoryId, id, device, user.UserId, input);
  21. return NoContent();
  22. });
  23. [HttpPost("{id:long}/edge/{device:guid}/recovery/preview"), RequestSizeLimit(4096)]
  24. public Task<IActionResult> PreviewEdgePage(long id, Guid device, long? factoryId, MdpEdgePagePreviewInput input) =>
  25. EdgeAction(id, factoryId, TrialPermission, async (service, scope) =>
  26. Ok(await service.PreviewPageAsync(scope.TenantId, scope.FactoryId, id, device, input)));
  27. [HttpPost("{id:long}/edge/{device:guid}/recovery"), RequestSizeLimit(4096)]
  28. public Task<IActionResult> RecoverEdgePage(long id, Guid device, long? factoryId, MdpEdgePageApproval input, CancellationToken token) =>
  29. EdgeAction(id, factoryId, PublishPermission, async (service, scope) =>
  30. {
  31. await service.RecoverPageAsync(scope.TenantId, scope.FactoryId, id, device, user.UserId, input, token);
  32. return NoContent();
  33. });
  34. [HttpPost("{id:long}/edge/{device:guid}/revoke")]
  35. public Task<IActionResult> RevokeEdgeDevice(long id, Guid device, long? factoryId) =>
  36. EdgeAction(id, factoryId, PublishPermission, async (service, scope) =>
  37. {
  38. await service.RevokeAsync(scope.TenantId, scope.FactoryId, id, device, user.UserId);
  39. return NoContent();
  40. });
  41. private Task<IActionResult> EdgeAction(long id, long? factoryId, string permission, Func<MdpEdgeDeviceService, DatasetScope, Task<IActionResult>> action)
  42. => WithScope(factoryId, async scope =>
  43. {
  44. if (!await Can(permission)) return Forbid();
  45. if (!await Datasets(scope).AnyAsync(row => row.Id == id)) return NotFound();
  46. Response.Headers.CacheControl = "no-store";
  47. try { return await action(HttpContext.RequestServices.GetRequiredService<MdpEdgeDeviceService>(), scope); }
  48. catch (Exception error) when (error is EdgeContractException or MdpIngestHttpException or IngestContractException
  49. or System.Data.Common.DbException or SqlSugarException)
  50. {
  51. return MdpEdgeDeviceController.EdgeFailure(error);
  52. }
  53. catch (Exception error) when (error is System.Text.Json.JsonException or ArgumentException or MdpApiPullException)
  54. {
  55. return BadRequest(new { code = "EDGE_TASK_INVALID" });
  56. }
  57. });
  58. }
  59. public sealed record MdpEdgeApproveInput(long SourceId, string Name);