Beispiel #1
0
        public static CatalogCollection GetCatalogs()
        {
            var allCatalogs   = new CatalogCollection();
            var mergedCatalog = new List <Catalog>();

            try
            {
                // TODO async tasks
                // companyA
                List <CatalogSupplier> suppliersA = FileProcessor.InportCsv <CatalogSupplier>("./input/suppliersA.csv");
                List <CatalogBarcodes> barcodesA  = FileProcessor.InportCsv <CatalogBarcodes>("./input/barcodesA.csv");
                List <Catalog>         productsA  = FileProcessor.InportCsv <Catalog>("./input/catalogA.csv");
                productsA.ForEach(p => p.Source = "A");

                // companyB
                List <CatalogSupplier> suppliersB = FileProcessor.InportCsv <CatalogSupplier>("./input/suppliersB.csv");
                List <CatalogBarcodes> barcodesB  = FileProcessor.InportCsv <CatalogBarcodes>("./input/barcodesB.csv");
                List <Catalog>         productsB  = FileProcessor.InportCsv <Catalog>("./input/catalogB.csv");
                productsB.ForEach(p => p.Source = "B");

                var duplicateProducts = barcodesB.Where(w => barcodesA.Any(a => a.Barcode == w.Barcode))
                                        .Select(s => s.SKU).Distinct().ToList();

                // merge A + B
                mergedCatalog = new List <Catalog>(productsA);
                var ignoreDuplicate = productsB.Where(w => !duplicateProducts.Any(a => a == w.SKU)).Select(s => s).ToList();
                mergedCatalog.AddRange(ignoreDuplicate);

                allCatalogs.MergedCatalog = mergedCatalog;
                allCatalogs.CompanyA      = new List <Catalog>(productsA);
                allCatalogs.CompanyB      = new List <Catalog>(productsB);

                // save output
                FileProcessor.ExportCsv <Catalog>("./output/result_output.csv", mergedCatalog.AsEnumerable());
            }
            catch (Exception)
            {
                throw;
            }
            return(allCatalogs);
        }
        public static List <Catalog> AddCatalog(Catalog addCatalog)
        {
            var updatedProducts = new List <Catalog>();

            try
            {
                // file paths
                var productFile = "./input/catalog" + addCatalog.Source + ".csv";
                // get data for company
                List <Catalog> products = FileProcessor.InportCsv <Catalog>(productFile);
                products.ForEach(p => p.Source = addCatalog.Source);
                // add product - should have vaildation before adding
                products.Add(addCatalog);

                // save back to input files
                FileProcessor.ExportCsv <Catalog>(productFile, products.AsEnumerable());
                updatedProducts = products;
            }
            catch (Exception)
            {
                return(updatedProducts);
            }
            return(updatedProducts);
        }
        public static List <Catalog> UpdateCatalog(UpdateCatalogDto updateCatalog)
        {
            var updatedProducts = new List <Catalog>();

            try
            {
                // file paths
                var supplierFile = "./input/suppliers" + updateCatalog.Source + ".csv";
                var barcodeFile  = "./input/barcodes" + updateCatalog.Source + ".csv";
                var productFile  = "./input/catalog" + updateCatalog.Source + ".csv";

                // get all data for company
                List <CatalogSupplier> suppliers = FileProcessor.InportCsv <CatalogSupplier>(supplierFile);
                List <CatalogBarcodes> barcodes  = FileProcessor.InportCsv <CatalogBarcodes>(barcodeFile);
                List <Catalog>         products  = FileProcessor.InportCsv <Catalog>(productFile);
                products.ForEach(p => p.Source = updateCatalog.Source);

                //find product
                var existProduct = products.Where(w => w.SKU == updateCatalog.SKU).Select(s => s).FirstOrDefault();
                if (existProduct != null)
                {
                    // supplier
                    var supplierID = suppliers.Max(m => m.ID) + 1;
                    var supplier   = suppliers.Where(w => w.Name == updateCatalog.SupplierName).Select(s => s).FirstOrDefault();
                    if (supplier == null)
                    {
                        // add supplier
                        supplier = new CatalogSupplier()
                        {
                            ID   = supplierID,
                            Name = updateCatalog.SupplierName
                        };
                        // add supplier
                        suppliers.Add(supplier);
                    }
                    else
                    {
                        supplierID = supplier.ID;
                    }
                    // barcode
                    var newBarcode = new CatalogBarcodes()
                    {
                        SupplierID = supplierID,
                        SKU        = updateCatalog.SKU,
                        Barcode    = updateCatalog.Barcode
                    };
                    barcodes.Add(newBarcode);
                }

                // save back to input files
                FileProcessor.ExportCsv <Catalog>(productFile, products.AsEnumerable());
                FileProcessor.ExportCsv <CatalogSupplier>(supplierFile, suppliers.AsEnumerable());
                FileProcessor.ExportCsv <CatalogBarcodes>(barcodeFile, barcodes.AsEnumerable());
                updatedProducts = products;
            }
            catch (Exception)
            {
                return(updatedProducts);
            }
            return(updatedProducts);
        }