Example #1
0
        public override async Task <int> HandleCommand(DeleteRoleCommand request, CancellationToken cancellationToken)
        {
            Role role = null;

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

                if (role == null)
                {
                    throw new BusinessException("Role.NotSelected");
                }
            }

            var rs = -1;

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

                        if (await roleRepository.UpdateAsync(role) > 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(InsertAccountCommand request, CancellationToken cancellationToken)
        {
            var id = 0;

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

            using (var conn = DALHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        request.Model.CreatedDate      = DateTime.Now;
                        request.Model.CreatedBy        = request.LoginSession.Id;
                        request.Model.ModifiedDate     = DateTime.Now;
                        request.Model.ModifiedBy       = request.LoginSession.Id;
                        request.Model.SecurityPassword = Guid.NewGuid();
                        request.Model.Password         = (request.Model.NewPassword + request.Model.SecurityPassword.ToString()).CalculateMD5Hash();

                        id = await accountRepository.AddAsync((Account)request.Model);

                        if (id > 0)
                        {
                            await accountRoleRepository.AddAsync(new AccountRole()
                            {
                                RoleId        = request.Model.RoleId ?? 0,
                                UserAccountId = id,
                                CreatedDate   = DateTime.Now,
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        if (id > 0)
                        {
                            trans.Commit();
                        }
                        else
                        {
                            try { trans.Rollback(); } catch { }
                        }
                    }
                }
            }

            return(id);
        }
Example #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(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);
        }