public async Task <bool> UpdateUserAsync(UpdateUserDto updateUserDto) { using (var scope = _dbContextScopeFactory.Create()) { // update user var createUser = await _userRepository.FindAsync( f => f.Id == updateUserDto.UserId); if (createUser == null) { throw new Exception("User not found !"); } else { createUser.Update(updateUserDto.FullName, updateUserDto.Gender, updateUserDto.Email, updateUserDto.Mobile, updateUserDto.Address, updateUserDto.HomePhone, updateUserDto.Ext, updateUserDto.BirthDate, updateUserDto.UserCode, null, updateUserDto.Language, updateUserDto.UserIndex); } // remove if (updateUserDto.RemovedDepartments != null) { foreach (var removeDeptId in updateUserDto.RemovedDepartments) { var delDept = _userDepartmentRepository.Get (w => w.UserID == updateUserDto.UserId && w.DeptID == removeDeptId) .FirstOrDefault(); _userDepartmentRepository.Delete(delDept); } } if (updateUserDto.AddedDepartments != null) { // create user depart foreach (var deptId in updateUserDto.AddedDepartments) { var createUserDept = _userDepartmentRepository .Get(w => w.UserID == createUser.Id && w.DeptID == deptId) .FirstOrDefault(); if (createUserDept == null) { createUserDept = UserDepartment.CreateUserDepartment( createUser.Id, deptId, updateUserDto.JobDescription, updateUserDto.OrderNumber, updateUserDto.JobTitleId, updateUserDto.IsManager); _userDepartmentRepository.Add(createUserDept); } else { createUserDept.Update(updateUserDto.JobDescription, updateUserDto.OrderNumber, updateUserDto.JobTitleId, updateUserDto.IsManager); } } } if (updateUserDto.RemovedUserSignatures != null && updateUserDto.RemovedUserSignatures.Count > 0) { foreach (var removeSignatureId in updateUserDto.RemovedUserSignatures) { var removeSign = await _signatureRepository.FindAsync(f => f.Id == removeSignatureId); _signatureRepository.Delete(removeSign); } } // create signature if (updateUserDto.AddedUserSignatures != null && updateUserDto.AddedUserSignatures.Count > 0) { foreach (var signature in updateUserDto.AddedUserSignatures) { var createSignature = UserSignature.CreateUserSignature( signature.SignServerId, createUser.Id, signature.Title, signature.CertificateBin, signature.SignImage, signature.SignImageType, signature.StampImage, signature.StampImageType, signature.Account, signature.Password); _signatureRepository.Add(createSignature); } } // create notification setting //if (updateUserDto.NotificationSettings != null && updateUserDto.NotificationSettings.Count > 0) //{ // foreach (var notifySetting in updateUserDto.NotificationSettings) // { // var createMotifySetting = NotificationSetting.Create(createUser.Id, // notifySetting.NotificationTypeId, notifySetting.IsUrgent); // _notificationSettingRepository.Add(createMotifySetting); // } //} await scope.SaveChangesAsync(); return(true); } }