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

            if (request.Model == null || request.Model.Id == 0)
            {
                throw new BusinessException("Product.NotExisted");
            }
            else
            {
                product = await productQueries.GetByIdAsync(request.Model.Id);

                if (product == null)
                {
                    throw new BusinessException("Product.NotExisted");
                }
            }

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

            var rs = -1;

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

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

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

                        if (request.Model.IsChangedImage && (request.Model.ImageData?.Length ?? 0) > 0)
                        {
                            string oldLogoPath = product?.ImagePath ?? "";
                            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.ImagePath = await storageFile.SaveProductImage(filePath, type, base64Data);

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

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

                        if (request.Model.ProductLanguages != null && request.Model.ProductLanguages.Count > 0)
                        {
                            foreach (var plang in request.Model.ProductLanguages)
                            {
                                if (plang.Id <= 0)
                                {
                                    await productLanguageRepository.AddAsync(plang);
                                }
                                else
                                {
                                    await productLanguageRepository.UpdateAsync(plang);
                                }
                            }
                        }

                        if (await productRepository.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(InsertProductCommand request, CancellationToken cancellationToken)
        {
            var id = 0;

            using (var conn = DALHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        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 productRepository.AddAsync(request.Model);

                        Product product = await productQueries.GetByIdAsync(id);

                        if (request.Model.ProductLanguages != null && request.Model.ProductLanguages.Count > 0)
                        {
                            foreach (var plang in request.Model.ProductLanguages)
                            {
                                if (plang.Id <= 0)
                                {
                                    plang.ProductId = id;
                                    await productLanguageRepository.AddAsync(plang);
                                }
                                else
                                {
                                    await productLanguageRepository.UpdateAsync(plang);
                                }
                            }
                        }

                        var filePath = await storageFile.getProductFilePathAsync(product);

                        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("-", "");
                            product.ImagePath = await storageFile.SaveProductImage(filePath, type, base64Data);

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

            return(id);
        }