public async Task Should_Import_from_non_tempfile_new_rows_with_generated_id_entity_createonly()
        {
            var excelIoWrapper     = new FakeExcelIo();
            var importer           = new XlsxToTableImporter(GetDb(), excelIoWrapper);
            var cat                = new ProductCategory();
            var importMatchingData = new DataMatchesForImportingProductData
            {
                FileName = "foo.xlsx",
                Sheet    = "mysheet",
                Selected = new List <XlsToEfColumnPair>
                {
                    XlsToEfColumnPair.Create(() => cat.CategoryCode, "xlsCol8"),
                    XlsToEfColumnPair.Create("CategoryName", "xlsCol7"),
                },
            };
            Func <string, Expression <Func <ProductCategory, bool> > > selectorFinder = (y) => z => z.Id == int.Parse(y);
            var fileLocation = @"c:\myfiles\";
            await importer.ImportColumnData(importMatchingData, finder : selectorFinder, saveBehavior : new ImportSaveBehavior {
                RecordMode = RecordMode.CreateOnly
            }, fileLocation : fileLocation);

            var updatedItem = GetDb().Set <ProductCategory>().First();

            updatedItem.CategoryCode.ShouldBe("FRZ");
            updatedItem.CategoryName.ShouldBe("Frozen Food");

            excelIoWrapper.FileName.ShouldBe(fileLocation + importMatchingData.FileName);
        }
Beispiel #2
0
        public async Task <ActionResult> SubmitProductColumnMatches([FromBody] DataMatchesForImportingProductData data)
        {
            //    var c = new DbContext("XlsToEf");
            var result = await _mediator.Send(data);

            return(Json(result));
        }
        public async Task Should_Import_new_and_update_rows_with_generated_id_entity_upsert()
        {
            var objectToUpdate = new ProductCategory
            {
                CategoryCode = "AAA",
                CategoryName = "BBB"
            };

            await PersistToDatabase(objectToUpdate);

            var excelIoWrapper     = new FakeExcelIo();
            var importer           = new XlsxToTableImporter(GetDb(), excelIoWrapper);
            var cat                = new ProductCategory();
            var importMatchingData = new DataMatchesForImportingProductData
            {
                FileName = "foo.xlsx",
                Sheet    = "mysheet",
                Selected = new List <XlsToEfColumnPair>
                {
                    XlsToEfColumnPair.Create(() => cat.Id, "xlsCol6"),
                    XlsToEfColumnPair.Create("CategoryCode", "xlsCol8"),
                    XlsToEfColumnPair.Create(() => cat.CategoryName, "xlsCol7"),
                },
            };
            var id = objectToUpdate.Id;

            excelIoWrapper.Rows[0]["xlsCol6"] = id.ToString(); // change the id to the autogenerated one so we can update it.
            excelIoWrapper.Rows.Add(
                new Dictionary <string, string>
            {
                { "xlsCol5", "347" },
                { "xlsCol1", "56493.7" },
                { "xlsCol2", "8/16/2014" },
                { "xlsCol3", "8888.5" },
                { "xlsCol4", "9/27/2015" },
                { "xlsCol6", "" },
                { "xlsCol7", "Vegetables" },
                { "xlsCol8", "VEG" },
            });

            Func <string, Expression <Func <ProductCategory, bool> > > selectorFinder = (y) => z => z.Id == int.Parse(y);
            await importer.ImportColumnData <ProductCategory>(importMatchingData);

            var updatedItem = GetDb().Set <ProductCategory>().First(x => x.Id == id);

            updatedItem.CategoryCode.ShouldBe("FRZ");
            updatedItem.CategoryName.ShouldBe("Frozen Food");

            var newItem = GetDb().Set <ProductCategory>().First(x => x.CategoryCode == "VEG");

            newItem.CategoryName.ShouldBe("Vegetables");
        }
        public async Task Should_Import_new_rows_with_generated_id_entity_with_shadow_ref_createonly()
        {
            var referencedCategory = new ProductCategory
            {
                CategoryName = "Pets",
                CategoryCode = "PET"
            };

            await PersistToDatabase(referencedCategory);

            var referencedProduct = new Product
            {
                ProductName       = "Kitten",
                ProductCategoryId = referencedCategory.Id
            };

            await PersistToDatabase(referencedProduct);

            var excelIoWrapper = new FakeExcelIo();

            excelIoWrapper.Rows[0]["xlsCol5"] = referencedProduct.Id.ToString();
            excelIoWrapper.Rows[0]["xlsCol8"] = "Large";

            var importer           = new XlsxToTableImporter(GetDb(), excelIoWrapper);
            var cat                = new ProductSizeOption();
            var importMatchingData = new DataMatchesForImportingProductData
            {
                FileName = "foo.xlsx",
                Sheet    = "mysheet",
                Selected = new List <XlsToEfColumnPair>
                {
                    XlsToEfColumnPair.Create("ProductId", "xlsCol5"),
                    XlsToEfColumnPair.Create(() => cat.Size, "xlsCol8"),
                },
            };
            Func <string, Expression <Func <ProductSizeOption, bool> > > selectorFinder = (y) => z => z.Id == int.Parse(y);
            await importer.ImportColumnData(importMatchingData, finder : selectorFinder, saveBehavior : new ImportSaveBehavior {
                RecordMode = RecordMode.CreateOnly
            });

            var updatedItem = GetDb().Set <ProductSizeOption>().Include(size => size.Product).First();

            updatedItem.Size.ShouldBe("Large");
            updatedItem.Product.Id.ShouldBe(referencedProduct.Id);
        }
        public async Task Should_update_rows_with_generated_id_entity_update()
        {
            var objectToUpdate = new ProductCategory
            {
                CategoryCode = "AAA",
                CategoryName = "BBB"
            };

            await PersistToDatabase(objectToUpdate);

            var excelIoWrapper     = new FakeExcelIo();
            var importer           = new XlsxToTableImporter(GetDb(), excelIoWrapper);
            var cat                = new ProductCategory();
            var importMatchingData = new DataMatchesForImportingProductData
            {
                FileName = "foo.xlsx",
                Sheet    = "mysheet",
                Selected = new List <XlsToEfColumnPair>
                {
                    XlsToEfColumnPair.Create(() => cat.Id, "xlsCol6"),
                    XlsToEfColumnPair.Create("CategoryCode", "xlsCol8"),
                    XlsToEfColumnPair.Create(() => cat.CategoryName, "xlsCol7"),
                },
            };
            var id = objectToUpdate.Id;

            excelIoWrapper.Rows[0]["xlsCol6"] = id.ToString(); // change the id to the autogenerated one so we can update it.

            await importer.ImportColumnData <ProductCategory>(importMatchingData, saveBehavior : new ImportSaveBehavior {
                RecordMode = RecordMode.UpdateOnly
            });

            var updatedItem = GetDb().Set <ProductCategory>().First(x => x.Id == id);

            updatedItem.CategoryCode.ShouldBe("FRZ");
            updatedItem.CategoryName.ShouldBe("Frozen Food");
        }