namespace Indotalent.ConfigBackEnd.Extensions; using Indotalent.Shared.Models; using Microsoft.AspNetCore.Http; using System.Text.Json; public static class GenericExtensions { public static bool IsNullOrEmpty(this IEnumerable? enumerable) { return enumerable == null || !enumerable.Any(); } public static T? ToObject(this string json) { if (string.IsNullOrWhiteSpace(json)) return default; return JsonSerializer.Deserialize(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); } public static string ToJson(this T obj) { return JsonSerializer.Serialize(obj); } public static TDestination MapTo(this object source) where TDestination : new() { var json = JsonSerializer.Serialize(source); return JsonSerializer.Deserialize(json) ?? new TDestination(); } public static string ToCurrency(this decimal value, string culture = "id-ID") { return value.ToString("C0", new System.Globalization.CultureInfo(culture)); } public static bool IsBetween(this T value, T low, T high) where T : IComparable { return value.CompareTo(low) >= 0 && value.CompareTo(high) <= 0; } public static IResult ToApiResponse(this PagedList result) { return Results.Ok(new ApiResponse> { IsSuccess = true, StatusCode = 200, Value = result.Value, Pagination = new PaginationMetadata { Count = result.Count, Top = result.Top, Skip = result.Skip }, ServerTime = DateTime.UtcNow }); } public static IResult ToApiResponse(this T? result, string? message = null, int statusCode = 200) { if (result == null) { return Results.NotFound(new ApiResponse { IsSuccess = false, StatusCode = 404, Message = message ?? "Data not found", ServerTime = DateTime.UtcNow }); } return Results.Ok(new ApiResponse { IsSuccess = true, StatusCode = statusCode, Message = message, Value = result, Pagination = null, ServerTime = DateTime.UtcNow }); } }