private DiscountCodeValidator MockedValidator(DiscountCode discountCode)
        {
            var variantRepo = new Mock<IRepository<ProductVariant>>();
              variantRepo.Setup(r => r.Find(It.IsAny<int>())).Returns<ProductVariant>(null);

              return MockedValidator(discountCode, variantRepo.Object);
        }
 public DiscountCodeApplier(DiscountCode discountCode)
 {
     _discountCode = discountCode;
 }
        private DiscountCodeValidator MockedValidator(DiscountCode discountCode,
      IRepository<ProductVariant> variantRepo)
        {
            var code = discountCode == null ? "invalid" : discountCode.Code;
              var service = new Mock<IDiscountCodeQueryService>();
              service.Setup(r => r.FindByCode(code)).Returns(discountCode);

              return new DiscountCodeValidator(service.Object, variantRepo);
        }
        private void UpdateTargets(DiscountCode discountCode, DiscountCodeEdit discountCodeEdit)
        {
            if (discountCode.Tours == null)
            discountCode.Tours = new List<Tour>();
              discountCode.Tours.Clear();
              if (!discountCode.IsGlobal && discountCodeEdit.TourId != null)
            discountCode.Tours =
              discountCodeEdit.TourId.Select(i => _tourQueryService.FindTour(Convert.ToInt32(i))).ToList();

              if (discountCode.Products == null)
            discountCode.Products = new List<Product>();
              discountCode.Products.Clear();
              if (!discountCode.IsGlobal && discountCodeEdit.ProductId != null)
            discountCode.Products =
              discountCodeEdit.ProductId.Select(i => _productQueryService.FindProduct(Convert.ToInt32(i))).ToList();

              if (discountCode.ProductVariants == null)
            discountCode.ProductVariants = new List<ProductVariant>();
              discountCode.ProductVariants.Clear();
              if (!discountCode.IsGlobal && discountCodeEdit.ProductVariantId != null)
            discountCode.ProductVariants =
              discountCodeEdit.ProductVariantId.Select(i => _productQueryService.FindProductVariant(Convert.ToInt32(i))).ToList();
        }
        private DiscountCodeEdit BuildEditModel(DiscountCodeEdit discountCodeEdit, DiscountCode discountCode)
        {
            var tours = _tourQueryService.Tours().ToList();
              discountCodeEdit.TourItems =
            tours.Select(
              t =>
            new SelectListItem
            {
              Text = string.Format("{0} - {1}", t.Code, t.Name),
              Value = t.Id.ToString(CultureInfo.InvariantCulture)
            });

              var firstTour = tours.FirstOrDefault();
              if (firstTour != null && firstTour.Departures.Any())
              {
            discountCodeEdit.ProductItems =
              firstTour.Departures.Select(d => d.Product)
            .Select(
              p =>
                new SelectListItem
                {
                  Text = string.Format("{0} - {1}", p.Departure.Code, p.Name),
                  Value = p.Id.ToString(CultureInfo.InvariantCulture)
                });

            var firstProduct = firstTour.Departures.First().Product;
            if (firstProduct != null)
            {
              discountCodeEdit.ProductVariantItems =
            firstProduct.ProductVariants.Select(
              pv => new SelectListItem
              {
                Text = string.Format("{0} - {1}", pv.Product.Departure.Code, pv.DisplayName),
                Value = pv.Id.ToString(CultureInfo.InvariantCulture)
              });
            }
              }

              if (discountCode.Tours != null)
              {
            discountCodeEdit.ToursJson =
              new JavaScriptSerializer().Serialize(
            discountCode.Tours.Select(
              t => new { id = t.Id, name = string.Format("{0} - {1}", t.Code, t.Name), targetType = "TourId" }));
              }

              if (discountCode.Products != null)
              {
            discountCodeEdit.ProductsJson =
              new JavaScriptSerializer().Serialize(
            discountCode.Products.Select(
              p =>
                new { id = p.Id, name = string.Format("{0} - {1}", p.Departure.Code, p.Name), targetType = "ProductId" }));
              }

              if (discountCode.ProductVariants != null)
              {
            discountCodeEdit.ProductVariantsJson =
              new JavaScriptSerializer().Serialize(
            discountCode.ProductVariants.Select(
              v =>
                new
                {
                  id = v.Id,
                  name = string.Format("{0} - {1}", v.Product.Departure.Code, v.DisplayName),
                  targetType = "ProductVariantId"
                }));
              }
              return discountCodeEdit;
        }
 private DiscountCodeEdit BuildEditModel(DiscountCode discountCode)
 {
     var edit = Mapper.Map<DiscountCodeEdit>(discountCode);
       return BuildEditModel(edit, discountCode);
 }
 public void Update(DiscountCode discountCode)
 {
     _discountCodeRepo.Update(discountCode);
 }
 public void Add(DiscountCode discountCode)
 {
     _discountCodeRepo.Add(discountCode);
 }