Example #1
0
        public void ShouldCreateNewColumnsWithErrors()
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddWorksheetParser <FakeClass>(x => x.WithParser <ClosedXmlParser <FakeClass> >().WithMap <MyWorksheetMap>());

            var serviceProvider = serviceCollection.BuildServiceProvider();

            var expectedItens = new List <FakeClass>
            {
                FakeClass.CreateItem(),
                FakeClass.CreateItem(false, false, false),
                FakeClass.CreateItem(false, false, true)
            };

            var parser = serviceProvider.GetService <WorksheetParser <FakeClass> >();

            expectedItens.ForEach(x => x.Bonus *= 0.1M);
            var validationResult = new ValidationResult <FakeClass>();
            var error            = new Error("Field TX_NAME canĀ“t not be null", 3, 2);

            validationResult.AddError(error);
            validationResult.AddItem(expectedItens.First());
            validationResult.AddItem(expectedItens.Last());

            var headers = new MyWorksheetMap().GetFields().Select(s => s.Name).ToList();

            using var worksheet = CreateStream(expectedItens, headers);
            var itens = parser.Parse(worksheet, worksheetName);

            using var streamWithErros = parser.WriteErrorsWithSummary(worksheet, worksheetName, itens.Errors);

            using var workbookWithErrors = new XLWorkbook(streamWithErros);
            var reader = new ClosedXmlReader(workbookWithErrors, worksheetName);

            reader.CountColumns().Should().Be(headers.Count() + 1);
        }
Example #2
0
        //Import with settings
        public FileImportEntity ImportProducts(Stream stream, int settingId, string columnCode, string columnName, string categoryName)
        {
            var fileImport = new FileImportEntity();

            try
            {
                var settingEntity = _settingRepository.GetById(settingId);
                if (settingEntity == null)
                {
                    return(fileImport);
                }

                _closedXmlReader = new ClosedXmlReader(stream);
                int invalidCount = 0;
                _closedXmlReader.UpdateDictMap(columnCode, columnName, categoryName);
                var productList = _closedXmlReader.ToProductList(ref invalidCount);
                fileImport.ProductCount = productList.Count;
                fileImport.FileSize     = stream.Length;
                if (productList.Count > invalidCount)
                {
                    fileImport.Status = true;
                    var barcodeService = new BarcodeService(settingEntity);

                    int iSaveCount = 0;
                    foreach (var product in productList)
                    {
                        //Check exist productCode
                        product.SettingId = settingId;
                        var productExist =
                            _productRepository.Get(
                                c => c.SettingId == product.SettingId && c.ProductCode == product.ProductCode);

                        if (productExist == null || string.IsNullOrWhiteSpace(productExist.Barcode))
                        {
                            barcodeService.UpdateBarcode(product);
                            if (productExist == null)
                            {
                                _productRepository.Add(product);
                            }
                            else
                            {
                                productExist.Barcode  = product.Barcode;
                                productExist.JsonData = product.JsonData;
                                productExist.Name     = product.Name;
                                productExist.Status   = product.Status;
                                _productRepository.Update(productExist);
                            }
                            iSaveCount++;
                            if (iSaveCount % 200 == 0)
                            {
                                _unitOfWork.Commit();
                            }
                        }
                    }
                    fileImport.SuccessCount = iSaveCount;
                    _unitOfWork.Commit();
                    return(fileImport);
                }
            }
            catch (Exception ex)
            {
                //
            }
            return(fileImport);
        }