using PostFundManagement.Application.Abstractions; using PostFundManagement.Application.Abstractions.Services; using PostFundManagement.Application.Services.Communications; using PostFundManagement.Application.Services.Evidence; using PostFundManagement.Application.Services.Shared; using PostFundManagement.Application.Services.Users; using PostFundManagement.Domain.Abstractions; namespace PostFundManagement.Application.Extensions; public static class General { public static IServiceCollection AddRegisterServicesWithCaching(this IServiceCollection services, IConfiguration configuration) { services.AddScoped(); services.Decorate(); services.AddScoped(); services.Decorate(); services.AddScoped(); services.Decorate(); services.AddScoped(); services.Decorate(); services.AddStackExchangeRedisCache(options => options.Configuration = configuration.GetConnectionString("Redis")); services.AddHybridCache(options => options.DefaultEntryOptions = new HybridCacheEntryOptions { Expiration = TimeSpan.FromMinutes(2), LocalCacheExpiration = TimeSpan.FromMinutes(1) }); return services; } public static IServiceCollection AddSharedServices(this IServiceCollection services) { var serviceType = typeof(IService); var sharedImplementations = typeof(IApplication).Assembly.GetTypes() .Where(t => serviceType.IsAssignableFrom(t) && t.IsClass && !t.IsAbstract); foreach (var sharedImplementation in sharedImplementations) services.AddScoped(sharedImplementation); return services; } public static Guid GetUserId(this HttpContext context) => (Guid)context.Items[Domain.Extensions.Constants.UserIdItemKey]!; public static string GetUserEmail(this HttpContext context) => (string)context.Items[Domain.Extensions.Constants.EmailtemKey]!; public static (bool Decoded, Guid userId) GetUserId(this ClaimsPrincipal principal) { var subClaim = principal.FindFirst(JwtRegisteredClaimNames.Sub)?.Value ?? principal.FindFirst(ClaimTypes.NameIdentifier)?.Value; var decoded = Guid.TryParse(subClaim, out var userId); return (decoded, userId); } public static (bool Decoded, string email) GetUserEmail(this ClaimsPrincipal principal) { var emailClaim = principal.FindFirst("email")?.Value ?? principal.FindFirst(ClaimTypes.Email)?.Value; var decoded = !string.IsNullOrWhiteSpace(emailClaim); return (decoded, emailClaim!); } public static Microsoft.AspNetCore.Mvc.ProblemDetails ToProblems(this IReadOnlyList errors) => new() { Detail = string.Join(',', errors.Select(e => e.Message)), Status = StatusCodes.Status400BadRequest, Title = errors[0]?.Message, }; }