using GB5Shared.DTO.Framework.Logging; using GB5Shared.DTO.Framework.Login; using GB5Shared.DTO.Framework.ResponseStandard; using GB5Shared.DTO.Framework.SuccessORError; using GB5Shared.GB5CommonFunction; using GB5Shared.GB5Library.Qualifier; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static GB5Shared.GB5Constant.Constant; namespace GB5Shared.ResponseStandard { public static class Response { public static async Task> CreateSuccessResponseEnumerable(IEnumerable data,int CacheKeyLevel, LoginDTO LoginDTO, string message = "Operation succeeded", int statusCode = 200) { IGB5CommonFunction commonFunction = new GB5Shared.GB5CommonFunction.GB5CommonFunction(); //int? userId = LoginDTO?.UserId; //object responseBody = (userId == null || userId.Value == 100) // ? data! // : await commonFunction.GZipCompress(data); return await Task.FromResult(new ResponseStandardDTO { Body = data, // Ensuring SuccessDTO is wrapped inside Body Status = GB5Shared.DTO.Framework.Enum.FrameworkEnumDTO.ResponseStatus.Ok, Id = "", To = "", From = "", CacheLevel = CacheKeyLevel, ReplyTo = "", ETag = "", Last = "", LastLoginUsedTime = "", Next = "", Previous = "", ResponseObject = " ", Total = data.Count(), ValidToTime = "", ValidityOfSession = "", }); } public static async Task> CreateSuccessResponse(T data, int CacheKeyLevel,LoginDTO LoginDTO, string message = "Operation succeeded", int statusCode = 200 ) { IGB5CommonFunction CommonFunction = new GB5Shared.GB5CommonFunction.GB5CommonFunction(); //int? userId = LoginDTO?.UserId; //object responseBody; //if (userId == null || userId.Value == 100) //{ // responseBody = data!; //} //else //{ // string compressed = await CommonFunction.GZipCompress(data); // responseBody = compressed; //} return await Task.FromResult(new ResponseStandardDTO { Body = data!, // Ensuring SuccessDTO is inside Body Status = GB5Shared.DTO.Framework.Enum.FrameworkEnumDTO.ResponseStatus.Ok, Id = "", To = "", From = "", CacheLevel = CacheKeyLevel, ReplyTo = "", ETag = "", Last = "", LastLoginUsedTime = "", Next = "", Previous = "", ResponseObject = " ", Total = 0, ValidToTime = "", ValidityOfSession = "", }); } public static async Task> CreateSuccessResponseForReport(T data, int CacheKeyLevel, LoginDTO LoginDTO, string message = "Operation succeeded", int statusCode = 200) { IGB5CommonFunction CommonFunction = new GB5Shared.GB5CommonFunction.GB5CommonFunction(); //int? userId = LoginDTO?.UserId; //object responseBody; //if (userId == null || userId.Value == 100) //{ // responseBody = data!; //} //else //{ // string compressed = await CommonFunction.GZipCompress(data); // responseBody = compressed; //} // INNER response (goes inside Body) var SuccessDTO = new SuccessDTO { Body = data! }; return await Task.FromResult(new ResponseStandardDTO { Body = SuccessDTO, // Ensuring SuccessDTO is inside Body Status = GB5Shared.DTO.Framework.Enum.FrameworkEnumDTO.ResponseStatus.Ok, Id = "", To = "", From = "", CacheLevel = CacheKeyLevel, ReplyTo = "", ETag = "", Last = "", LastLoginUsedTime = "", Next = "", Previous = "", ResponseObject = " ", Total = 0, ValidToTime = "", ValidityOfSession = "", }); } public static async Task> GB4CreateSuccessResponseGZipCompress(string data, int CacheKeyLevel, LoginDTO LoginDTO, string message = "Operation succeeded", int statusCode = 200) { IGB5CommonFunction CommonFunction = new GB5Shared.GB5CommonFunction.GB5CommonFunction(); //string compressed = await CommonFunction.GZipCompress(data); GB4SuccessDTO successDTO = new(); successDTO.Body= data; successDTO.Status = "200"; string Json = JsonConvert.SerializeObject(successDTO); return await Task.FromResult(new GB4ResponseStandardDTO { Body = await CommonFunction.GZipCompress(Json) , // Ensuring SuccessDTO is inside Body Status = GB5Shared.DTO.Framework.Enum.FrameworkEnumDTO.ResponseStatus.Ok, Id = "", To = "", From = "", CacheLevel = CacheKeyLevel, ReplyTo = "", ETag = "", Last = "", LastLoginUsedTime = "", Next = "", Previous = "", ResponseObject = " ", Total = 0, ValidToTime = "", ValidityOfSession = "", }); } // Error response for an exception. // Automatically surfaces ValidationException field-level errors so the client // receives the real reason (e.g. "TaskName: required; AssignedTo: required") // instead of the generic "Validation failed" message. public static async Task> CreateExceptionError(Exception ex, int CacheKeyLevel, LoginDTO LoginDTO , string message = "An error occurred", int statusCode = 500) { IGB5CommonFunction commonFunction = new GB5Shared.GB5CommonFunction.GB5CommonFunction(); // Unwrap ValidationException — the real errors are in ve.Errors, not ex.Message. // ex.Message is always the hardcoded "Validation failed" sentinel. string[] generalErrors; if (ex is ValidationException ve && ve.Errors?.Count > 0) { generalErrors = ve.Errors .Select(e => string.IsNullOrEmpty(e.Field) ? e.Message : $"{e.Field}: {e.Message}") .ToArray(); } else { generalErrors = ex.InnerException != null ? new[] { ex.Message, ex.InnerException.Message } : new[] { ex.Message }; } return await Task.FromResult(new ResponseStandardDTO { Body = new { GeneralErrors = generalErrors }, Status = GB5Shared.DTO.Framework.Enum.FrameworkEnumDTO.ResponseStatus.Failed, Id = "", To = "", From = "", CacheLevel = CacheKeyLevel, ReplyTo = "", ETag = "", Last = "", LastLoginUsedTime = "", Next = "", Previous = "", ResponseObject = " ", Total = 0, ValidToTime = "", ValidityOfSession = "", }); } public static async Task> ContollerExceptionError(Exception ex, int CacheKeyLevel, LoginDTO LoginDTO, string message = "An error occurred", int statusCode = 500) { IGB5CommonFunction commonFunction = new GB5Shared.GB5CommonFunction.GB5CommonFunction(); var errorData = new { GeneralErrors = new[] { ex.Message } }; //int? userId = LoginDTO?.UserId; //object responseBody; //if (userId == null || userId.Value == 100) //{ // responseBody = errorData; //} //else //{ // string compressed = await commonFunction.GZipCompress(errorData); // responseBody = compressed; //} return await Task.FromResult(new ControllerResponseDTO { Body = new { GeneralErrors = new[] { ex.Message } }, Status = statusCode, Id = "", To = "", From = "", CacheLevel = CacheKeyLevel, ReplyTo = "", ETag = "", Last = "", LastLoginUsedTime = "", Next = "", Previous = "", ResponseObject = " ", Total = 0, ValidToTime = "", ValidityOfSession = "", }); } public static async Task> CreateExceptionErrorGZipCompress(Exception ex, int CacheKeyLevel, LoginDTO LoginDTO, string message = "An error occurred", int statusCode = 500) { IGB5CommonFunction commonFunction = new GB5Shared.GB5CommonFunction.GB5CommonFunction(); var errorData = new { GeneralErrors = new[] { ex.Message } }; //object responseBody; //string compressed = await commonFunction.GZipCompress(errorData); //responseBody = compressed; return await Task.FromResult(new ResponseStandardDTO { Body = new { GeneralErrors = new[] { ex.Message } }, Status = GB5Shared.DTO.Framework.Enum.FrameworkEnumDTO.ResponseStatus.Failed, Id = "", To = "", From = "", CacheLevel = CacheKeyLevel, ReplyTo = "", ETag = "", Last = "", LastLoginUsedTime = "", Next = "", Previous = "", ResponseObject = " ", Total = 0, ValidToTime = "", ValidityOfSession = "", }); } public static async Task> CreateErrorResponse(string message,int CacheKeyLevel, LoginDTO loginDTO, int statusCode = 400) { return await Task.FromResult(new ResponseStandardDTO { Body = message, // Ensuring ErrorDTO is inside Body Status = GB5Shared.DTO.Framework.Enum.FrameworkEnumDTO.ResponseStatus.Failed, Id = "", To = "", From = "", CacheLevel = CacheKeyLevel, ReplyTo = "", ETag = "", Last = "", LastLoginUsedTime = "", Next = "", Previous = "", ResponseObject = " ", Total = 0, ValidToTime = "", ValidityOfSession = "", }); } public static async Task> ControllerErrorResponse(string message, int CacheKeyLevel, int statusCode = 400) { return await Task.FromResult(new ControllerResponseDTO { Body = message, // Ensuring ErrorDTO is inside Body Status = statusCode, Id = "", To = "", From = "", CacheLevel = CacheKeyLevel, ReplyTo = "", ETag = "", Last = "", LastLoginUsedTime = "", Next = "", Previous = "", ResponseObject = " ", Total = 0, ValidToTime = "", ValidityOfSession = "", }); } public static async Task> CreateErrorResponseGZipCompress(string message, int CacheKeyLevel, int statusCode = 400) { IGB5CommonFunction commonFunction = new GB5Shared.GB5CommonFunction.GB5CommonFunction(); //object responseBody; //string compressed = await commonFunction.GZipCompress(message); //responseBody = compressed; return await Task.FromResult(new ResponseStandardDTO { Body = message, // Ensuring ErrorDTO is inside Body Status = GB5Shared.DTO.Framework.Enum.FrameworkEnumDTO.ResponseStatus.Failed, Id = "", To = "", From = "", CacheLevel = CacheKeyLevel, ReplyTo = "", ETag = "", Last = "", LastLoginUsedTime = "", Next = "", Previous = "", ResponseObject = " ", Total = 0, ValidToTime = "", ValidityOfSession = "", }); } public static async Task> CreateEmptyResponse(int CacheKeyLevel ,string message = "No data found", int statusCode = 404) { //ErrorDTO errorDTO = new() //{ // Body = message, // InnerException = "No inner exception", // ErrorNumber = statusCode.ToString() //}; return await Task.FromResult(new ResponseStandardDTO { Body = message, // Ensuring ErrorDTO is wrapped inside Body Status = GB5Shared.DTO.Framework.Enum.FrameworkEnumDTO.ResponseStatus.Failed, Id = "", To = "", From = "", CacheLevel = CacheKeyLevel , ReplyTo = "", ETag = "", Last = "", LastLoginUsedTime = "", Next = "", Previous = "", ResponseObject = " ", Total = 0, ValidToTime = "", ValidityOfSession = "", }); } public static async Task> ControllerSuccessResponse(T data, int CacheKeyLevel, LoginDTO LoginDTO, string message = "Operation succeeded", int statusCode = 200) { IGB5CommonFunction CommonFunction = new GB5Shared.GB5CommonFunction.GB5CommonFunction(); //int? userId = LoginDTO?.UserId; //object responseBody; //if (userId == null || userId.Value == 100) //{ // responseBody = data!; //} //else //{ // string compressed = await CommonFunction.GZipCompress(data); // responseBody = compressed; //} return await Task.FromResult(new ControllerResponseDTO { Body = data!, // Ensuring SuccessDTO is inside Body Status = statusCode, Id = "", To = "", From = "", CacheLevel = CacheKeyLevel, ReplyTo = "", ETag = "", Last = "", LastLoginUsedTime = "", Next = "", Previous = "", ResponseObject = " ", Total = 0, ValidToTime = "", ValidityOfSession = "", }); } } }