private async Task <ProcessResult <bool> > CategoryViewModelIsValid(CategoryViewModel viewModel, int existingCategoryId = 0) { ProcessResult <bool> result = new ProcessResult <bool>(); PhotoCategory category = await _db .PhotoCategories .FirstOrDefaultAsync(p => p.CategoryName.ToUpper() == viewModel.CategoryName.ToUpper() && p.CategoryId != existingCategoryId); if (category != null) { result.AddFieldError("CategoryName", "You dummy. A category with name has already been created"); } if (!CategoryStatusCodes.GetAll().Contains(viewModel.StatusCode)) { result.AddFieldError("StatusCode", "Invalid status code"); } if (!PhotoNamingThemes.ThemeCodes.GetAll().Contains(viewModel.PhotoNamingThemeCode)) { result.AddFieldError("PhotoNamingThemeCode", "Invalid theme code"); } if (existingCategoryId != 0) { category = await _db .PhotoCategories .FirstOrDefaultAsync(p => p.CategoryId == existingCategoryId); if (category == null) { result.AddError("This category cannot be found"); } } return(result); }
private async Task <ProcessResult <bool> > UserIdentityDetailsAreUniqueAsync(UserViewModel viewModel, string existingUserName = "") { ProcessResult <bool> result = new ProcessResult <bool>(); // User name is unique ApplicationUser userByUserName = await _userManager.FindByNameAsync(viewModel.UserName); // Attempting to add an existing user name if (userByUserName != null && string.IsNullOrEmpty(existingUserName)) { result.AddFieldError("UserName", $"Copy cat. User name '{viewModel.UserName}' already exists"); } if (userByUserName == null && !string.IsNullOrEmpty(existingUserName)) { result.AddFieldError("UserName", $"Copy cat. User with user name '{viewModel.UserName}' cannot be found"); } ApplicationUser userByEmail = await _userManager.FindByEmailAsync(viewModel.Email); // Attempting to add an email of the same address if (userByEmail != null && string.IsNullOrEmpty(existingUserName)) { result.AddFieldError("Email", $"Copy cat. Email address '{viewModel.Email}' already exists for another user"); } if (userByEmail != null && !string.IsNullOrEmpty(existingUserName) && viewModel.UserName != userByEmail.UserName) { result.AddFieldError("Email", $"Copy cat. Email address '{viewModel.Email}' already exists for another user"); } //TODO: Inject this into the controller ApplicationUser userByNickname = _db.Users.FirstOrDefault(u => u.Nickname.ToUpper() == viewModel.Nickname.ToUpper()); if (userByNickname != null && string.IsNullOrEmpty(existingUserName)) { result.AddFieldError("Nickname", "Copy cat. Nickname already exists for another user"); } if (userByNickname != null && !string.IsNullOrEmpty(existingUserName) && userByNickname.UserName != viewModel.UserName) { result.AddFieldError("Nickname", "Copy cat. Nickname already exists for another user"); } return(result); }