public List <InvalidVoucher> Validate( List <Product> products, List <OfferVoucher> vouchers) { var groupedProducts = products.GroupBy(product => product.Category) .Select(grouping => new { category = grouping.Key, totalValue = grouping.Sum(g => g.Value) }).ToList(); var invalidVouchers = new List <InvalidVoucher>(); var validVouchers = new List <OfferVoucher>(); foreach (var offerVoucher in vouchers) { if (offerVoucher.CategoryRestriction != null) { var productsInCaegory = groupedProducts.SingleOrDefault( p => p.category == offerVoucher.CategoryRestriction); if (productsInCaegory == null) { invalidVouchers.Add(new InvalidVoucher(offerVoucher, "There are no products in your basket applicable to this voucher.")); continue; } } validVouchers.Add(offerVoucher); } invalidVouchers.AddRange(_offerThresholdValidator.Validate(products, validVouchers)); return(invalidVouchers); }
public List <InvalidVoucher> Validate(List <Product> products, List <OfferVoucher> vouchers) { var productsWithoutGiftVouchers = products .Where(p => p.Category != ProductCategory.GiftVoucher).ToList(); var basketTotal = productsWithoutGiftVouchers.Sum(v => v.Value); var invalidVouchers = new List <InvalidVoucher>(); var validVouchers = new List <OfferVoucher>(); foreach (var offerVoucher in vouchers) { if (offerVoucher.Threshold > basketTotal) { invalidVouchers.Add(new InvalidVoucher(offerVoucher, "You have not reached the spend threshold. " + $"Spend another {((offerVoucher.Threshold - basketTotal) + 0.01m).ToString("C")} " + $"to receive {offerVoucher.Value.ToString("C")} discount.")); continue; } validVouchers.Add(offerVoucher); } invalidVouchers.AddRange(_singleOfferVoucherValidator.Validate(products, validVouchers)); return(invalidVouchers); }
public List <InvalidVoucher> Validate(List <Product> products, List <Voucher> vouchers) { var offerVouchers = vouchers.OfType <OfferVoucher>().ToList(); return(_offerVoucherValidator.Validate(products, offerVouchers)); }