public async Task <Unit> Handle(Command message) { var userId = message.UserId; var dbUser = await _userManager.FindByIdAsync(userId.ToString()); _mapper.Map(message.UserDetailDto, dbUser); try { await _userManager.UpdateAsync(dbUser); await _userRolesUpdater.UpdateAsync(dbUser, message.UserDetailDto.RoleIds); await _userIcgsUpdater.UpdateAsync(dbUser, message.UserDetailDto.IcgsIds); await _userPasswordUpdater.UpdateAsync(dbUser, message.UserDetailDto.Password); } catch (Exception e) { throw new DatabaseException(e.InnerException?.Message ?? e.Message); } return(await Unit.Task); }
public async Task <UserDetailsDto> Handle(Command message) { if (await _userManager.FindByNameAsync(message.UserDetailsDto.Xin.Trim()) != null) { throw new ValidationException("user with same xin already registered"); } if (await _userManager.FindByEmailAsync(message.UserDetailsDto.Email.Trim()) != null) { throw new ValidationException("user with same email already registered"); } var userDto = message.UserDetailsDto; userDto.Id = 0; var newUser = _mapper.Map <UserDetailsDto, Domain.Entities.Security.ApplicationUser>(userDto); try { await _userManager.CreateAsync(newUser, userDto.Password.Trim()); await _userManager.SetLockoutEnabledAsync(newUser, false); await _userRolesUpdater.UpdateAsync(newUser, message.UserDetailsDto.RoleIds); await _userIcgsUpdater.UpdateAsync(newUser, message.UserDetailsDto.IcgsIds); } catch (Exception e) { throw new DatabaseException(e.InnerException?.Message ?? e.Message); } var userRoles = await _context.UserRoles .Where(ur => ur.UserId == newUser.Id) .ToListAsync(); return(_mapper.Map <UserDetailsDto>(newUser, options => options.Items["userRoles"] = userRoles)); }