public async Task <IActionResult> Post([FromBody] CartRuleForm model)
        {
            if (ModelState.IsValid)
            {
                var cartRule = new CartRule
                {
                    Name                  = model.Name,
                    Description           = model.Description,
                    IsActive              = model.IsActive,
                    StartOn               = model.StartOn,
                    EndOn                 = model.EndOn,
                    IsCouponRequired      = model.IsCouponRequired,
                    RuleToApply           = model.RuleToApply,
                    DiscountAmount        = model.DiscountAmount,
                    DiscountStep          = model.DiscountStep,
                    MaxDiscountAmount     = model.MaxDiscountAmount,
                    UsageLimitPerCoupon   = model.UsageLimitPerCoupon,
                    UsageLimitPerCustomer = model.UsageLimitPerCustomer
                };

                if (model.IsCouponRequired && !string.IsNullOrWhiteSpace(model.CouponCode))
                {
                    var coupon = new Coupon
                    {
                        CartRule = cartRule,
                        Code     = model.CouponCode
                    };

                    cartRule.Coupons.Add(coupon);
                }

                foreach (var item in model.Products)
                {
                    var cartRuleProduct = new CartRuleProduct
                    {
                        CartRule  = cartRule,
                        ProductId = item.Id
                    };
                    cartRule.Products.Add(cartRuleProduct);
                }

                _cartRuleRepository.Add(cartRule);
                await _cartRuleRepository.SaveChangesAsync();

                return(CreatedAtAction(nameof(Get), new { id = cartRule.Id }, null));
            }
            return(BadRequest(ModelState));
        }
        public async Task <IActionResult> Put(long id, [FromBody] CartRuleForm model)
        {
            if (ModelState.IsValid)
            {
                var cartRule = await _cartRuleRepository.Query()
                               .Include(x => x.Coupons)
                               .Include(x => x.Products)
                               .FirstOrDefaultAsync(x => x.Id == id);

                if (cartRule == null)
                {
                    return(NotFound());
                }

                cartRule.Name                  = model.Name;
                cartRule.Description           = model.Description;
                cartRule.StartOn               = model.StartOn;
                cartRule.EndOn                 = model.EndOn;
                cartRule.IsActive              = model.IsActive;
                cartRule.IsCouponRequired      = model.IsCouponRequired;
                cartRule.RuleToApply           = model.RuleToApply;
                cartRule.DiscountAmount        = model.DiscountAmount;
                cartRule.DiscountStep          = model.DiscountStep;
                cartRule.MaxDiscountAmount     = model.MaxDiscountAmount;
                cartRule.UsageLimitPerCoupon   = model.UsageLimitPerCoupon;
                cartRule.UsageLimitPerCustomer = model.UsageLimitPerCustomer;

                if (model.IsCouponRequired && !string.IsNullOrWhiteSpace(model.CouponCode))
                {
                    var coupon = cartRule.Coupons.FirstOrDefault();
                    if (coupon == null)
                    {
                        coupon = new Coupon
                        {
                            CartRule = cartRule,
                            Code     = model.CouponCode
                        };

                        cartRule.Coupons.Add(coupon);
                    }
                    else
                    {
                        coupon.Code = model.CouponCode;
                    }
                }

                foreach (var item in model.Products)
                {
                    var cartRuleProduct = cartRule.Products.FirstOrDefault(x => x.ProductId == item.Id);
                    if (cartRuleProduct == null)
                    {
                        cartRuleProduct = new CartRuleProduct
                        {
                            CartRule  = cartRule,
                            ProductId = item.Id
                        };
                        cartRule.Products.Add(cartRuleProduct);
                    }
                }

                var modelProductIds = model.Products.Select(x => x.Id);
                var deletedProducts = cartRule.Products.Where(x => !modelProductIds.Contains(x.ProductId)).ToList();
                foreach (var item in deletedProducts)
                {
                    item.CartRule = null;
                    cartRule.Products.Remove(item);
                }

                await _cartRuleRepository.SaveChangesAsync();

                return(Accepted());
            }
            return(BadRequest(ModelState));
        }