using System.Text.Json; using AnalyticsBLL.Analysis; using FastEndpoints; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.DTO.Framework.ResponseStandard; using GB5Shared.DTO.Report; using GB5Shared.FastEndPoint; using static GB5Shared.GB5Constant.Constant; namespace AnalyticsSL.EndPoints.Analysis { public class AnalysisDynamicOutput : BaseEndpoint> { private readonly IAnalysisBLL _AnalysisBLL; public AnalysisDynamicOutput(IAnalysisBLL IAnalysisBLL) { _AnalysisBLL = IAnalysisBLL; } public override void Configure() { Post("/Analysis/AnalysisDynamicOutput"); AllowAnonymous(); } public record AnalysisDynamicOutputParameters( [property: FromHeader] string Login, [property: QueryParam] int AnalysisQueryId, [property: QueryParam] int ReportviewId, [property: QueryParam] int FirstNumber, [property: QueryParam] int MaxResult, [property: FromBody] ReportCallingDTO ReportCallingDTO ); protected override async Task> ExecuteAsync(AnalysisDynamicOutputParameters req, LoginDTO LoginDTO, CancellationToken ct) { try { var rows = await _AnalysisBLL.DynamicOutput(req.AnalysisQueryId, req.ReportviewId, req.FirstNumber, req.MaxResult, req.ReportCallingDTO.CriteriaDTO, LoginDTO!, ct); // For export requests (ReportFormat header present) pass the list directly as the // response body so BaseEndpoint.ToAsyncRows takes the fast IEnumerable path — // no JSON round-trip. For normal grid requests, serialize to a JSON string so the // frontend receives Body as a JSON string, preserving the existing contract. bool isExport = HttpContext.Request.Headers.ContainsKey("ReportFormat"); object body = isExport ? (object)rows : JsonSerializer.Serialize(rows); return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse(body, CacheKeyLevel.USER_LEVEL, LoginDTO); } catch (Exception ex) { return await GB5Shared.ResponseStandard.Response.CreateExceptionError(ex, CacheKeyLevel.USER_LEVEL, LoginDTO, ex.Message, 500); } } } }