Exemplo n.º 1
0
        public override async Task <int> HandleCommand(UpdateCompanyCommand request, CancellationToken cancellationToken)
        {
            Company company = null;

            if (request.Model == null || request.Model.Id == 0)
            {
                throw new BusinessException("Company.NotExisted");
            }
            else
            {
                company = await companyQueries.GetByIdAsync(request.Model.Id);

                if (company == null)
                {
                    throw new BusinessException("Company.NotExisted");
                }
            }

            var rs = -1;

            using (var conn = DALHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        request.Model.CreatedBy   = company.CreatedBy;
                        request.Model.CreatedDate = company.CreatedDate;

                        request.Model.ModifiedDate = DateTime.Now;
                        request.Model.ModifiedBy   = request.LoginSession.Id;

                        var filePath = await storageFile.getCompanyFilePathAsync(request.Model);

                        if ((request.Model.ImageData?.Length ?? 0) > Constant.MaxImageLength)
                        {
                            throw new BusinessException("Image.OutOfLength");
                        }

                        if (request.Model.IsChangedImage && (request.Model.ImageData?.Length ?? 0) > 0)
                        {
                            string oldLogoPath = company?.LogoPath ?? "";
                            string type        = CommonHelper.GetImageType(System.Text.Encoding.ASCII.GetBytes(request.Model.ImageData));
                            if (!CommonHelper.IsImageType(type))
                            {
                                throw new BusinessException("Image.WrongType");
                            }
                            string base64Data = request.Model.ImageData.Substring(request.Model.ImageData.IndexOf(",") + 1);
                            string fileName   = Guid.NewGuid().ToString().Replace("-", "");
                            request.Model.LogoPath = await storageFile.SaveCompanyLogo(filePath, type, base64Data);

                            if (string.IsNullOrWhiteSpace(oldLogoPath))
                            {
                                var imageFolderPath = settingQueries.GetValueAsync(SettingKeys.Path_Company);

                                var scheduleAction = (new RemoveFileAction()
                                {
                                    FilePath = $"{imageFolderPath}/{oldLogoPath}"
                                })
                                                     .GetScheduleAction(request.LoginSession?.Id ?? 0);
                                await scheduleActionRepository.AddAsync(scheduleAction);
                            }
                        }

                        if (await companyRepository.UpdateAsync(request.Model) > 0)
                        {
                            rs = 0;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        if (rs == 0)
                        {
                            trans.Commit();
                        }
                        else
                        {
                            try { trans.Rollback(); } catch { }
                        }
                    }
                }
            }
            return(rs);
        }
Exemplo n.º 2
0
        public override async Task <int> HandleCommand(InsertCompanyCommand request, CancellationToken cancellationToken)
        {
            var id = 0;

            using (var conn = DALHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        request.Model.Address.CreatedDate  = DateTime.Now;
                        request.Model.Address.CreatedBy    = request.LoginSession?.Id ?? 0;
                        request.Model.Address.ModifiedDate = DateTime.Now;
                        request.Model.Address.ModifiedBy   = request.LoginSession?.Id ?? 0;
                        var addressId = await addressRepository.AddAsync(request.Model.Address);


                        request.Model.Contact.CreatedDate  = DateTime.Now;
                        request.Model.Contact.CreatedBy    = request.LoginSession?.Id ?? 0;
                        request.Model.Contact.ModifiedDate = DateTime.Now;
                        request.Model.Contact.ModifiedBy   = request.LoginSession?.Id ?? 0;
                        var contactId = await contactRepository.AddAsync(request.Model.Contact);

                        request.Model.AddressId    = addressId;
                        request.Model.ContactId    = contactId;
                        request.Model.CreatedDate  = DateTime.Now;
                        request.Model.CreatedBy    = request.LoginSession?.Id ?? 0;
                        request.Model.ModifiedDate = DateTime.Now;
                        request.Model.ModifiedBy   = request.LoginSession?.Id ?? 0;
                        id = await companyRepository.AddAsync(request.Model);

                        Company company = await companyQueries.GetByIdAsync(id);

                        var filePath = await storageFile.getCompanyFilePathAsync(company);

                        if ((request.Model.ImageData?.Length ?? 0) > Constant.MaxImageLength)
                        {
                            throw new BusinessException("Image.OutOfLength");
                        }

                        if (request.Model.IsChangedImage && (request.Model.ImageData?.Length ?? 0) > 0)
                        {
                            string type = CommonHelper.GetImageType(System.Text.Encoding.ASCII.GetBytes(request.Model.ImageData));
                            if (!CommonHelper.IsImageType(type))
                            {
                                throw new BusinessException("Image.WrongType");
                            }
                            string base64Data = request.Model.ImageData.Substring(request.Model.ImageData.IndexOf(",") + 1);
                            string fileName   = Guid.NewGuid().ToString().Replace("-", "");
                            company.LogoPath = await storageFile.SaveCompanyLogo(filePath, type, base64Data);

                            await companyRepository.UpdateAsync(company);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        if (id > 0)
                        {
                            trans.Commit();
                        }
                        else
                        {
                            try { trans.Rollback(); } catch { }
                        }
                    }
                }
            }

            return(id);
        }