private ProductAttributeMappingDto PrepareProductAttributeMappingDto( ProductAttributeMapping productAttributeMapping) { ProductAttributeMappingDto productAttributeMappingDto = null; if (productAttributeMapping != null) { productAttributeMappingDto = new ProductAttributeMappingDto { Id = productAttributeMapping.Id, ProductAttributeId = productAttributeMapping.ProductAttributeId, ProductAttributeName = _productAttributeService .GetProductAttributeById(productAttributeMapping.ProductAttributeId).Name, TextPrompt = productAttributeMapping.TextPrompt, DefaultValue = productAttributeMapping.DefaultValue, AttributeControlTypeId = productAttributeMapping.AttributeControlTypeId, DisplayOrder = productAttributeMapping.DisplayOrder, IsRequired = productAttributeMapping.IsRequired, ProductAttributeValues = productAttributeMapping.ProductAttributeValues .Select(x => PrepareProductAttributeValueDto(x, productAttributeMapping.Product)).ToList() }; } return(productAttributeMappingDto); }
/// <summary> /// Adds an attribute /// </summary> /// <param name="attributesXml">Attributes in XML format</param> /// <param name="productAttributeMapping">Product attribute mapping</param> /// <param name="value">Value</param> /// <returns>Updated result (XML format)</returns> public virtual string AddProductAttribute(string attributesXml, ProductAttributeMapping productAttributeMapping, string value) { string result = string.Empty; try { var xmlDoc = new XmlDocument(); if (String.IsNullOrEmpty(attributesXml)) { var element1 = xmlDoc.CreateElement("Attributes"); xmlDoc.AppendChild(element1); } else { xmlDoc.LoadXml(attributesXml); } var rootElement = (XmlElement)xmlDoc.SelectSingleNode(@"//Attributes"); XmlElement attributeElement = null; //find existing var nodeList1 = xmlDoc.SelectNodes(@"//Attributes/ProductAttribute"); foreach (XmlNode node1 in nodeList1) { if (node1.Attributes != null && node1.Attributes["ID"] != null) { string str1 = node1.Attributes["ID"].InnerText.Trim(); int id; if (int.TryParse(str1, out id)) { if (id == productAttributeMapping.Id) { attributeElement = (XmlElement)node1; break; } } } } //create new one if not found if (attributeElement == null) { attributeElement = xmlDoc.CreateElement("ProductAttribute"); attributeElement.SetAttribute("ID", productAttributeMapping.Id.ToString()); rootElement.AppendChild(attributeElement); } var attributeValueElement = xmlDoc.CreateElement("ProductAttributeValue"); attributeElement.AppendChild(attributeValueElement); var attributeValueValueElement = xmlDoc.CreateElement("Value"); attributeValueValueElement.InnerText = value; attributeValueElement.AppendChild(attributeValueValueElement); result = xmlDoc.OuterXml; } catch (Exception exc) { Debug.Write(exc.ToString()); } return(result); }
/// <summary> /// Remove an attribute /// </summary> /// <param name="attributesXml">Attributes in XML format</param> /// <param name="productAttributeMapping">Product attribute mapping</param> /// <returns>Updated result (XML format)</returns> public virtual string RemoveProductAttribute(string attributesXml, ProductAttributeMapping productAttributeMapping) { var result = string.Empty; try { var xmlDoc = new XmlDocument(); if (string.IsNullOrEmpty(attributesXml)) { var element1 = xmlDoc.CreateElement("Attributes"); xmlDoc.AppendChild(element1); } else { xmlDoc.LoadXml(attributesXml); } var rootElement = (XmlElement)xmlDoc.SelectSingleNode(@"//Attributes"); XmlElement attributeElement = null; //find existing var nodeList1 = xmlDoc.SelectNodes(@"//Attributes/ProductAttribute"); foreach (XmlNode node1 in nodeList1) { if (node1.Attributes?["ID"] == null) { continue; } var str1 = node1.Attributes["ID"].InnerText.Trim(); if (!int.TryParse(str1, out var id)) { continue; } if (id != productAttributeMapping.Id) { continue; } attributeElement = (XmlElement)node1; break; } //found if (attributeElement != null) { rootElement.RemoveChild(attributeElement); } result = xmlDoc.OuterXml; } catch (Exception exc) { Debug.Write(exc.ToString()); } return(result); }
/// <summary> /// Check whether condition of some attribute is met (if specified). Return "null" if not condition is specified /// </summary> /// <param name="pam">Product attribute</param> /// <param name="selectedAttributes">Selected attributes</param> /// <returns>Result</returns> public virtual bool?IsConditionMet(Product product, ProductAttributeMapping pam, IList <CustomAttribute> selectedAttributes) { if (pam == null) { throw new ArgumentNullException(nameof(pam)); } if (selectedAttributes == null) { selectedAttributes = new List <CustomAttribute>(); } var conditionAttribute = pam.ConditionAttribute; if (!conditionAttribute.Any()) { //no condition return(null); } //load an attribute this one depends on var dependOnAttribute = ParseProductAttributeMappings(product, conditionAttribute).FirstOrDefault(); if (dependOnAttribute == null) { return(true); } var valuesThatShouldBeSelected = ParseValues(conditionAttribute, dependOnAttribute.Id) .Where(x => !string.IsNullOrEmpty(x)) .ToList(); var selectedValues = ParseValues(selectedAttributes, dependOnAttribute.Id); if (valuesThatShouldBeSelected.Count != selectedValues.Count) { return(false); } //compare values var allFound = true; foreach (var t1 in valuesThatShouldBeSelected) { bool found = false; foreach (var t2 in selectedValues) { if (t1 == t2) { found = true; } } if (!found) { allFound = false; } } return(allFound); }
public async Task <int> InsertAsync(ProductAttributeMapping entity) { if (entity == null) { throw new ArgumentNullException("ProductAttributeMapping"); } return(await _ProductAttributeMappingRepository.InsertAsync(entity)); }
/// <summary> /// Check whether condition of some attribute is met (if specified). Return "null" if not condition is specified /// </summary> /// <param name="pam">Product attribute</param> /// <param name="selectedAttributesXml">Selected attributes (XML format)</param> /// <returns>Result</returns> public virtual bool?IsConditionMet(ProductAttributeMapping pam, string selectedAttributesXml) { if (pam == null) { throw new ArgumentNullException(nameof(pam)); } var conditionAttributeXml = pam.ConditionAttributeXml; if (string.IsNullOrEmpty(conditionAttributeXml)) { //no condition return(null); } //load an attribute this one depends on var dependOnAttribute = ParseProductAttributeMappings(conditionAttributeXml).FirstOrDefault(); if (dependOnAttribute == null) { return(true); } var valuesThatShouldBeSelected = ParseValues(conditionAttributeXml, dependOnAttribute.Id) //a workaround here: //ConditionAttributeXml can contain "empty" values (nothing is selected) //but in other cases (like below) we do not store empty values //that's why we remove empty values here .Where(x => !string.IsNullOrEmpty(x)) .ToList(); var selectedValues = ParseValues(selectedAttributesXml, dependOnAttribute.Id); if (valuesThatShouldBeSelected.Count != selectedValues.Count) { return(false); } //compare values var allFound = true; foreach (var t1 in valuesThatShouldBeSelected) { var found = false; foreach (var t2 in selectedValues) { if (t1 == t2) { found = true; } } if (!found) { allFound = false; } } return(allFound); }
public ProductAttributeMappingDto PrepareProductAttributeMappingDTO(ProductAttributeMapping productAttribute) { var mapping = productAttribute.ToDto(); mapping.ProductAttributeValues = _productAttributeService.GetProductAttributeValues(productAttribute.Id) .Select(PrepareProductAttributeValueDto) .ToList(); return(mapping); }
/// <summary> /// Updates the product attribute mapping /// </summary> /// <param name="productAttributeMapping">The product attribute mapping</param> public virtual void UpdateProductAttributeMapping(ProductAttributeMapping productAttributeMapping) { if (productAttributeMapping == null) { throw new ArgumentNullException(nameof(productAttributeMapping)); } _productAttributeMappingRepository.Update(productAttributeMapping); }
/// <summary> /// A value indicating whether this product attribute should have values /// </summary> /// <param name="productAttributeMapping">Product attribute mapping</param> /// <returns>Result</returns> public static bool ShouldHaveValues(this ProductAttributeMapping productAttributeMapping) { if (productAttributeMapping == null) { return(false); } //other attribute controle types support values return(true); }
/// <summary> /// Inserts a product attribute mapping /// </summary> /// <param name="productAttributeMapping">The product attribute mapping</param> public virtual void InsertProductAttributeMapping(ProductAttributeMapping productAttributeMapping) { if (productAttributeMapping == null) { throw new ArgumentNullException("productAttributeMapping"); } _productAttributeMappingRepository.Insert(productAttributeMapping); //_unitOfWork.Commit(); }
public async Task <(decimal taxRate, decimal sciSubTotalInclTax, decimal sciUnitPriceInclTax, decimal warrantyUnitPriceExclTax, decimal warrantyUnitPriceInclTax)> CalculateWarrantyTaxAsync( ShoppingCartItem sci, Customer customer, decimal sciSubTotalExclTax, decimal sciUnitPriceExclTax ) { var product = await _productService.GetProductByIdAsync(sci.ProductId); var sciSubTotalInclTax = await _taxService.GetProductPriceAsync(product, sciSubTotalExclTax, true, customer); var sciUnitPriceInclTax = await _taxService.GetProductPriceAsync(product, sciUnitPriceExclTax, true, customer); var warrantyUnitPriceExclTax = decimal.Zero; (decimal price, decimal taxRate)warrantyUnitPriceInclTax; warrantyUnitPriceInclTax.price = decimal.Zero; warrantyUnitPriceInclTax.taxRate = decimal.Zero; // warranty item handling ProductAttributeMapping warrantyPam = await _attributeUtilities.GetWarrantyAttributeMappingAsync(sci.AttributesXml); if (warrantyPam != null) { warrantyUnitPriceExclTax = (await _productAttributeParser.ParseProductAttributeValuesAsync(sci.AttributesXml)) .Where(pav => pav.ProductAttributeMappingId == warrantyPam.Id) .Select(pav => pav.PriceAdjustment) .FirstOrDefault(); // get warranty "product" - this is so the warranties have a tax category Product warrProduct = _importUtilities.GetExistingProductBySku("WARRPLACE_SKU"); var isCustomerInTaxableState = await IsCustomerInTaxableStateAsync(customer); if (warrProduct == null) { // taxed warranty price warrantyUnitPriceInclTax = await _taxService.GetProductPriceAsync(product, warrantyUnitPriceExclTax, false, customer); } else { warrantyUnitPriceInclTax = await _taxService.GetProductPriceAsync(warrProduct, warrantyUnitPriceExclTax, isCustomerInTaxableState, customer); } var productUnitPriceInclTax = await _taxService.GetProductPriceAsync(product, sciUnitPriceExclTax - warrantyUnitPriceExclTax, true, customer); sciUnitPriceInclTax.price = productUnitPriceInclTax.price + warrantyUnitPriceInclTax.price; sciSubTotalInclTax.price = sciUnitPriceInclTax.price * sci.Quantity; } return(warrantyUnitPriceInclTax.taxRate, sciSubTotalInclTax.price, sciUnitPriceInclTax.price, warrantyUnitPriceExclTax, warrantyUnitPriceInclTax.price); }
private async Task UpdatePamAsync( Product product, ProductAttributeMapping pam, AbcMattressEntry abcMattressEntry) { var sizeAttrs = await GetSizeAttributesAsync(product, abcMattressEntry); pam.ConditionAttributeXml = $"<Attributes><ProductAttribute ID=\"{sizeAttrs.pam.Id}\"><ProductAttributeValue><Value>{sizeAttrs.pav.Id}</Value></ProductAttributeValue></ProductAttribute></Attributes>"; await _productAttributeService.UpdateProductAttributeMappingAsync(pam); }
public async Task <IHttpActionResult> Delete(int id) { ProductAttributeMapping entity = await ProductAttributeMappingService.FindOneAsync(id); if (entity == null) { return(NotFound()); } await ProductAttributeMappingService.DeleteAsync(entity); return(Ok(entity.ToModel())); }
/// <summary> /// Updates the product attribute mapping /// </summary> /// <param name="productAttributeMapping">The product attribute mapping</param> public virtual void UpdateProductAttributeMapping(ProductAttributeMapping productAttributeMapping) { if (productAttributeMapping == null) { throw new ArgumentNullException(nameof(productAttributeMapping)); } _productAttributeMappingRepository.Update(productAttributeMapping); //event notification _eventPublisher.EntityUpdated(productAttributeMapping); }
public async Task InsertProductAttributeMappingAsync( int productId, int attributeId, EntityManager <ProductAttributeMapping> attributeManager) { ProductAttributeMapping productAttributeMapping = new ProductAttributeMapping(); productAttributeMapping.ProductId = productId; productAttributeMapping.ProductAttributeId = attributeId; productAttributeMapping.AttributeControlType = AttributeControlType.MultilineTextbox; productAttributeMapping.IsRequired = false; await attributeManager.InsertAsync(productAttributeMapping); }
public async Task <decimal?> GetListingPriceForMattressProductAsync(int productId) { // only need to do this if we're on the 'shop by size' categories // but we're opening this up to be called anywhere // including the JSON schema for google crawler var url = _webHelper.GetThisPageUrl(true); if (!IsSizeCategoryPage(url)) { return(null); } var pams = await _productAttributeService.GetProductAttributeMappingsByProductIdAsync(productId); ProductAttributeMapping mattressSizePam = null; foreach (var pam in pams) { var productAttribute = await _productAttributeService.GetProductAttributeByIdAsync(pam.ProductAttributeId); if (productAttribute?.Name == AbcMattressesConsts.MattressSizeName) { mattressSizePam = pam; break; } } if (mattressSizePam == null) // if no mattress sizes, return price { return(null); } var value = (await _productAttributeService.GetProductAttributeValuesAsync( mattressSizePam.Id )).Where(pav => pav.Name == GetMattressSizeFromUrl(url)) .FirstOrDefault(); if (value == null) // no matching sizes, check for default (queen) { value = (await _productAttributeService.GetProductAttributeValuesAsync( mattressSizePam.Id )).Where(pav => pav.Name == AbcMattressesConsts.Queen) .FirstOrDefault(); } var product = await _productService.GetProductByIdAsync(productId); return(value == null ? (decimal?)null : Math.Round(product.Price + value.PriceAdjustment, 2)); }
/// <summary> /// Remove an attribute /// </summary> /// <param name="attributesXml">Attributes in XML format</param> /// <param name="productAttributeMapping">Product attribute mapping</param> /// <returns>Updated result (XML format)</returns> public virtual string RemoveProductAttribute(string attributesXml, ProductAttributeMapping productAttributeMapping) { string result = string.Empty; try { var xmlDoc = new XmlDocument(); if (String.IsNullOrEmpty(attributesXml)) { var element1 = xmlDoc.CreateElement("Attributes"); xmlDoc.AppendChild(element1); } else { xmlDoc.LoadXml(attributesXml); } //tbh //var rootElement = (XmlElement)xmlDoc.SelectSingleNode(@"//Attributes"); //XmlElement attributeElement = null; ////find existing //var nodeList1 = xmlDoc.SelectNodes(@"//Attributes/ProductAttribute"); //foreach (XmlNode node1 in nodeList1) //{ // if (node1.Attributes != null && node1.Attributes["ID"] != null) // { // string str1 = node1.Attributes["ID"].InnerText.Trim(); // if (str1 == productAttributeMapping.Id) // { // attributeElement = (XmlElement)node1; // break; // } // } //} ////found //if (attributeElement != null) //{ // rootElement.RemoveChild(attributeElement); //} result = xmlDoc.OuterXml; } catch (Exception exc) { Debug.Write(exc.ToString()); } return(result); }
/// <summary> /// Updates the product attribute mapping /// </summary> /// <param name="productAttributeMapping">The product attribute mapping</param> /// <param name="productId">Product ident</param> /// <param name="values">Update values</param> public virtual async Task UpdateProductAttributeMapping(ProductAttributeMapping productAttributeMapping, string productId, bool values = false) { if (productAttributeMapping == null) { throw new ArgumentNullException(nameof(productAttributeMapping)); } await _productRepository.UpdateToSet(productId, x => x.ProductAttributeMappings, z => z.Id, productAttributeMapping.Id, productAttributeMapping); //cache await _cacheBase.RemoveByPrefix(string.Format(CacheKey.PRODUCTS_BY_ID_KEY, productId)); //event notification await _mediator.EntityUpdated(productAttributeMapping); }
private async Task UpdateProductAttributesAsync(Product entityToUpdate, Delta <ProductDto> productDtoDelta) { // If no product attribute mappings are specified means we don't have to update anything if (productDtoDelta.Dto.ProductAttributeMappings == null) { return; } // delete unused product attribute mappings var toBeUpdatedIds = productDtoDelta.Dto.ProductAttributeMappings.Where(y => y.Id != 0).Select(x => x.Id); var productAttributeMappings = await _productAttributeService.GetProductAttributeMappingsByProductIdAsync(entityToUpdate.Id); var unusedProductAttributeMappings = productAttributeMappings.Where(x => !toBeUpdatedIds.Contains(x.Id)).ToList(); foreach (var unusedProductAttributeMapping in unusedProductAttributeMappings) { await _productAttributeService.DeleteProductAttributeMappingAsync(unusedProductAttributeMapping); } foreach (var productAttributeMappingDto in productDtoDelta.Dto.ProductAttributeMappings) { if (productAttributeMappingDto.Id > 0) { // update existing product attribute mapping var productAttributeMappingToUpdate = productAttributeMappings.FirstOrDefault(x => x.Id == productAttributeMappingDto.Id); if (productAttributeMappingToUpdate != null) { productDtoDelta.Merge(productAttributeMappingDto, productAttributeMappingToUpdate, false); await _productAttributeService.UpdateProductAttributeMappingAsync(productAttributeMappingToUpdate); await UpdateProductAttributeValuesAsync(productAttributeMappingDto, productDtoDelta); } } else { var newProductAttributeMapping = new ProductAttributeMapping { ProductId = entityToUpdate.Id }; productDtoDelta.Merge(productAttributeMappingDto, newProductAttributeMapping); // add new product attribute await _productAttributeService.InsertProductAttributeMappingAsync(newProductAttributeMapping); } } }
public static bool IsNonCombinable(this ProductAttributeMapping productAttributeMapping) { if (productAttributeMapping == null) { return(false); } if (!productAttributeMapping.Combination) { return(true); } var result = !ShouldHaveValues(productAttributeMapping); return(result); }
public static bool ValidationRulesAllowed(this ProductAttributeMapping productAttributeMapping) { if (productAttributeMapping == null) { return(false); } if (productAttributeMapping.AttributeControlTypeId == AttributeControlType.TextBox || productAttributeMapping.AttributeControlTypeId == AttributeControlType.MultilineTextbox || productAttributeMapping.AttributeControlTypeId == AttributeControlType.FileUpload) { return(true); } return(false); }
public IActionResult Create(ProductPictureModifierModel model, bool continueEditing) { if (!_permissionService.Authorize(StandardPermissionProvider.ManageWidgets)) { return(AccessDeniedView()); } //todo find out why model state validation failed even though product id is only validated and model has product id if (model.ProductId < 1) { ModelState.AddModelError(nameof(model.ProductId), _localizationService.GetResource("Admin.Widgets.ProductPictureModifier.Fields.Product.Required")); return(View("~/Plugins/Widgets.ProductPictureModifier/Views/Admin/Create.cshtml", model)); } ProductAttribute productAttribute = _productPictureModifierService. GetProductAttributeByName(ProductPictureModifierDefault.ProductAttributeName).First(); var productAttributeMapping = new ProductAttributeMapping { ProductAttributeId = productAttribute.Id, ProductId = model.ProductId, DefaultValue = model.ColorCode, AttributeControlTypeId = (int)AttributeControlType.TextBox }; _productAttributeService.InsertProductAttributeMapping(productAttributeMapping); //Logo Default Value _logoPositionService.Insert(new LogoPosition { ProductId = model.ProductId, ColorCode = model.ColorCode }); //Clear the cache for product picture preparation _cacheManager.RemoveByPattern(string.Format(ModelCacheEventConsumer.PRODUCT_DEFAULTPICTURE_PATTERN_KEY_BY_ID, model.ProductId)); SuccessNotification(_localizationService.GetResource("Widgets.ProductPictureModifier.Mapping.Created")); if (!continueEditing) { return(RedirectToAction("Configure")); } return(RedirectToAction("Edit", new { id = productAttributeMapping.Id })); }
/// <summary> /// A value indicating whether this product attribute should can have some validation rules /// </summary> /// <param name="productAttributeMapping">Product attribute mapping</param> /// <returns>Result</returns> public static bool ValidationRulesAllowed(this ProductAttributeMapping productAttributeMapping) { if (productAttributeMapping == null) { return(false); } if (productAttributeMapping.AttributeControlType == AttributeControlType.TextBox || productAttributeMapping.AttributeControlType == AttributeControlType.MultilineTextbox || productAttributeMapping.AttributeControlType == AttributeControlType.FileUpload) { return(true); } //other attribute controle types does not have validation return(false); }
/// <summary> /// Inserts a product attribute mapping /// </summary> /// <param name="productAttributeMapping">The product attribute mapping</param> public virtual async Task InsertProductAttributeMapping(ProductAttributeMapping productAttributeMapping) { if (productAttributeMapping == null) { throw new ArgumentNullException("productAttributeMapping"); } var updatebuilder = Builders <Product> .Update; var update = updatebuilder.AddToSet(p => p.ProductAttributeMappings, productAttributeMapping); await _productRepository.Collection.UpdateOneAsync(new BsonDocument("_id", productAttributeMapping.ProductId), update); //cache await _cacheManager.RemoveAsync(string.Format(CacheKey.PRODUCTS_BY_ID_KEY, productAttributeMapping.ProductId)); //event notification await _mediator.EntityInserted(productAttributeMapping); }
/// <summary> /// Deletes a product attribute mapping /// </summary> /// <param name="productAttributeMapping">Product attribute mapping</param> public virtual async Task DeleteProductAttributeMapping(ProductAttributeMapping productAttributeMapping) { if (productAttributeMapping == null) { throw new ArgumentNullException("productAttributeMapping"); } var updatebuilder = Builders <Product> .Update; var update = updatebuilder.PullFilter(p => p.ProductAttributeMappings, y => y.Id == productAttributeMapping.Id); await _productRepository.Collection.UpdateManyAsync(new BsonDocument("_id", productAttributeMapping.ProductId), update); //cache _cacheManager.RemoveByPattern(string.Format(PRODUCTS_BY_ID_KEY, productAttributeMapping.ProductId)); //event notification await _eventPublisher.EntityDeleted(productAttributeMapping); }
public async Task <IActionResult> StoreSelected(string shopId) { Shop shop = await _shopService.GetShopByIdAsync(Int32.Parse(shopId)); _customerShopService.InsertOrUpdateCustomerShop((await _workContext.GetCurrentCustomerAsync()).Id, Int32.Parse(shopId)); var shoppingCartItems = (await _shoppingCartService.GetShoppingCartAsync(await _workContext.GetCurrentCustomerAsync(), ShoppingCartType.ShoppingCart)) .Select(sci => sci); // update all attribute definitions foreach (var cartItem in shoppingCartItems) { // update attribute definitions to include the proper name ProductAttributeMapping pickupAttribute = await _attributeUtilities.GetPickupAttributeMappingAsync(cartItem.AttributesXml); if (pickupAttribute != null) { // check if product is available at the selected store StockResponse stockResponse = await _backendStockService.GetApiStockAsync(cartItem.ProductId); bool available = false; if (stockResponse != null) { available = stockResponse.ProductStocks.Where(ps => ps.Available && ps.Shop.Id == shop.Id).Any(); } if (available) { string removedAttr = _productAttributeParser.RemoveProductAttribute(cartItem.AttributesXml, pickupAttribute); cartItem.AttributesXml = await _attributeUtilities.InsertPickupAttributeAsync( await _productService.GetProductByIdAsync(cartItem.ProductId), stockResponse, removedAttr); } else { cartItem.AttributesXml = await _attributeUtilities.InsertHomeDeliveryAttributeAsync( await _productService.GetProductByIdAsync(cartItem.ProductId), cartItem.AttributesXml); } await _shoppingCartService.UpdateShoppingCartItemAsync(await _workContext.GetCurrentCustomerAsync(), cartItem.Id, cartItem.AttributesXml, cartItem.CustomerEnteredPrice, null, null, cartItem.Quantity, false); } } return(Json(await _shopService.GetShopByIdAsync(int.Parse(shopId)))); }
/// <summary> /// Deletes a product attribute mapping /// </summary> /// <param name="productAttributeMapping">Product attribute mapping</param> /// <param name="productId">Product ident</param> public virtual async Task DeleteProductAttributeMapping(ProductAttributeMapping productAttributeMapping, string productId) { if (productAttributeMapping == null) { throw new ArgumentNullException(nameof(productAttributeMapping)); } var updatebuilder = Builders <Product> .Update; var update = updatebuilder.PullFilter(p => p.ProductAttributeMappings, y => y.Id == productAttributeMapping.Id); await _productRepository.Collection.UpdateManyAsync(new BsonDocument("_id", productId), update); //cache await _cacheBase.RemoveByPrefix(string.Format(CacheKey.PRODUCTS_BY_ID_KEY, productId)); //event notification await _mediator.EntityDeleted(productAttributeMapping); }
public static bool ShouldHaveValues(this ProductAttributeMapping productAttributeMapping) { if (productAttributeMapping == null) { return(false); } if (productAttributeMapping.AttributeControlTypeId == AttributeControlType.TextBox || productAttributeMapping.AttributeControlTypeId == AttributeControlType.MultilineTextbox || productAttributeMapping.AttributeControlTypeId == AttributeControlType.Datepicker || productAttributeMapping.AttributeControlTypeId == AttributeControlType.FileUpload) { return(false); } return(true); }
public ActionResult ProductAttributeAdd(string productId, string attributecontroltype, string productattributeId, string textprompt) { if (string.IsNullOrEmpty(productId)) { throw new Exception("product Id" + productId); } if (string.IsNullOrEmpty(productattributeId)) { throw new Exception("productattribute Id" + productattributeId); } ProductAttributeMapping _map = new ProductAttributeMapping(); _map.AttributeControlTypeId = Convert.ToInt32(attributecontroltype); _map.ProductAttributeId = Convert.ToInt32(productattributeId); _map.ProductId = Convert.ToInt32(productId); _map.TextPrompt = textprompt; if (_productAttributeMappingService.Insert(_map) != null) { int _productAttributeId = Convert.ToInt32(productattributeId); var data = _predefinedProductAttributeValueService.GetList(x => x.ProductAttributeId == _productAttributeId).ToList(); if (data != null) { var result = false; ProductAttributeValue _value = null; foreach (var item in data) { _value = new ProductAttributeValue(); _value.AttributeValueTypeId = 0; _value.Cost = item.Cost; _value.Name = item.Name; _value.OrderNo = item.OrderNo; _value.Quantity = 0; _value.ProductAttributeMappingId = _map.Id; result = _prodcutAttributeValueService.Insert(_value) != null ? true : false; } if (result) { return(Json("1", JsonRequestBehavior.AllowGet)); } } } return(Json("-1", JsonRequestBehavior.AllowGet)); }
private void UpdateProductAttributes(Product entityToUpdate, Delta <ProductDto> productDtoDelta) { // If no product attribute mappings are specified means we don't have to update anything if (productDtoDelta.Dto.ProductAttributeMappings == null) { return; } // delete unused product attribute mappings IEnumerable <int> toBeUpdatedIds = productDtoDelta.Dto.ProductAttributeMappings.Where(y => y.Id != 0).Select(x => x.Id); var unusedProductAttributeMappings = entityToUpdate.ProductAttributeMappings.Where(x => !toBeUpdatedIds.Contains(x.Id)).ToList(); foreach (var unusedProductAttributeMapping in unusedProductAttributeMappings) { _productAttributeService.DeleteProductAttributeMapping(unusedProductAttributeMapping); } foreach (var productAttributeMappingDto in productDtoDelta.Dto.ProductAttributeMappings) { if (productAttributeMappingDto.Id > 0) { // update existing product attribute mapping var productAttributeMappingToUpdate = entityToUpdate.ProductAttributeMappings.FirstOrDefault(x => x.Id == productAttributeMappingDto.Id); if (productAttributeMappingToUpdate != null) { productDtoDelta.Merge(productAttributeMappingDto, productAttributeMappingToUpdate, false); _productAttributeService.UpdateProductAttributeMapping(productAttributeMappingToUpdate); UpdateProductAttributeValues(productAttributeMappingDto, productDtoDelta); } } else { ProductAttributeMapping newProductAttributeMapping = new ProductAttributeMapping(); newProductAttributeMapping.ProductId = entityToUpdate.Id; productDtoDelta.Merge(productAttributeMappingDto, newProductAttributeMapping); // add new product attribute _productAttributeService.InsertProductAttributeMapping(newProductAttributeMapping); } } }