public async Task InsertAsync(ModifyRoleDto role) { bool isRepeatName = await _roleRepo.Select.AnyAsync(r => r.Name == role.Name); if (isRepeatName)//角色名重复 { throw new KnownException("角色名称重复,请重新输入", ServiceResultCode.RepeatField); } var permissions = await _permissionRepo.Select.Where(p => p.IsDeleted == false).ToListAsync(); foreach (var permissionId in role.PermissionIds) { if (!permissions.Any(p => p.Id == permissionId)) { throw new KnownException($"Id:{permissionId} 权限不存在!", ServiceResultCode.NotFound); } } var input = Mapper.Map <RoleEntity>(role); var entity = await _roleRepo.InsertAsync(input); await _rolePermissionRepo.InsertAsync(role.PermissionIds.Select(p => new RolePermissionEntity { RoleId = entity.Id, PermissionId = p })); }
public async Task <IdentityResult> CreateAsync(Role role, CancellationToken token) { await _roleRepo.InsertAsync(role); return(IdentityResult.Success); }