public async Task <OperationResponse> UpdateRoleAsync(RoleInputDto dto) { dto.NotNull(nameof(dto)); var role = await _roleManager.FindByIdAsync(dto.Id.ToString()); role = dto.MapTo(role);//拿到所有角色的权限 return(await _roleMenuRepository.UnitOfWork.UseTranAsync(async() => { var result = await _roleManager.UpdateAsync(role); if (!result.Succeeded) { return result.ToOperationResponse(); } if (dto.MenuIds?.Any() == true) { var list = dto.MenuIds.Select(x => new RoleMenuEntity { MenuId = x.Value, RoleId = role.Id, }).ToArray(); int count = await _roleMenuRepository.DeleteBatchAsync(x => x.RoleId == role.Id); int insertcount = await _roleMenuRepository.InsertAsync(list); if (count <= 0 && insertcount <= 0) { return new OperationResponse("保存失败", OperationResponseType.Error); } await _eventBus?.PublishAsync(new RoleMenuCacheAddOrUpdateEvent() { RoleId = role.Id, MenuIds = dto.MenuIds.Select(o => o.Value), EventState = Flow.Events.EventState.Update }); } return new OperationResponse("保存成功", OperationResponseType.Success); })); }
/// <summary> /// 异步添加角色 /// </summary> /// <param name="dto"></param> /// <returns></returns> public async Task <OperationResponse> AddRoleAsync(RoleInputDto dto) { dto.NotNull(nameof(dto)); var role = dto.MapTo <Role>(); return(await _roleMenuRepository.UnitOfWork.UseTranAsync(async() => { var result = await _roleManager.CreateAsync(role); if (!result.Succeeded) { return result.ToOperationResponse(); } if (dto.MenuIds?.Any() == true) { var list = dto.MenuIds.Select(x => new RoleMenuEntity { MenuId = x.Value, RoleId = role.Id, }).ToArray(); int count = await _roleMenuRepository.InsertAsync(list); await _eventBus?.PublishAsync(new RoleMenuCacheAddOrUpdateEvent() { RoleId = role.Id, MenuIds = dto.MenuIds.Select(o => o.Value), EventState = Flow.Events.EventState.Add }); if (count <= 0) { return new OperationResponse("保存失败", OperationResponseType.Error); } } return new OperationResponse("保存成功", OperationResponseType.Success); })); }
/// <summary> /// 修改角色及分配权限 /// </summary> /// <param name="input"></param> /// <returns></returns> public async Task <OperationResponse> UpdateAsync(RoleInputDto input) { input.NotNull(nameof(input)); var role = await _roleManager.FindByIdAsync(input.Id.ToString()); role = input.MapTo(role); return((await _roleManager.UpdateAsync(role)).ToOperationResponse()); //return await _roleMenuRepository.UnitOfWork.UseTranAsync(async () => //{ // var result = await _roleManager.UpdateAsync(role); // if (!result.Succeeded) // { // return result.ToOperationResponse(); // } // if (input.MenuIds?.Any() == true) // { // await _roleMenuRepository.DeleteBatchAsync(x => x.RoleId == input.Id); // if (await _roleMenuRepository.InsertAsync(input.MenuIds.Select(x => new RoleMenuEntity // { // MenuId = x, // RoleId = role.Id, // }).ToArray()) <= 0) // { // return new OperationResponse(ResultMessage.InsertFail, Shared.Enums.OperationEnumType.Error); // } // } // return new OperationResponse(ResultMessage.InsertSuccess, OperationEnumType.Success); //}); }
/// <summary> /// 异步添加角色 /// </summary> /// <param name="dto"></param> /// <returns></returns> public async Task <OperationResponse> AddRoleAsync(RoleInputDto dto) { dto.NotNull(nameof(dto)); var role = dto.MapTo <Role>(); return(await _roleMenuRepository.UnitOfWork.UseTranAsync(async() => { var result = await _roleManager.CreateAsync(role); if (!result.Succeeded) { return result.ToOperationResponse(); } if (dto.MenuIds?.Any() == true) { var list = dto.MenuIds.Select(x => new RolemenuEntity { MenuId = x, RoleId = role.Id, }).ToArray(); int count = await _roleMenuRepository.InsertAsync(list); if (count <= 0) { return new OperationResponse("保存失败", OperationResponseType.Error); } } return new OperationResponse("保存成功", OperationResponseType.Success); })); }
/// <summary> /// 创建角色及分配权限 /// </summary> /// <param name="input"></param> /// <returns></returns> public async Task <OperationResponse> CreateAsync(RoleInputDto input) { input.NotNull(nameof(input)); var role = input.MapTo <RoleEntity>(); return(await _roleMenuRepository.UnitOfWork.UseTranAsync(async() => { var result = await _roleManager.CreateAsync(role); if (!result.Succeeded) { return result.ToOperationResponse(); } if (input.MenuIds?.Any() == true) { ; if (await _roleMenuRepository.InsertAsync(input.MenuIds.Select(x => new RoleMenuEntity { MenuId = x, RoleId = role.Id, }).ToArray()) <= 0) { return new OperationResponse(ResultMessage.InsertFail, Shared.Enums.OperationEnumType.Error); } } return new OperationResponse(ResultMessage.InsertSuccess, OperationEnumType.Success); })); }
public async Task <OperationResponse> AddRoleAsync(RoleInputDto dto) { dto.NotNull(nameof(dto)); var role = dto.MapTo <Role>(); var result = await _roleManager.CreateAsync(role); if (!result.Succeeded) { return(result.ToOperationResponse()); } return(new OperationResponse("添加角色成功", Enums.OperationResponseType.Success)); }
public async Task <OperationResponse> UpdateRoleAsync(RoleInputDto dto) { dto.NotNull(nameof(dto)); var role = await _roleManager.FindByIdAsync(dto.ToString()); role = dto.MapTo(role); var result = await _roleManager.UpdateAsync(role); if (!result.Succeeded) { return(result.ToOperationResponse()); } return(new OperationResponse("更新成功!", Enums.OperationResponseType.Success)); }