public virtual IActionResult CategoryAddPopup(AddCategoryToDiscountModel 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");

            foreach (var id in model.SelectedCategoryIds)
            {
                var category = _categoryService.GetCategoryById(id);
                if (category == null)
                {
                    continue;
                }

                if (category.DiscountCategoryMappings.Count(mapping => mapping.DiscountId == discount.Id) == 0)
                {
                    category.DiscountCategoryMappings.Add(new DiscountCategoryMapping {
                        Discount = discount
                    });
                }

                _categoryService.UpdateCategory(category);
            }

            ViewBag.RefreshPage = true;

            return(View(new AddCategoryToDiscountSearchModel()));
        }
        /// <returns>A task that represents the asynchronous operation</returns>
        public virtual async Task <IActionResult> CategoryAddPopup(AddCategoryToDiscountModel 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");

            foreach (var id in model.SelectedCategoryIds)
            {
                var category = await _categoryService.GetCategoryByIdAsync(id);

                if (category == null)
                {
                    continue;
                }

                if (await _categoryService.GetDiscountAppliedToCategoryAsync(category.Id, discount.Id) is null)
                {
                    await _categoryService.InsertDiscountCategoryMappingAsync(new DiscountCategoryMapping { DiscountId = discount.Id, EntityId = category.Id });
                }

                await _categoryService.UpdateCategoryAsync(category);
            }

            ViewBag.RefreshPage = true;

            return(View(new AddCategoryToDiscountSearchModel()));
        }