Example #1
0
        public ErrorsCollection Validate(UserUserRolePostModel userUserRolePostModel, TasksDbContext context)
        {
            ErrorsCollection errorsCollection = new ErrorsCollection {
                Entity = nameof(UserUserRolePostModel)
            };

            List <string> userRoles = context.UserRole
                                      .Select(userRole => userRole.Name)
                                      .ToList();

            if (!userRoles.Contains(userUserRolePostModel.UserRoleName))
            {
                errorsCollection.ErrorMessages.Add($"The userRole {userUserRolePostModel.UserRoleName} does not exists in Db!");
            }

            if (errorsCollection.ErrorMessages.Count > 0)
            {
                return(errorsCollection);
            }
            return(null);
        }
Example #2
0
        public ErrorsCollection Validate(RegisterPostModel registerPostModel, TasksDbContext 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 nrOfDigits = 0;

            foreach (char c in registerPostModel.Password)
            {
                if (c >= '0' && c <= '9')
                {
                    nrOfDigits++;
                }
            }

            if (nrOfDigits < 2)
            {
                errorsCollection.ErrorMessages.Add("The password must contain at least two digits!");
            }

            if (errorsCollection.ErrorMessages.Count > 0)
            {
                return(errorsCollection);
            }
            return(null);
        }