Exemplo n.º 1
0
        public override async Task <int> HandleCommand(AddCommand request, CancellationToken cancellationToken)
        {
            if (request.Role == null || string.IsNullOrEmpty(request.Role.Name))
            {
                throw new BusinessException("Common.WrongInput");
            }

            var checkingRole = await roleQueries.GetByName(request.Role.Name);

            if (checkingRole != null)
            {
                throw new BusinessException("User.ExistedRole");
            }

            var roleId = -1;

            using (var conn = DalHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        roleRepository.JoinTransaction(conn, trans);
                        rolePermissionRepository.JoinTransaction(conn, trans);

                        request.Role.IsExternalRole = false;
                        request.Role = CreateBuild(request.Role, request.LoginSession);
                        roleId       = await roleRepository.Add(request.Role);

                        foreach (var item in request.Role.RolePermissions)
                        {
                            item.RoleId = roleId;
                            await rolePermissionRepository.AddOrUpdate(item);
                        }
                    }
                    finally
                    {
                        if (roleId > 0)
                        {
                            trans.Commit();
                        }
                        else
                        {
                            try
                            {
                                trans.Rollback();
                            }
                            catch (Exception ex)
                            {
                                LogHelper.GetLogger().Error(ex);
                            }
                        }
                    }
                }
            }

            return(roleId);
        }
Exemplo n.º 2
0
        public override async Task <int> HandleCommand(UpdateCommand request, CancellationToken cancellationToken)
        {
            if (request.Role == null || request.Role.Id == 0)
            {
                throw new BusinessException("Common.WrongInput");
            }
            var role = await roleQueries.Get(request.Role.Id);

            if (role == null)
            {
                throw new BusinessException("User.NotExistedRole");
            }

            var checkingRole = await roleQueries.GetByName(request.Role.Name);

            if (checkingRole != null && checkingRole.Id != request.Role.Id)
            {
                throw new BusinessException("User.ExistedRole");
            }

            var rs = -1;

            using (var conn = DalHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        roleRepository.JoinTransaction(conn, trans);
                        rolePermissionRepository.JoinTransaction(conn, trans);

                        role.Name        = request.Role.Name;
                        role.Description = request.Role.Description;
                        role.IsActive    = request.Role.IsActive;
                        role             = UpdateBuild(role, request.LoginSession);
                        var rsUp = await roleRepository.Update(role);

                        if (rsUp == -1)
                        {
                            return(rs);
                        }

                        foreach (var item in request.Role.RolePermissions)
                        {
                            item.RoleId = role.Id;
                            await rolePermissionRepository.AddOrUpdate(item);
                        }

                        rs = 0;
                    }
                    finally
                    {
                        if (rs == 0)
                        {
                            trans.Commit();
                        }
                        else
                        {
                            try
                            {
                                trans.Rollback();
                            }
                            catch (Exception ex)
                            {
                                LogHelper.GetLogger().Error(ex);
                            }
                        }
                    }
                }
            }

            return(rs);
        }