public string ValidateDuplicateDiscount(DiscountFormViewModel newDiscount, string groupIdentifier = null) { var discounts = _repo.GetAll(); #region Check for Duplicate Brands if (newDiscount.BrandIds != null) { foreach (var item in newDiscount.BrandIds) { if (discounts.Any(d => d.BrandId == item)) { var brandName = _brandRepo.GetAll().FirstOrDefault(b => b.Id == item).Name; var discountName = discounts.FirstOrDefault(d => d.BrandId == item).Title; if (groupIdentifier != null) { if (discounts.FirstOrDefault(d => d.BrandId == item).GroupIdentifier != groupIdentifier) { return($"برای برند {brandName} قبلا تخفیف ثبت شده ( {discountName} )"); } } else { return($"برای برند {brandName} قبلا تخفیف ثبت شده ( {discountName} )"); } } } } #endregion #region Check for Duplicate ProductGroups if (newDiscount.ProductGroupIds != null) { foreach (var item in newDiscount.ProductGroupIds) { if (discounts.Any(d => d.ProductGroupId == item)) { var productGroupName = _productGroupRepo.GetAll().FirstOrDefault(b => b.Id == item).Title; var discountName = discounts.FirstOrDefault(d => d.ProductGroupId == item).Title; if (groupIdentifier != null) { if (discounts.FirstOrDefault(d => d.ProductGroupId == item).GroupIdentifier != groupIdentifier) { return($"برای دسته {productGroupName} قبلا تخفیف ثبت شده ( {discountName} )"); } } else { return($"برای دسته {productGroupName} قبلا تخفیف ثبت شده ( {discountName} )"); } } } } #endregion #region Check for Duplicate Products if (newDiscount.ProductIds != null) { foreach (var item in newDiscount.ProductIds) { if (discounts.Any(d => d.ProductId == item)) { var productName = _productRepo.GetAll().FirstOrDefault(b => b.Id == item).Title; var discountName = discounts.FirstOrDefault(d => d.ProductId == item).Title; if (groupIdentifier != null) { if (discounts.FirstOrDefault(d => d.ProductId == item).GroupIdentifier != groupIdentifier) { return($"برای محصول {productName} قبلا تخفیف ثبت شده ( {discountName} )"); } } else { return($"برای محصول {productName} قبلا تخفیف ثبت شده ( {discountName} )"); } } } } #endregion return("valid"); }