예제 #1
0
        public static void ThrowIsNotValid(string message, ValidationResult validationResult)
        {
            if (validationResult.IsValid)
            {
                return;
            }

            var details = new Dictionary <string, IList <string> >();

            foreach (var error in validationResult.Errors)
            {
                if (details.ContainsKey(error.PropertyName))
                {
                    details[error.PropertyName].Add(error.ErrorMessage);
                }
                else
                {
                    details[error.PropertyName] = new List <string> {
                        error.ErrorMessage
                    }
                };
            }

            var ex = new CommandHandlerException(message, details);

            throw ex;
        }
    }
예제 #2
0
        public async Task ExecuteAsync(CreateAccount cmd)
        {
            var validator        = new CreateAccountValidator(_context);
            var validationResult = validator.Validate(cmd);

            CommandHandlerException.ThrowIsNotValid("Ошибочные значения параметров", validationResult);

            var existsAccount = await _context.Accounts.Where(a => a.IP == cmd.IP)
                                .OrderByDescending(a => a.CreatedAt)
                                .FirstOrDefaultAsync();

            var currentDateTime = DateTime.Now;
            var allowedInterval = _config.Value.AllowedIntervalInSeconds;

            if (existsAccount != null && currentDateTime.Subtract(existsAccount.CreatedAt).Seconds < allowedInterval)
            {
                var ex = new CommandHandlerException(403, $"С одного IP разрешено регистрироваться не более одного раза в {allowedInterval} сек.");
                _loggerService.LogError(ex, ex.Message);
                throw ex;
            }

            var account = new Account(currentDateTime,
                                      cmd.IP,
                                      cmd.Email.ToLower(),
                                      _passwordService.GetSha256Hash(cmd.Password),
                                      cmd.Country,
                                      cmd.Province);

            _context.Accounts.Add(account);

            await _context.SaveChangesAsync();

            _domainEventDispatcher.Raise(new CreatedAccountEvent(account));
        }