示例#1
0
        public override async Task <int> HandleCommand(DeleteAccountCommand request, CancellationToken cancellationToken)
        {
            Account account = null;

            if (request.Model == 0)
            {
                throw new BusinessException("Account.NotExisted");
            }
            else
            {
                account = await accountQueries.GetByIdAsync(request.Model);

                if (account == null)
                {
                    throw new BusinessException("Account.NotExisted");
                }
            }

            var rs = -1;

            using (var conn = DALHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        account.IsDeleted    = true;
                        account.ModifiedDate = DateTime.Now;
                        account.ModifiedBy   = request.LoginSession.Id;

                        if (await accountRepository.UpdateAsync(account) > 0)
                        {
                            rs = 0;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        if (rs == 0)
                        {
                            trans.Commit();
                        }
                        else
                        {
                            try { trans.Rollback(); } catch { }
                        }
                    }
                }
            }
            return(rs);
        }
示例#2
0
        public override async Task <AccountSingleRole> HandleCommand(GetByIdAccountPartnerCommand request, CancellationToken cancellationToken)
        {
            var company = await companyQueries.GetByUserIdAsync(request.LoginSession.Id);

            if (company == null)
            {
                throw new BusinessException("Common.NoPermission");
            }

            var rs = await accountQueries.GetByIdAsync(request.Model);

            if (rs == null)
            {
                throw new BusinessException("Account.NotExist");
            }

            return(rs);
        }
示例#3
0
        public override async Task <int> HandleCommand(UpdateAccountPartnerCommand request, CancellationToken cancellationToken)
        {
            var company = await _companyQueries.GetByUserIdAsync(request.LoginSession.Id);

            if (company == null)
            {
                throw new BusinessException("Common.NoPermission");
            }

            AccountSingleRole account = null;

            if (request.Model == null || request.Model.Id == 0)
            {
                throw new BusinessException("Account.NotExisted");
            }
            else
            {
                account = await _accountQueries.GetByIdAsync(request.Model.Id);

                if (account == null)
                {
                    throw new BusinessException("Account.NotExisted");
                }
                else if (account.PartnerId != company.Id)
                {
                    throw new BusinessException("Common.NoPermission");
                }
            }

            if ((await _roleQueries.GetByIdAsync(request.Model.RoleId ?? 0)) == null)
            {
                throw new BusinessException("Role.NotSelected");
            }

            var rs = -1;

            using (var conn = DALHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        account.UserName       = request.Model.UserName;
                        account.Email          = request.Model.Email;
                        account.IsActived      = request.Model.IsActived;
                        account.IsExternalUser = request.Model.IsExternalUser;
                        account.IsUsed         = request.Model.IsUsed;

                        account.ModifiedDate = DateTime.Now;
                        account.ModifiedBy   = request.LoginSession.Id;
                        if (!string.IsNullOrWhiteSpace(request.Model.NewPassword))
                        {
                            account.Password = (request.Model.NewPassword + account.SecurityPassword.ToString()).CalculateMD5Hash();
                        }


                        if (await _accountRepository.UpdateAsync(account) > 0)
                        {
                            rs = 0;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        if (rs == 0)
                        {
                            trans.Commit();
                        }
                        else
                        {
                            try { trans.Rollback(); } catch { }
                        }
                    }
                }
            }
            return(rs);
        }
        public override async Task <int> HandleCommand(DeleteMaterialCommand request, CancellationToken cancellationToken)
        {
            var company = await _companyQueries.GetByUserIdAsync(request.LoginSession.Id);

            if (company == null)
            {
                throw new BusinessException("Common.NoPermission");
            }

            Account material = null;

            if (request.Model == 0)
            {
                throw new BusinessException("Account.NotExisted");
            }
            else
            {
                material = await _materialQueries.GetByIdAsync(request.Model);

                if (material == null)
                {
                    throw new BusinessException("Account.NotExisted");
                }
                else if (material.PartnerId != company.Id)
                {
                    throw new BusinessException("Common.NoPermission");
                }
            }

            var rs = -1;

            using (var conn = DALHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        material.IsDeleted    = true;
                        material.ModifiedDate = DateTime.Now;
                        material.ModifiedBy   = request.LoginSession.Id;

                        if (await _materialRepository.UpdateAsync(material) > 0)
                        {
                            rs = 0;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        if (rs == 0)
                        {
                            trans.Commit();
                        }
                        else
                        {
                            try { trans.Rollback(); } catch { }
                        }
                    }
                }
            }
            return(rs);
        }
        public override async Task <int> HandleCommand(UpdateAccountCommand request, CancellationToken cancellationToken)
        {
            AccountSingleRole account = null;

            if (request.Model == null || request.Model.Id == 0)
            {
                throw new BusinessException("Account.NotExisted");
            }
            else
            {
                account = await accountQueries.GetByIdAsync(request.Model.Id);

                if (account == null)
                {
                    throw new BusinessException("Account.NotExisted");
                }
            }

            if ((await roleQueries.GetByIdAsync(request.Model.RoleId ?? 0)) == null)
            {
                throw new BusinessException("Role.NotSelected");
            }

            var rs = -1;

            using (var conn = DALHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        if (account.RoleId != request.Model.RoleId)
                        {
                            var accRoles = (await accountRoleQueries.GetsAsync($"user_account_id = {request.Model.Id}")).ToList();
                            for (int i = 0; i < accRoles.Count; i++)
                            {
                                await accountRoleRepository.DeleteAsync(accRoles[i].Id);
                            }

                            await accountRoleRepository.AddAsync(new AccountRole()
                            {
                                RoleId        = request.Model.RoleId ?? 0,
                                UserAccountId = account.Id,
                                CreatedDate   = DateTime.Now,
                            });
                        }

                        request.Model.ModifiedDate     = DateTime.Now;
                        request.Model.ModifiedBy       = request.LoginSession.Id;
                        request.Model.IsDeleted        = account.IsDeleted;
                        request.Model.SecurityPassword = account.SecurityPassword;
                        if (!string.IsNullOrWhiteSpace(request.Model.NewPassword))
                        {
                            request.Model.Password = (request.Model.NewPassword + request.Model.SecurityPassword.ToString()).CalculateMD5Hash();
                        }
                        else
                        {
                            request.Model.Password = account.Password;
                        }

                        if (await accountRepository.UpdateAsync((Account)request.Model) > 0)
                        {
                            rs = 0;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        if (rs == 0)
                        {
                            trans.Commit();
                        }
                        else
                        {
                            try { trans.Rollback(); } catch { }
                        }
                    }
                }
            }
            return(rs);
        }