示例#1
0
        public ErrorsCollection Validate(RegisterPostModel registerPostModel, ExpensesDbContext context)
        {
            ErrorsCollection errorsCollection = new ErrorsCollection {
                Entity = nameof(RegisterPostModel)
            };
            User existing = context.Users.FirstOrDefault(u => u.Username == registerPostModel.Username);

            if (existing != null)
            {
                errorsCollection.ErrorMessages.Add($"The username {registerPostModel.Username} is already taken!");
            }
            if (registerPostModel.Password.Length < 6)
            {
                errorsCollection.ErrorMessages.Add("The password cannot be shorter than 6 characters!");
            }
            int numberOfDigits = 0;

            foreach (char c in registerPostModel.Password)
            {
                if (c >= '0' && c <= '9')
                {
                    numberOfDigits++;
                }
            }
            if (numberOfDigits < 2)
            {
                errorsCollection.ErrorMessages.Add("The password must contain at least two digits!");
            }
            if (errorsCollection.ErrorMessages.Count > 0)
            {
                return(errorsCollection);
            }
            return(null);
        }
示例#2
0
        public ErrorsCollection Validate(UserRole userRole, ExpensesDbContext context)
        {
            ErrorsCollection errorsCollection = new ErrorsCollection {
                Entity = nameof(UserRole)
            };

            var existing = context.UserRole.FirstOrDefault(userRole1 => userRole1.Name == userRole.Name);

            if (existing == null)
            {
                errorsCollection.ErrorMessages.Add("The role does not exist !");
            }
            if (errorsCollection.ErrorMessages.Count > 0)
            {
                return(errorsCollection);
            }
            return(null);
        }