public virtual IActionResult ProductAddPopup(AddProductToDiscountModel model) { if (!_permissionService.Authorize(StandardPermissionProvider.ManageDiscounts)) { return(AccessDeniedView()); } //try to get a discount with the specified id var discount = _discountService.GetDiscountById(model.DiscountId) ?? throw new ArgumentException("No discount found with the specified id"); var selectedProducts = _productService.GetProductsByIds(model.SelectedProductIds.ToArray()); if (selectedProducts.Any()) { foreach (var product in selectedProducts) { if (product.DiscountProductMappings.Count(mapping => mapping.DiscountId == discount.Id) == 0) { product.DiscountProductMappings.Add(new DiscountProductMapping { Discount = discount }); } _productService.UpdateProduct(product); _productService.UpdateHasDiscountsApplied(product); } } ViewBag.RefreshPage = true; return(View(new AddProductToDiscountSearchModel())); }
/// <returns>A task that represents the asynchronous operation</returns> public virtual async Task <IActionResult> ProductAddPopup(AddProductToDiscountModel model) { if (!await _permissionService.AuthorizeAsync(StandardPermissionProvider.ManageDiscounts)) { return(AccessDeniedView()); } //try to get a discount with the specified id var discount = await _discountService.GetDiscountByIdAsync(model.DiscountId) ?? throw new ArgumentException("No discount found with the specified id"); var selectedProducts = await _productService.GetProductsByIdsAsync(model.SelectedProductIds.ToArray()); if (selectedProducts.Any()) { foreach (var product in selectedProducts) { if (await _productService.GetDiscountAppliedToProductAsync(product.Id, discount.Id) is null) { await _productService.InsertDiscountProductMappingAsync(new DiscountProductMapping { EntityId = product.Id, DiscountId = discount.Id }); } await _productService.UpdateProductAsync(product); await _productService.UpdateHasDiscountsAppliedAsync(product); } } ViewBag.RefreshPage = true; return(View(new AddProductToDiscountSearchModel())); }