using Admin.NET.Plugin.AiDOP.DataPlatform.Core; using Admin.NET.Plugin.AiDOP.DataPlatform.Core.Edge; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; namespace Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress; public sealed partial class MdpIngestDatasetController { [HttpGet("{id:long}/edge")] public Task ListEdgeDevices(long id, long? factoryId) => EdgeAction(id, factoryId, TrialPermission, async (service, scope) => Ok(new { devices = await service.ListAsync(scope.TenantId, scope.FactoryId, id), canApprove = await Can(PublishPermission) })); [HttpPost("{id:long}/edge"), RequestSizeLimit(4096)] public Task ApproveEdgeDevice(long id, long? factoryId, MdpEdgeApproveInput input) => EdgeAction(id, factoryId, PublishPermission, async (service, scope) => Ok(await service.ApproveAsync(scope.TenantId, scope.FactoryId, id, input.SourceId, input.Name, user.UserId))); [HttpPut("{id:long}/edge/{device:guid}/task"), RequestSizeLimit(98304)] public Task ConfigureEdgeDevice(long id, Guid device, long? factoryId, MdpEdgeTaskInput input) => EdgeAction(id, factoryId, PublishPermission, async (service, scope) => { await service.ConfigureAsync(scope.TenantId, scope.FactoryId, id, device, user.UserId, input); return NoContent(); }); [HttpPost("{id:long}/edge/{device:guid}/recovery/preview"), RequestSizeLimit(4096)] public Task PreviewEdgePage(long id, Guid device, long? factoryId, MdpEdgePagePreviewInput input) => EdgeAction(id, factoryId, TrialPermission, async (service, scope) => Ok(await service.PreviewPageAsync(scope.TenantId, scope.FactoryId, id, device, input))); [HttpPost("{id:long}/edge/{device:guid}/recovery"), RequestSizeLimit(4096)] public Task RecoverEdgePage(long id, Guid device, long? factoryId, MdpEdgePageApproval input, CancellationToken token) => EdgeAction(id, factoryId, PublishPermission, async (service, scope) => { await service.RecoverPageAsync(scope.TenantId, scope.FactoryId, id, device, user.UserId, input, token); return NoContent(); }); [HttpPost("{id:long}/edge/{device:guid}/revoke")] public Task RevokeEdgeDevice(long id, Guid device, long? factoryId) => EdgeAction(id, factoryId, PublishPermission, async (service, scope) => { await service.RevokeAsync(scope.TenantId, scope.FactoryId, id, device, user.UserId); return NoContent(); }); private Task EdgeAction(long id, long? factoryId, string permission, Func> action) => WithScope(factoryId, async scope => { if (!await Can(permission)) return Forbid(); if (!await Datasets(scope).AnyAsync(row => row.Id == id)) return NotFound(); Response.Headers.CacheControl = "no-store"; try { return await action(HttpContext.RequestServices.GetRequiredService(), scope); } catch (Exception error) when (error is EdgeContractException or MdpIngestHttpException or IngestContractException or System.Data.Common.DbException or SqlSugarException) { return MdpEdgeDeviceController.EdgeFailure(error); } catch (Exception error) when (error is System.Text.Json.JsonException or ArgumentException or MdpApiPullException) { return BadRequest(new { code = "EDGE_TASK_INVALID" }); } }); } public sealed record MdpEdgeApproveInput(long SourceId, string Name);