コード例 #1
0
        private void BtnDone_Click(object sender, RoutedEventArgs e)
        {
            string image = "";

            if (imageSelect != null)
            {
                image = PassImage();
            }
            if (isUpdate == false)
            {
                var product = new ProductCreateVO();
                product.name      = tbName.Text;
                product.quantity  = int.Parse(tbQuantity.Text);
                product.price     = int.Parse(tbPrice.Text);
                product.catId     = (cbbCategorySelect.SelectedItem as Category).ID;
                product.imagePath = imageSelect != null ? image : "";
                var validateString = product.validate();
                if (validateString.Length > 0)
                {
                    MessageBox.Show(validateString);
                    return;
                }
                productBus.insert(product);
                FunctionHelper.CallPaginationFilter(ActionPagination.Load);
            }
            else
            {
                var product = new ProductUpdateVO();
                product.name      = tbName.Text;
                product.quantity  = int.Parse(tbQuantity.Text);
                product.price     = int.Parse(tbPrice.Text);
                product.catId     = (cbbCategorySelect.SelectedItem as Category).ID;
                product.imagePath = imageSelect != null ? image : productSelected.ImagePath;
                product.SKU       = productSelected.SKU;
                var validateString = product.validate();
                if (validateString.Length > 0)
                {
                    MessageBox.Show(validateString);
                    return;
                }
                productBus.update(productSelected.ID, product);
                FunctionHelper.CallPaginationFilter(ActionPagination.Load);
            }

            DisableForm();
            isUpdate = false;
        }
コード例 #2
0
        public Product insert(ProductCreateVO product)
        {
            var productModel = new Product()
            {
                Name        = product.name,
                CatID       = product.catId,
                Price       = product.price,
                Quantity    = product.quantity,
                SKU         = FunctionHelper.generalSKU("product"),
                Name_Slug   = FunctionHelper.ConvertToSlug(product.name),
                ImagePath   = product.imagePath,
                CreatedAt   = product.createdAt,
                UpdatedAt   = product.updatedAt,
                Description = product.description
            };

            var t = this._productDAO.insert(productModel);

            return(t);
        }
コード例 #3
0
        public static bool ImportFile(string FileName)
        {
            var dialog = new OpenFileDialog();

            try
            {
                var info         = new FileInfo(FileName);
                var imagesFolder = info.Directory + "\\images";
                var excelFile    = new Workbook(FileName);
                var tabs         = excelFile.Worksheets;

                Category category = new Category();
                foreach (var tab in tabs)
                {
                    var row        = 3;
                    var categoryVo = new CategoryCreateVO()
                    {
                        Name = tab.Name
                    };


                    var categoryBus = new CategoryBusiness();
                    var productBus  = new ProductBusiness();

                    var categoryF = categoryBus.checkNameExist(categoryVo.Name);



                    var productsVO = new List <ProductCreateVO>();

                    var cell = tab.Cells[$"C3"];
                    while (cell.Value != null || cell.StringValue.IsNotEmpty())
                    {
                        var name        = tab.Cells[$"D{row}"].StringValue;
                        var price       = tab.Cells[$"E{row}"].IntValue;
                        var quantity    = tab.Cells[$"F{row}"].IntValue;
                        var description = tab.Cells[$"G{row}"].StringValue;
                        var image       = tab.Cells[$"H{row}"].StringValue;

                        var baseFolder = AppDomain.CurrentDomain.BaseDirectory; // Có dấu \ ở cuối

                        // Kiểm tra thư mục Images có tồn tại hay chưa
                        if (!Directory.Exists(baseFolder + "Assert\\image\\"))
                        {
                            Directory.CreateDirectory(baseFolder + "Assert\\image\\");
                        }
                        string newName = "", extension = "";
                        if (File.Exists(imagesFolder + "\\" + image))
                        {
                            extension = image.Substring(image.Length - 4, 4);
                            newName   = Guid.NewGuid().ToString();
                            File.Copy(imagesFolder + "\\" + image, baseFolder + "Assert\\image\\" + newName + extension);
                        }
                        else
                        {
                            image = "";
                        }
                        var productVO = new ProductCreateVO()
                        {
                            name        = name,
                            price       = price,
                            quantity    = quantity,
                            imagePath   = newName + extension,
                            description = description
                        };
                        var checkNameProductExist = productBus.checkNameExist(productVO.name);
                        if (checkNameProductExist == null)
                        {
                            productsVO.Add(productVO);
                        }
                        row++;
                        cell = tab.Cells[$"C{row}"];
                    }
                    if (categoryF != null)
                    {
                        category = categoryBus.InsertData(categoryF.ID, productsVO);
                    }
                    else
                    {
                        category = categoryBus.InsertData(categoryVo, productsVO);
                        App.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => CategoryState.categoriesState.Add(category)));
                    }
                }
                return(true);
            }
            catch (Exception e)
            {
                return(false);
            }
        }