using System.Text.Json; using FastEndpoints; using FrameworkBLL.Region; using GB5Shared.DTO.Framework.Login; using GB5Shared.DTO.Framework.ResponseStandard; using GB5Shared.FastEndPoint; using GB5Shared.DateConverter; using static GB5Shared.GB5Constant.Constant; namespace FrameworkSL.Endpoints.Region { public class GetRegion : BaseEndpoint> { private readonly IRegionBLL _RegionBLL; public GetRegion(IRegionBLL RegionBLL) { _RegionBLL = RegionBLL; } public override void Configure() { Get("/Region/GetRegion"); AllowAnonymous(); } public record GetRegionParameters( [property: FromHeader] string Login, [property: QueryParam] int RegionId ); protected override string? GetCacheKey(GetRegionParameters GetRegionParameters, LoginDTO LoginDTO) { try { return KeyGenerator.KeyGeneration(GetRegionParameters.RegionId!, EntityConstant.OBJECTREGION, CacheKeyLevel.USER_LEVEL, LoginDTO); } catch (Exception) { throw; } } protected override async Task> ExecuteAsync( GetRegionParameters req, LoginDTO LoginDTO, CancellationToken ct) { try { // Get the region var region = await _RegionBLL.GetRegion(req.RegionId, LoginDTO); // Setup JsonSerializerOptions with GB5 converters var options = new JsonSerializerOptions { PropertyNamingPolicy = null }; options.AddGB5Converters(); // Serialize to JSON string string json = JsonSerializer.Serialize(region, options); // Deserialize back to object so Body remains an object // object body = JsonSerializer.Deserialize(json, options)!; // Return response return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse( json, CacheKeyLevel.USER_LEVEL, LoginDTO); } catch (Exception ex) { return await GB5Shared.ResponseStandard.Response.CreateExceptionError( ex, CacheKeyLevel.USER_LEVEL, LoginDTO, ex.Message, 500); } } } }