/// <summary> /// Receives generating data type and index to be casted into CsvProduct class which is then used to fill up the csvOutput. /// /// Writes summary in the cnosole for each callback. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="data"></param> /// <param name="index"></param> /// <returns></returns> public async override Task <Task> ParseAsync <T>(T data, int index) { CsvProduct product = (CsvProduct)Convert.ChangeType(data, typeof(CsvProduct)); CsvOutput output = new CsvOutput { Filename = GetFileReader().GetFilename(), Id = product.Id, Quantity = product.Quantity, Price = product.Price }; await SemaphoreSlim.WaitAsync(); try { if (AsyncFileWriter) { CsvOutputSerializer csv = new CsvOutputSerializer(output); await GetStreamWriter().WriteLineAsync(csv.ToCsvString()); } else { await AppendOutput(output); } Console.WriteLine("[CSV PARSER ] Processing line: {0} of file: {1}", index + 1, GetFileReader().GetFilename()); } finally { SemaphoreSlim.Release(); } return(Task.CompletedTask); }
private void ReplaceEmptyStringsWithNull(CsvProduct csvProduct) { csvProduct.Id = string.IsNullOrEmpty(csvProduct.Id) ? null : csvProduct.Id; csvProduct.OuterId = string.IsNullOrEmpty(csvProduct.OuterId) ? null : csvProduct.OuterId; csvProduct.CategoryId = string.IsNullOrEmpty(csvProduct.CategoryId) ? null : csvProduct.CategoryId; csvProduct.MainProductId = string.IsNullOrEmpty(csvProduct.MainProductId) ? null : csvProduct.MainProductId; csvProduct.PriceId = string.IsNullOrEmpty(csvProduct.PriceId) ? null : csvProduct.PriceId; csvProduct.PriceListId = string.IsNullOrEmpty(csvProduct.PriceListId) ? null : csvProduct.PriceListId; csvProduct.FulfillmentCenterId = string.IsNullOrEmpty(csvProduct.FulfillmentCenterId) ? null : csvProduct.FulfillmentCenterId; csvProduct.PackageType = string.IsNullOrEmpty(csvProduct.PackageType) ? null : csvProduct.PackageType; csvProduct.Reviews = csvProduct.Reviews.Where(x => !string.IsNullOrEmpty(x.Content) && !string.IsNullOrEmpty(x.ReviewType)).ToList(); }
public static Product ToProduct(this CsvProduct csvProduct) { return(new Product { PartNumber = csvProduct.PartNumber, Name = csvProduct.Name, Price = csvProduct.Price, Description = csvProduct.Description, MinimumPrice = csvProduct.MinimumPrice, Comment = csvProduct.Comment }); }
private List <CsvProduct> MakeMultipleExportProducts(CatalogProduct product, Dictionary <string, Price> prices, Dictionary <string, InventoryInfo> inventories) { List <CsvProduct> result = new List <CsvProduct>(); prices.TryGetValue(product.Id, out Price price); inventories.TryGetValue(product.Id, out InventoryInfo inventoryInfo); foreach (var seoInfo in product.SeoInfos.Any() ? product.SeoInfos : new List <Domain.Commerce.Model.SeoInfo>() { null }) { var csvProduct = new CsvProduct(product, _blobUrlResolver, price, inventoryInfo, seoInfo); result.Add(csvProduct); } return(result); }
private static AddProductDto FromCsvToDto(CategoryType category, CsvProduct product) { var shopColor = LamodaColorType.StringToLamodaColorType(product.Color, true); ContentPath path = new ContentPath(); var handler = new ResourceHandler(); //TODO вынести это и чтобы сразу при запуске программы все ресурсы подгрузились var lamodaColors = handler.ReadeResourceFile <LamodaColorsDeserializer>(path.LamodaColors); var printIds = product.Print.Split(",").Select(x => PrintType.StringToPrintType(x.Trim(), true).Id).ToList(); var printTypeId = printIds[0]; var extraPrintTypeId = printIds.Count >= 2 ? printIds[1] : 0; if (printIds.Count > 2) { throw new System.Exception("There are more than 2 printType"); } var result = new AddProductDto { BrandName = product.Brand, ShopColorId = shopColor.Id, ShopTypeId = 0, ColorIds = lamodaColors.Colors.First(x => x.LamodaColorType == shopColor.Id).HexColors.Select(ServerColor.ToInt).ToList(), CategoryTypeId = category.Id, Country = product.MadeInCountry, Link = product.Link, Photos = product.PhotosUrls.ToList(), Price = decimal.Parse(product.Price, CultureInfo.InvariantCulture.NumberFormat), PrintTypeId = printTypeId, ExtraPrintTypeId = extraPrintTypeId, Sizes = product.Sizes.Select(x => new SizeDto { CountryCode = x.CountryCode, IsAvailable = x.IsAvailable, OtherCountry = x.OtherCountry, RussianSize = x.RussianSize }).ToList(), VendorCode = product.VendorCode }; return(result); }
public void CsvProductMergeTest_ProductHasSameImages_ImagesUpdated() { //Arrange var catalogProduct = GetCatalogProductWithImage(); var csvProduct = new CsvProduct() { Images = new List <Image>() { new Image() { Id = "", Url = "SameURL" } } }; //Act csvProduct.MergeFrom(catalogProduct); //Assets Assert.Equal(1, csvProduct.Images.Count); Assert.NotNull(csvProduct.Images.FirstOrDefault(x => x.Id == "1")); }
private CsvProduct ExportAndImportProduct(CatalogProduct product) { var exportInfo = new CsvExportInfo(); using (var stream = new MemoryStream()) { var streamWriter = new StreamWriter(stream, Encoding.UTF8, 1024, true) { AutoFlush = true }; using (var csvWriter = new CsvWriter(streamWriter)) { exportInfo.Configuration = CsvProductMappingConfiguration.GetDefaultConfiguration(); exportInfo.Configuration.PropertyCsvColumns = product.Properties.Select(x => x.Name).Distinct().ToArray(); csvWriter.Configuration.Delimiter = exportInfo.Configuration.Delimiter; csvWriter.Configuration.RegisterClassMap(new CsvProductMap(exportInfo.Configuration)); csvWriter.WriteHeader <CsvProduct>(); csvWriter.NextRecord(); var csvProduct = new CsvProduct(product, null, null, null, null); csvWriter.WriteRecord(csvProduct); csvWriter.Flush(); stream.Position = 0; } using (var reader = new CsvReader(new StreamReader(stream, Encoding.UTF8))) { reader.Configuration.Delimiter = exportInfo.Configuration.Delimiter; reader.Configuration.RegisterClassMap(new CsvProductMap(exportInfo.Configuration)); reader.Configuration.MissingFieldFound = (strings, i, arg3) => { //do nothing }; reader.Configuration.TrimOptions = TrimOptions.Trim; reader.Read(); return(reader.GetRecord <CsvProduct>()); } } }
public void CsvProductMergeTest_ProductHasAnotherImages_ImagesAdded() { //Arrange var catalogProduct = GetCatalogProductWithImage(); var csvProduct = new CsvProduct() { Images = new List <Image>() { new Image() { Id = "", Url = "AnotherUrl" } } }; //Act csvProduct.MergeFrom(catalogProduct); //Assert Assert.Equal(2, csvProduct.Images.Count); Assert.NotNull(csvProduct.Images.FirstOrDefault(x => x.Id == "1")); Assert.NotNull(csvProduct.Images.FirstOrDefault(x => x.Id == "")); }
/// <summary> /// Creates a memory stream out of read bytes from the file, then creates a StreamReader which is used by the CsvReader from CSVHelper library. /// /// Configuration settings are: ignore any header records, maps each record into our CsvProduct model and converts/returns those items into a List. /// </summary> /// <returns></returns> private async Task <CsvReader> GetRecordsAsync() { using var memoryStream = new MemoryStream(GetFileReader().GetPayload()); using var reader = new StreamReader(memoryStream); using var csvReader = new CsvReader(reader, CultureInfo.InvariantCulture); csvReader.Configuration.HasHeaderRecord = false; csvReader.Configuration.IgnoreBlankLines = true; csvReader.Configuration.BadDataFound = null; csvReader.Configuration.MissingFieldFound = null; csvReader.Configuration.AutoMap <CsvProduct>(); int index = 0; while (await csvReader.ReadAsync()) { try { CsvProduct product = new CsvProduct { Id = csvReader.GetField <string>(0), Quantity = csvReader.GetField <int>(1), Price = csvReader.GetField <double>(2) }; await ParseAsync(product, index); index++; } catch (Exception) { Console.WriteLine("[CSV PARSER ] ERROR: Unable to parse CSV object on line {0} of file: {1}", index, GetFileReader().GetFilename()); } } return(csvReader); }