public void installServices(IServiceCollection services, IConfiguration configuration)
        {
            services.AddSingleton(new ContextProvider(
                                      Environment.GetEnvironmentVariable("DB_CONNECTION_STRING")
                                      ?? throw new ArgumentNullException()
                                      ));

            // Valid emails, that mapped from appsettings.json
            var validEmails = new ValidEmails();

            configuration.Bind(nameof(ValidEmails), validEmails);
            services.AddSingleton(validEmails);

            // Valid platforms, that mapped from appsettings.json
            var validPlatforms = new ValidPlatforms();

            configuration.Bind(nameof(ValidPlatforms), validPlatforms);
            services.AddSingleton(validPlatforms);

            // Validators
            services.AddTransient <IValidator <ActivateRequest>, ActivateRequestValidator>();
            services.AddTransient <IValidator <RegistrationRequest>, RegistrationRequestValidator>();
            services.AddTransient <IValidator <LoginRequest>, LoginRequestValidation>();
            services.AddTransient <IValidator <RefreshTokenRequest>, RefreshRequestValidator>();

            // CQRS
            services.AddTransient <IQueryHandler <AuthQuery, User>, AuthQueryHandler>();
            services.AddTransient <IQueryHandler <UserByEmailQuery, User>, UserByEmailHandler>();
            services.AddTransient <IQueryHandler <UserByLoginQuery, User>, UserByLoginHandler>();
            services.AddTransient <IQueryHandler <TokenQuery, Token>, TokenQueryHandler>();

            services.AddTransient <ICommandHandler <AddUserCommand>, AddUserCommandHandler>();
            services.AddTransient <ICommandHandler <ConfirmEmailCommand>, ConfirmEmailCommandHandler>();
            services.AddTransient <ICommandHandler <WriteTokenCommand>, WriteTokenCommandHandler>();

            // RabbitMQ connection factory
            var rabbitHost = Environment.GetEnvironmentVariable("RABBIT_HOST")
                             ?? throw new ArgumentNullException();
            var rabbitPort = int.Parse(Environment.GetEnvironmentVariable("RABBIT_PORT")
                                       ?? throw new ArgumentNullException());
            var rabbitUser = Environment.GetEnvironmentVariable("RABBIT_USER")
                             ?? throw new ArgumentNullException();
            var rabbitPassword = Environment.GetEnvironmentVariable("RABBIT_PASSWORD")
                                 ?? throw new ArgumentNullException();

            services.AddSingleton <IEventBus>(new RabbiBus(new ConnectionFactory
            {
                HostName = rabbitHost,
                Port     = rabbitPort,
                UserName = rabbitUser,
                Password = rabbitPassword,
                DispatchConsumersAsync = true,
            }, services.BuildServiceProvider()));
        }
Esempio n. 2
0
        public RegistrationRequestValidator(ValidEmails validEmails, ValidPlatforms validPlatforms)
        {
            RuleFor(x => x.Email)
            .NotNull()
            .MaximumLength(50)
            .Must(x => validEmails.Emails.FirstOrDefault(x.EndsWith) != null)
            .WithMessage($"Email is not valid. Valid emails: {string.Join(',', validEmails.Emails)}");

            RuleFor(x => x.Login)
            .NotNull()
            .MinimumLength(5)
            .MaximumLength(20);

            RuleFor(x => x.Password)
            .NotNull()
            .MinimumLength(5)
            .MaximumLength(40);

            RuleFor(x => x.Platform)
            .NotNull()
            .Must(validPlatforms.Platforms.Contains)
            .WithMessage("Platform must br one of " + string.Join(',', validPlatforms.Platforms));
        }
Esempio n. 3
0
 public void AddValidEmail(int index)
 {
     ValidEmails.Add(index);
 }