using System.Text.Json; using FastEndpoints; using FrameworkBLL.EntityViewer; using GB5Shared.DTO.Framework.Login; using GB5Shared.FastEndPoint; using static GB5Shared.GB5Constant.Constant; namespace FrameworkSL.Endpoints.EntityViewer { // The Entity Viewer's real read: resolve the Active LayoutSchema JSON for // an entity — a TenantId-scoped override wins over the global default. public class GetEntityLayout : BaseEndpoint> { private readonly IEntityLayoutBLL _Bll; public GetEntityLayout(IEntityLayoutBLL bll) => _Bll = bll; public override void Configure() { Get("/EntityViewer/GetEntityLayout"); AllowAnonymous(); } public record Params( [property: QueryParam] string EntityCode, [property: FromHeader] string Login ); protected override string? GetCacheKey(Params req, LoginDTO loginDTO) => KeyGenerator.KeyGeneration(req.EntityCode, EntityConstant.ENTITYLAYOUT, CacheKeyLevel.CLIENT_LEVEL, loginDTO); protected override async Task> ExecuteAsync( Params req, LoginDTO loginDTO, CancellationToken ct) { try { var result = await _Bll.GetActiveLayout(req.EntityCode, loginDTO, ct); // The FE expects the parsed LayoutSchema (with a top-level "sections" array) // directly as the response body, not the EntityLayoutVersionDTO wrapper whose // SnapshotJson field carries that schema as a serialized string. object? layout = string.IsNullOrWhiteSpace(result?.SnapshotJson) ? null : JsonSerializer.Deserialize(result.SnapshotJson); return await GB5Shared.ResponseStandard.Response .CreateSuccessResponse(layout, CacheKeyLevel.CLIENT_LEVEL, loginDTO); } catch (Exception ex) { return await GB5Shared.ResponseStandard.Response .CreateExceptionError(ex, CacheKeyLevel.CLIENT_LEVEL, loginDTO, ex.Message, 500); } } } }