private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            Action action = () =>
            {
                this.Dispatcher.Invoke(() =>
                {
                    if (isCreate == true)
                    {
                        var categoryVo = new CategoryCreateVO()
                        {
                            Name = name
                        };

                        var validateString = categoryVo.validate();
                        if (validateString.Length > 0)
                        {
                            MessageBox.Show(validateString);
                            return;
                        }
                        var g = categoryBus.InsertData(categoryVo);

                        CategoryState.categoriesState.Add(g);
                    }
                    else
                    {
                        var categoryVo = new CategoryUpdateVO()
                        {
                            Name = name
                        };

                        var validateString = categoryVo.validate();
                        if (validateString.Length > 0)
                        {
                            MessageBox.Show(validateString);
                            return;
                        }

                        int id = ProductState.categoryId;
                        CategoryState.categoriesState.Remove(category);
                        categoryBus.update(id, categoryVo);
                        var categoryGet = categoryBus.getById(id);
                        CategoryState.categoriesState.Add(categoryGet);
                        isSuccess = true;
                    }
                });
            };

            FunctionHelper.LoadingEvent(action);
            this.Close();
        }
Beispiel #2
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);
            }
        }