public async Task AddAsync(UserDomain user) { if (user == null) { throw new ArgumentNullException(nameof(user)); } if (user.Password != user.ConfirmPassword) { throw new InvalidOperationException("Password and confirmation password do not match"); } if (string.IsNullOrEmpty(user.Img)) { user.Img = ConfigurationManager.AppSettings["DefaultImageUrl"]; } var newUser = new ApplicationUser { Password = PasswordHasher.HashPassword(user.Password), Email = user.Email, UserName = user.UserName, Img = user.Img, Language = await _languageRepository.GetByLanguageTypeAsync(user.Language.LanguageType) }; _authContext.Users.Add(newUser); await _authContext.SaveChangesAsync(); }
public async Task UpdateAsync(UserDomain user) { if (user == null) { throw new ArgumentNullException(nameof(user)); } if (user.Password != user.ConfirmPassword) { throw new InvalidOperationException("Password and confirmation password do not match"); } var existingUser = await GetByIdAsync(user.Id); if (existingUser == null) { throw new InvalidOperationException("user is not found"); } var isVerifiedPassword = PasswordHasher .VerifyHashedPassword(existingUser.Password, user.OldPassword); if (!isVerifiedPassword) { throw new InvalidOperationException("Incorrect password"); } if (user.Img != null && !user.Img.IsUrl()) { existingUser.Img = await ImageUploader.UploadAsync(user.Img); } existingUser.Email = user.Email; existingUser.UserName = user.UserName; existingUser.Password = PasswordHasher.HashPassword(user.Password); existingUser.Language = await _languageRepository.GetByLanguageTypeAsync(user.Language.LanguageType); await _authContext.SaveChangesAsync(); }