Ejemplo n.º 1
0
        public async Task <JsonResponse> DisRole([FromBody] DisRoleViewModel vm)
        {
            var user = await _userServices.QueryById(vm.UserId);

            if (user == null)
            {
                throw new UserOperationException("用户不存在");
            }
            _unitOfWork.Begin();
            var userRoles   = (await _userRoleServices.Query(p => p.UserId == vm.UserId)).ToList();
            var removeCount = 0;
            var addCount    = 0;

            foreach (var userRole in userRoles)
            {
                removeCount += await _userRoleServices.DeleteByIdAsync(userRole.Id);
            }
            foreach (var roleId in vm.RoleIds)
            {
                var userRoleId = await _userRoleServices.AddAsync(new SysUserRole { UserId = vm.UserId, RoleId = roleId });

                addCount += userRoleId > 0 ? 1 : 0;
            }
            if (!(removeCount == userRoles.Count && addCount == vm.RoleIds.Count))
            {
                _unitOfWork.Rollback();
                throw new UserOperationException("操作失败");
            }
            _unitOfWork.Commit();
            return(new JsonResponse(true));
        }