private async Task CreateOfficerAsync(OfficerCreateOrUpdateInput input) { var nationalCode = input.NationalCode.Replace("-", ""); var user = new User { IsActive = true, ShouldChangePasswordOnNextLogin = true, UserName = input.UserName, EmailAddress = input.UserName + "@mgnsys.ir", Name = input.Name, Surname = input.Family }; user.Password = _passwordHasher.HashPassword(user, nationalCode); CheckErrors(await UserManager.CreateAsync(user)); await CurrentUnitOfWork.SaveChangesAsync(); var officerRole = _roleManager.GetRoleByName(StaticRoleNames.Host.Officer); long userId = user.ToUserIdentifier().UserId; user.Roles = new List <UserRole>(); user.Roles.Add(new UserRole(null, user.Id, officerRole.Id)); if (userId > 0) { var officer = ObjectMapper.Map <Officer>(input); officer.UserId = userId; await _officerRepository.InsertAsync(officer); } else { throw new UserFriendlyException(L("AnErrorOccurred")); } }
public async Task CreateOrUpdateOfficer(OfficerCreateOrUpdateInput input) { if (input.Id.HasValue) { await UpdateOfficerAsync(input); } else { await CreateOfficerAsync(input); } }
private async Task UpdateOfficerAsync(OfficerCreateOrUpdateInput input) { var user = await UserManager.GetUserByIdAsync(input.UserId); user.Name = input.Name; user.Surname = input.Family; user.UserName = input.UserName; user.EmailAddress = input.UserName + "@mgnsys.ir"; CheckErrors(await UserManager.UpdateAsync(user)); await CurrentUnitOfWork.SaveChangesAsync(); var officer = ObjectMapper.Map <Officer>(input); await _officerRepository.UpdateAsync(officer); }