public async Task<GetUserForEditOutput> GetUserForEdit(NullableIdInput<long> input) { //Getting all available roles var userRoleDtos = (await _roleManager.Roles .OrderBy(r => r.DisplayName) .Select(r => new UserRoleDto { RoleId = r.Id, RoleName = r.Name, RoleDisplayName = r.DisplayName }) .ToArrayAsync()); var output = new GetUserForEditOutput { Roles = userRoleDtos }; if (!input.Id.HasValue) { //Creating a new user output.User = new UserEditDto { IsActive = true, ShouldChangePasswordOnNextLogin = true }; foreach (var defaultRole in await _roleManager.Roles.Where(r => r.IsDefault).ToListAsync()) { var defaultUserRole = userRoleDtos.FirstOrDefault(ur => ur.RoleName == defaultRole.Name); if (defaultUserRole != null) { defaultUserRole.IsAssigned = true; } } } else { //Editing an existing user var user = await UserManager.GetUserByIdAsync(input.Id.Value); output.User = user.MapTo<UserEditDto>(); output.ProfilePictureId = user.ProfilePictureId; foreach (var userRoleDto in userRoleDtos) { userRoleDto.IsAssigned = await UserManager.IsInRoleAsync(input.Id.Value, userRoleDto.RoleName); } } return output; }
public CreateOrEditUserModalViewModel(GetUserForEditOutput output) { output.MapTo(this); }