using Govor.API.Common.Mapping; using Govor.API.Common.SignalR.Helpers; using Govor.API.Hubs.Infrastructure; using Govor.Application.Infrastructure.AdminsStuff; using Govor.Application.Infrastructure.Extensions; using Govor.Application.Infrastructure.Validators; using Govor.Application.Interfaces; using Govor.Application.Interfaces.Authentication; using Govor.Application.Interfaces.Friends; using Govor.Application.Interfaces.Infrastructure.Extensions; using Govor.Application.Interfaces.Medias; using Govor.Application.Interfaces.Messages; using Govor.Application.Interfaces.PushNotifications; using Govor.Application.Interfaces.UserOnlineStatus; using Govor.Application.Interfaces.UserSession; using Govor.Application.Interfaces.UserSession.Crypto; using Govor.Application.Services; using Govor.Application.Services.Authentication; using Govor.Application.Services.Friends; using Govor.Application.Services.Medias; using Govor.Application.Services.Messages; using Govor.Application.Services.PushNotifications; using Govor.Application.Services.PushNotifications.Providers; using Govor.Application.Services.UserOnlineStatus; using Govor.Application.Services.UserSessions; using Govor.Application.Services.UserSessions.Crypto; using Govor.Core.Infrastructure.Extensions; using Govor.Core.Infrastructure.Validators; using Govor.Core.Models; using Govor.Core.Models.Messages; using Govor.Core.Models.Users; using Govor.Core.Repositories.Admins; using Govor.Core.Repositories.Friendships; using Govor.Core.Repositories.Groups; using Govor.Core.Repositories.Invaites; using Govor.Core.Repositories.MediasAttachments; using Govor.Core.Repositories.Messages; using Govor.Core.Repositories.PrivateChats; using Govor.Core.Repositories.PushTokens; using Govor.Core.Repositories.Users; using Govor.Core.Repositories.UserSessionsRepository; using Govor.Data; using Govor.Data.Repositories; using Microsoft.EntityFrameworkCore; namespace Govor.API.Common.Extensions; public static class ConfigurationProgramExtensions { public static void AddServices(this IServiceCollection services) { services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); // Friends services services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(sp => { var env = sp.GetRequiredService(); return new LocalStorageService(env.ContentRootPath); }); services.AddHttpContextAccessor(); // it's very important for CurrentUserService services.AddScoped(); services.AddScoped(); services.AddMemoryCache(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); // UserSession services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddSingleton(); // Hubs Infrastructure services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddSingleton(); // Pushs services.AddScoped(); services.AddSingleton(); // Auto Mapper services.AddAutoMapper(typeof(MappingProfile)); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); } public static void AddRepositories(this IServiceCollection services) { services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); // other } public static void AddValidators(this IServiceCollection services) { services.AddScoped, UserValidator>(); services.AddScoped, MessageValidator>(); services.AddScoped, MediaAttachmentsValidator>(); services.AddScoped, AdminValidator>(); services.AddScoped, InvitationValidator>(); services.AddScoped, FriendshipValidator>(); services.AddScoped, PrivateChatValidator>(); services.AddScoped, ChatGroupValidator>(); } public static void AddGovorDbContext(this IServiceCollection services, IConfiguration configuration) { var useMySql = configuration.GetValue("UseMySql"); if (useMySql) { services.AddDbContext(options => { var connectionString = configuration.GetConnectionString(nameof(GovorDbContext)); options.UseMySql( connectionString, new MySqlServerVersion(new Version(8, 0, 21)), mySqlOptions => { mySqlOptions.EnableRetryOnFailure( maxRetryCount: 5, maxRetryDelay: TimeSpan.FromSeconds(5), errorNumbersToAdd: null); }); options.EnableSensitiveDataLogging(); options.EnableDetailedErrors(); }); } else { services.AddDbContext(options => { options.UseNpgsql( configuration.GetConnectionString(nameof(GovorDbContext)), npgsqlOptions => { // retry for transient failures npgsqlOptions.EnableRetryOnFailure( maxRetryCount: 5, maxRetryDelay: TimeSpan.FromSeconds(5), errorCodesToAdd: null); }); //options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); options.EnableSensitiveDataLogging(); options.EnableDetailedErrors(); }); } } }