Ejemplo n.º 1
0
        public ErrorsCollection ValidateRole(RoleIdModel roleIdModel, MoviesDbContext context)
        {
            ErrorsCollection errors = new ErrorsCollection
            {
                Entity = nameof(RolePostModel)
            };

            Role role = context.Roles.FirstOrDefault(r => r.Id == roleIdModel.RoleId);

            if (role == null)
            {
                errors.ErrorMessages.Add($"The role {roleIdModel.RoleId} does not exist!");
            }
            return(null);
        }
Ejemplo n.º 2
0
        public ErrorsCollection Validate(RegisterPostModel registerPostModel, MoviesDbContext 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);
        }