Exemple #1
0
        public async Task <IEnumerable <User> > Get(UserValidateOptions options)
        {
            try
            {
                StringBuilder sql = new StringBuilder();

                _logger.LogInformation("Try to create get users sql query");

                sql.AppendLine(@"
                    select 
                        Id,
                        UserName,
                        FirstName,
                        LastName,
                        Email,
                        ConfirmCode
                    from [User]
                ");

                int conditionIndex = 0;

                if (options.Id.HasValue)
                {
                    sql.AppendLine($"{(conditionIndex++ == 0 ? "where" : "and")} Id <> @Id");
                }

                if (!string.IsNullOrEmpty(options.UserName))
                {
                    sql.AppendLine($"{(conditionIndex++ == 0 ? "where" : "and")} UserName = @UserName");
                }

                if (!string.IsNullOrEmpty(options.Email))
                {
                    sql.AppendLine($"{(conditionIndex++ == 0 ? "where" : "and")} Email = @Email");
                }

                _logger.LogInformation($"Sql query successfully created:\n{sql.ToString()}");

                _logger.LogInformation("Try to execute sql get users query");
                var result = await QueryAsync <User>(sql.ToString(), options);

                _logger.LogInformation("Sql get users query successfully executed");
                return(result);
            }
            catch (Exception exception)
            {
                _logger.LogError(exception.Message);
                throw exception;
            }
        }
        public async Task <string> Validate(UserValidateOptions options)
        {
            try
            {
                _logger.LogInformation("Start user name and email validating.");

                string result = ValidateUserName(options.UserName);
                if (!string.IsNullOrEmpty(result))
                {
                    return(result);
                }

                if (!options.Id.HasValue)
                {
                    result = ValidatePassword(options.Password);
                    if (!string.IsNullOrEmpty(result))
                    {
                        return(result);
                    }
                }

                result = ValidateEmail(options.Email);
                if (!string.IsNullOrEmpty(result))
                {
                    return(result);
                }

                var users = await _dao.Get(options);

                if (users != null && users.Count() > 0)
                {
                    string message = "User with same user name or email have been already created. Please try another or try to sign in.";
                    _logger.LogInformation(message);
                    return(message);
                }

                _logger.LogInformation("User name and email successfuly validated.");
                return(null);
            }
            catch (Exception exception)
            {
                _logger.LogError(exception.Message);
                throw exception;
            }
        }
        public async Task <IActionResult> ValidateUser([FromQuery] UserValidateOptions options)
        {
            var result = await _userService.Validate(options);

            return(Ok(result));
        }