/// <summary> /// Deletes a related product /// </summary> /// <param name="relatedProduct">Related product</param> public virtual void DeleteRelatedProduct(RelatedProduct relatedProduct) { if (relatedProduct == null) throw new ArgumentNullException("relatedProduct"); _relatedProductRepository.Delete(relatedProduct); //event notification _eventPublisher.EntityDeleted(relatedProduct); }
public virtual void InsertRelatedProduct(RelatedProduct relatedProduct) { if (relatedProduct == null) throw new ArgumentNullException("relatedProduct"); //_relatedProductRepository.Insert(relatedProduct); var updatebuilder = Builders<Product>.Update; var update = updatebuilder.AddToSet(p => p.RelatedProducts, relatedProduct); _productRepository.Collection.UpdateOneAsync(new BsonDocument("Id", relatedProduct.ProductId1), update); //cache _cacheManager.RemoveByPattern(string.Format(PRODUCTS_BY_ID_KEY, relatedProduct.ProductId1)); //event notification _eventPublisher.EntityInserted(relatedProduct); }
/// <summary> /// Updates a related product /// </summary> /// <param name="relatedProduct">Related product</param> public virtual void UpdateRelatedProduct(RelatedProduct relatedProduct) { if (relatedProduct == null) throw new ArgumentNullException("relatedProduct"); //_relatedProductRepository.Update(relatedProduct); var builder = Builders<Product>.Filter; var filter = builder.Eq(x => x.Id, relatedProduct.ProductId1); filter = filter & builder.ElemMatch(x => x.RelatedProducts, y => y.Id == relatedProduct.Id); var update = Builders<Product>.Update .Set(x => x.RelatedProducts.ElementAt(-1).DisplayOrder, relatedProduct.DisplayOrder); var result = _productRepository.Collection.UpdateManyAsync(filter, update).Result; //cache _cacheManager.RemoveByPattern(string.Format(PRODUCTS_BY_ID_KEY, relatedProduct.ProductId1)); //event notification _eventPublisher.EntityUpdated(relatedProduct); }
public ActionResult RelatedProductAddPopup(string btnId, string formId, ProductModel.AddRelatedProductModel model) { if (!_permissionService.Authorize(StandardPermissionProvider.ManageProducts)) return AccessDeniedView(); if (model.SelectedProductIds != null) { var productId1 = _productService.GetProductById(model.ProductId); foreach (int id in model.SelectedProductIds) { var product = _productService.GetProductById(id); if (product != null) { //a vendor should have access only to his products if (_workContext.CurrentVendor != null && product.VendorId != _workContext.CurrentVendor.Id) continue; var existingRelatedProducts = productId1.RelatedProducts; //_productService.GetRelatedProductsByProductId1(model.ProductId); //if (existingRelatedProducts.FindRelatedProduct(model.ProductId, id) == null) if (model.ProductId != id) if (existingRelatedProducts.Where(x => x.ProductId2 == id).Count() == 0) { var related = new RelatedProduct { ProductId1 = model.ProductId, ProductId2 = id, DisplayOrder = 1, _id = ObjectId.GenerateNewId().ToString(), Id = productId1.RelatedProducts.Count > 0 ? productId1.RelatedProducts.Max(x => x.Id) + 1 : 1 }; productId1.RelatedProducts.Add(related); _productService.InsertRelatedProduct(related); } } } } //a vendor should have access only to his products model.IsLoggedInAsVendor = _workContext.CurrentVendor != null; ViewBag.RefreshPage = true; ViewBag.btnId = btnId; ViewBag.formId = formId; return View(model); }