public async Task SynchronizeAdlerImagesAsync(int itemCardId, ImportAdlerDto itemCardImport, CancellationToken cancellationToken) { var itemCardImageRepository = _unitOfWork.GetRepository <ItemCardImage>(); var images = await _itemCardImageQueries.GetEntitiesAsync(_ajkaShopDbContext, itemCardId, cancellationToken).ConfigureAwait(false); foreach (var imageRecord in images) { var checkImage = itemCardImport.ImagePaths.FirstOrDefault(s => s.ImagePath == imageRecord.ImagePath); if (checkImage != null) { imageRecord.ColorName = checkImage.ColorName; imageRecord.AvailableSizesList = checkImage.AvailableSizesList; itemCardImageRepository.Update(imageRecord); itemCardImport.ImagePaths.Remove(checkImage); } } if (itemCardImport.ImagePaths.Any()) { foreach (var image in itemCardImport.ImagePaths) { var imageForInsert = new ItemCardImage { ItemCardId = itemCardId, ColorName = image.ColorName, ImagePath = image.ImagePath }; await itemCardImageRepository.InsertAsync(imageForInsert).ConfigureAwait(false); } } await _unitOfWork.SaveChangesAsync().ConfigureAwait(false); }
public void SynchronizeAdlerImages_Succeeds() { // Arrange itemCardImageRepository.Repository.Add(GetItemCardImageTestData().First()); unitOfWork.Setup(x => x.GetRepository <ItemCardImage>(It.IsAny <bool>())).Returns(itemCardImageRepository); var someItemImport = new ImportAdlerDto(); someItemImport.ImagePaths.Add(new ImportAdlerImageDto { ImagePath = someImagePath }); someItemImport.ImagePaths.Add(new ImportAdlerImageDto { ImagePath = Guid.NewGuid().ToString() }); someItemImport.ImagePaths.Add(new ImportAdlerImageDto { ImagePath = Guid.NewGuid().ToString() }); itemCardImageQueries.Setup(x => x.GetEntitiesAsync(It.IsAny <IAjkaShopDbContext>(), It.IsAny <int>(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult(GetItemCardImageTestData())); var serviceForTest = new ItemCardImageService(ajkaShopDbContext.Object, unitOfWork.Object, environment.Object, fileProcessingService.Object, itemCardImageQueries.Object); // Act serviceForTest.SynchronizeAdlerImagesAsync(idOfItemCard, someItemImport, CancellationToken.None).Wait(); // Assert // In repository is one existing record and two new. Assert.Equal(3, itemCardImageRepository.Repository.Count); }
private DAL.Model.ItemCard UpdateItemCard(DAL.Model.ItemCard itemCard, ImportAdlerDto itemCardImport, int categoryId) { itemCard.CategoryId = categoryId; itemCard.Headline = $"{itemCardImport.ProductName} {ConvertConstantToDescriptionHelper.ConvertProductLabelToDescription(itemCardImport.ProductLabel)}"; itemCard.Description = itemCardImport.Description; itemCard.Description += $"<p><b>Gramáž: </b>{itemCardImport.Grammage}</p>"; itemCard.Description += $"<p><b>Typ:</b>{string.Join(",", itemCardImport.SexList.Select(type => ConvertConstantToDescriptionHelper.ConvertSexTypeToDescription(type)))}</p>"; itemCard.Quantity = itemCardImport.Quantity; itemCard.Price = PriceRoundHelper.RoundToFive(Decimal.Round(itemCardImport.Price * ItemCardConstants.priceMultiplier)); itemCard.CommodityIdentifier = itemCardImport.CategoryId; itemCard.IsAdlerProduct = true; itemCard.State = Common.Enums.ItemCardEnums.ItemCardState.ForSale; if (itemCard.Quantity <= 0) { itemCard.State = Common.Enums.ItemCardEnums.ItemCardState.Inactive; } return(itemCard); }
public async Task ImportAsync(string productsData, CancellationToken cancellationToken) { try { var productsToImport = new List <ImportAdlerDto>(); var document = new XmlDocument(); document.LoadXml(productsData); var productList = document.GetElementsByTagName("product"); foreach (XmlNode product in productList) { XmlNode child = product.SelectSingleNode("childs"); var sexList = new List <string>(); XmlNode sex = child.SelectSingleNode("sex"); foreach (XmlNode sexId in sex.SelectSingleNode("childs")) { sexList.Add(sexId.InnerText); } var productToAdd = new ImportAdlerDto { CategoryId = child.SelectSingleNode("id_category")?.InnerText, CategoryName = child.SelectSingleNode("category")?.InnerText, ProductName = child.SelectSingleNode("name")?.InnerText, ProductLabel = child.SelectSingleNode("label")?.InnerText, Description = child.SelectSingleNode("description")?.InnerText, SizeName = child.SelectSingleNode("name_size")?.InnerText, ColorName = child.SelectSingleNode("name_color")?.InnerText, SizeId = child.SelectSingleNode("id_size")?.InnerText, Price = Convert.ToDecimal(child.SelectSingleNode("price")?.InnerText), ProductImagePath = child.SelectSingleNode("product_image")?.InnerText, Grammage = child.SelectSingleNode("grammage")?.InnerText, Quantity = int.Parse(child.SelectSingleNode("expedition_package")?.InnerText), SexList = sexList }; productsToImport.Add(productToAdd); } if (productsToImport.Any()) { var categories = productsToImport.GroupBy(g => g.CategoryName).Select(x => x.Key); await _categoryService.SynchronizeGroupCategoriesAsync(categories, cancellationToken).ConfigureAwait(false); var itemCards = productsToImport.GroupBy(g => new { g.CategoryId, g.CategoryName, g.ProductName, g.ProductLabel }) .Select(item => new ImportAdlerDto { CategoryId = item.Key.CategoryId, CategoryName = item.Key.CategoryName, ProductName = item.Key.ProductName, ProductLabel = item.Key.ProductLabel, Description = item.Min(d => d.Description), Quantity = item.Min(q => q.Quantity), Price = item.Max(p => p.Price), Grammage = item.Min(g => g.Grammage), SexList = item.Select(s => s.SexList).FirstOrDefault(), ImagePaths = item.GroupBy(g => new { g.ColorName, g.ProductImagePath }) .Select(select => new ImportAdlerImageDto { ColorName = select.Key.ColorName, ImagePath = select.Key.ProductImagePath.Replace("xl.jpg", "l.jpg") }).ToList(), Sizes = item.GroupBy(g => new { g.SizeId, g.SizeName }) .Select(select => new ImportAdlerSizeDto { SizeId = select.Key.SizeId, SizeName = select.Key.SizeName, Price = select.Max(p => p.Price) }).ToList() }); await _itemCardService.SynchronizeAdlerItemCardsAsync(itemCards, cancellationToken).ConfigureAwait(false); } Debug.WriteLine("Done"); } catch (Exception ex) { await _errorLogService.LogExceptionAsync(ex).ConfigureAwait(false); } }