Exemple #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 < 7)
            {
                errorsCollection.ErrorMessages.Add("The password cannot be shorter than 7 characters !");
            }
            if (errorsCollection.ErrorMessages.Count > 0)
            {
                return(errorsCollection);
            }
            return(null);
        }
Exemple #2
0
        public ErrorsCollection Validate(UserUserRolePostModel userUserRolePostModel, ExpensesDbContext context)
        {
            ErrorsCollection errorsCollection = new ErrorsCollection {
                Entity = nameof(UserUserRolePostModel)
            };

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

            if (!userRoles.Contains(userUserRolePostModel.UserRoleName))
            {
                errorsCollection.ErrorMessages.Add($"The UserRole {userUserRolePostModel.UserRoleName} does not exist!");
            }
            if (errorsCollection.ErrorMessages.Count > 0)
            {
                return(errorsCollection);
            }
            return(null);
        }