public async Task WHEN_parameters_ok_SHOULD_invoke_Client()
        {
            //Arrange
            var p = new CouponParam()
            {
                CartName    = GetRandom.String(10),
                CouponCode  = GetRandom.String(10),
                CultureInfo = CultureInfo.CurrentCulture,
                CustomerId  = GetRandom.Guid(),
                Scope       = GetRandom.String(10)
            };
            var ovClientMock = GetClientMock();

            Container.Use(ovClientMock);
            var sut = Container.CreateInstance <CartRepository>();

            //Act
            var cart = await sut.AddCouponAsync(p);

            //Assert
            ovClientMock.Verify();
            cart.Should().NotBeNull();

            cart.Name.Should().Be(p.CartName);
            cart.Coupons.Should().NotBeNullOrEmpty();
            cart.Coupons.First().CouponCode.Should().Be(p.CouponCode);
            cart.CultureName.Should().Be(p.CultureInfo.Name);
            cart.CustomerId.Should().Be(p.CustomerId);
            cart.ScopeId.Should().Be(p.Scope);
        }
示例#2
0
        public async Task <string> QueryPagedAsync([FromQuery] CouponParam param)
        {
            var filter = param.SearchLambda <Coupon, CouponParam>();
            var result = await _couponRepository.QueryPagedAsync(param.PageNum, param.PageSize, null, filter);

            var dtos = new List <CouponDto>();

            foreach (var coupon in result.Items)
            {
                var productCategoryRelationList = await _couponProductCategoryRelationRepository.QueryAsync(c => c.CouponId == coupon.Id);

                var productRelationList = await _couponProductRelationRepository.QueryAsync(c => c.CouponId == coupon.Id);

                var dto = coupon.EntityMap <Coupon, CouponDto>();
                dto.ProductCategoryRelationList = productCategoryRelationList.ToList();
                dto.ProductRelationList         = productRelationList.ToList();
                dtos.Add(dto);
            }
            var pageData = new PagedDto <CouponDto>
            {
                Code     = 200,
                Msg      = "获取数据成功",
                Total    = result.TotalResults,
                PageSize = param.PageSize,
                Data     = dtos
            };
            var json = pageData.ToString();

            return(json);
        }
示例#3
0
        public async Task WHEN_parameters_ok_SHOULD_call_repo_cleanup_invalid_coupons_and_add_msg()
        {
            //Arrange
            var p = new CouponParam()
            {
                CartName    = GetRandom.String(10),
                CouponCode  = GetRandom.String(6),
                CultureInfo = CultureInfo.InvariantCulture,
                CustomerId  = GetRandom.Guid(),
                Scope       = GetRandom.String(5),
                BaseUrl     = GetRandom.String(32)
            };

            Container.Use(CreateCartRepoMock(CartRepositoryFactory.CreateCartBasedOnAddCouponRequest, CouponState.Ok));

            var sut = Container.CreateInstance <CouponViewService>();

            //Act
            var vm = await sut.AddCouponAsync(p);

            //Assert
            vm.Should().NotBeNull();
            vm.Coupons.Messages.Where(m => m.Level == CartMessageLevels.Success).Should().NotBeEmpty();
            Container.Verify <ICartRepository>();
        }
        /// <summary>
        /// Adds a coupon to the Cart.
        /// </summary>
        /// <param name="param"></param>
        /// <returns>The lightweight CartViewModel</returns>
        public virtual async Task <CartViewModel> AddCouponAsync(CouponParam param)
        {
            var cart = await CartRepository.AddCouponAsync(param).ConfigureAwait(false);

            await CartRepository.RemoveCouponsAsync(new RemoveCouponsParam
            {
                CartName    = param.CartName,
                CouponCodes = GetInvalidCouponsCode(cart.Coupons).ToList(),
                CustomerId  = param.CustomerId,
                Scope       = param.Scope
            }).ConfigureAwait(false);

            var vmParam = new CreateCartViewModelParam
            {
                Cart        = cart,
                CultureInfo = param.CultureInfo,
                IncludeInvalidCouponsMessages = true,
                BaseUrl = param.BaseUrl
            };

            var viewModel = await CreateCartViewModelAsync(vmParam).ConfigureAwait(false);

            AddSuccessMessageIfRequired(param, viewModel, param.CultureInfo);

            return(viewModel);
        }
        public void WHEN_param_is_null_SHOULD_throw_ArgumentNullException()
        {
            //Arrange
            CouponParam p   = null;
            var         sut = Container.CreateInstance <CartRepository>();

            //Act
            var ex = Assert.ThrowsAsync <ArgumentNullException>(() => sut.AddCouponAsync(p));

            //Assert
            ex.Message.Should().ContainEquivalentOf("param");
        }
        public void WHEN_parameter_is_null_SHOULD_throw_NullArgumentException()
        {
            //Arrange
            CouponParam p = null;

            var sut = Container.CreateInstance <CouponViewService>();

            //Act
            var ex = Assert.ThrowsAsync <ArgumentNullException>(() => sut.RemoveCouponAsync(p));

            //Assert
            ex.ParamName.Should().BeEquivalentTo("param");
        }
示例#7
0
        private AddCouponRequest MapParamToRequest(CouponParam parameters)
        {
            var request = new AddCouponRequest()
            {
                CartName    = parameters.CartName,
                CouponCode  = parameters.CouponCode,
                CultureName = parameters.CultureInfo.Name,
                CustomerId  = parameters.CustomerId,
                ScopeId     = parameters.Scope,
            };

            return(request);
        }
        public void WHEN_CartName_is_null_or_whitespace_SHOULD_throw_ArgumentException(string cartName)
        {
            //Arrange
            var p = new CouponParam()
            {
                CartName    = cartName,
                CouponCode  = GetRandom.String(10),
                CultureInfo = CultureInfo.CurrentCulture,
                CustomerId  = GetRandom.Guid(),
                Scope       = GetRandom.String(10)
            };
            var sut = Container.CreateInstance <CartRepository>();

            //Act
            var ex = Assert.ThrowsAsync <ArgumentException>(() => sut.AddCouponAsync(p));

            //Assert
            ex.ParamName.Should().ContainEquivalentOf("param");
        }
        protected virtual void AddSuccessMessageIfRequired(CouponParam param, CartViewModel viewModel, CultureInfo cultureInfo)
        {
            if (viewModel.Coupons.ApplicableCoupons.Any(
                    c => string.Equals(c.CouponCode, param.CouponCode, StringComparison.InvariantCultureIgnoreCase)))
            {
                var templateMessage = LocalizationProvider.GetLocalizedString(new GetLocalizedParam
                {
                    Category    = "ShoppingCart",
                    Key         = "F_PromoCodeSucces",
                    CultureInfo = cultureInfo
                });

                viewModel.Coupons.Messages.Add(new CartMessageViewModel
                {
                    Message = string.Format(templateMessage, param.CouponCode),
                    Level   = CartMessageLevels.Success
                });
            }
        }
示例#10
0
        /// <summary>
        /// Removes a coupon from the Cart.
        /// </summary>
        /// <param name="param"></param>
        /// <returns>The lightweight CartViewModel</returns>
        public virtual async Task <CartViewModel> RemoveCouponAsync(CouponParam param)
        {
            if (param == null)
            {
                throw new ArgumentNullException(nameof(param));
            }

            await CartRepository.RemoveCouponsAsync(new RemoveCouponsParam
            {
                CartName    = param.CartName,
                CouponCodes = param.CouponCode == null
                    ? null
                    : new List <string>
                {
                    param.CouponCode
                },
                CustomerId = param.CustomerId,
                Scope      = param.Scope
            }).ConfigureAwait(false);

            var cart = await CartRepository.GetCartAsync(new GetCartParam
            {
                CartName        = param.CartName,
                CultureInfo     = param.CultureInfo,
                CustomerId      = param.CustomerId,
                Scope           = param.Scope,
                ExecuteWorkflow = true
            }).ConfigureAwait(false);

            var vmParam = new CreateCartViewModelParam
            {
                Cart        = cart,
                CultureInfo = param.CultureInfo,
                IncludeInvalidCouponsMessages = true,
                BaseUrl = param.BaseUrl
            };

            var viewModel = await CreateCartViewModelAsync(vmParam).ConfigureAwait(false);

            return(viewModel);
        }
示例#11
0
        /// <summary>
        /// Adds a coupon to the Cart, then returns an instance of the cart.
        /// </summary>
        /// <param name="param"></param>
        /// <returns>The full and updated cart details.</returns>
        public virtual Task <ProcessedCart> AddCouponAsync(CouponParam param)
        {
            if (param == null)
            {
                throw new ArgumentNullException("param");
            }
            if (string.IsNullOrWhiteSpace(param.Scope))
            {
                throw new ArgumentException("Scope may not be null or whitespace.", "param");
            }
            if (param.CustomerId == Guid.Empty)
            {
                throw new ArgumentException("CustomerId may not be Empty", "param");
            }
            if (string.IsNullOrWhiteSpace(param.CartName))
            {
                throw new ArgumentException("CartName may not be null or whitespace.", "param");
            }
            if (param.CultureInfo == null)
            {
                throw new ArgumentNullException("param", "CultureInfo cannot be null.");
            }
            if (string.IsNullOrWhiteSpace(param.CouponCode))
            {
                throw new ArgumentException("CouponCode may not be null or whitespace", "param");
            }

            var request = new AddCouponRequest
            {
                CartName    = param.CartName,
                CouponCode  = param.CouponCode,
                CultureName = param.CultureInfo.Name,
                CustomerId  = param.CustomerId,
                ScopeId     = param.Scope
            };

            var cacheKey = BuildCartCacheKey(param.Scope, param.CustomerId, param.CartName);

            return(CacheProvider.ExecuteAndSetAsync(cacheKey, () => OvertureClient.SendAsync(request)));
        }
示例#12
0
        /// <summary>
        /// Adds a coupon to the Cart, then returns an instance of the cart.
        /// </summary>
        /// <param name="param"></param>
        /// <returns>The full and updated cart details.</returns>
        public virtual Task <ProcessedCart> AddCouponAsync(CouponParam param)
        {
            if (param == null)
            {
                throw new ArgumentNullException(nameof(param));
            }
            if (string.IsNullOrWhiteSpace(param.Scope))
            {
                throw new ArgumentException(GetMessageOfNullWhiteSpace(nameof(param.Scope)), nameof(param));
            }
            if (param.CustomerId == Guid.Empty)
            {
                throw new ArgumentException(GetMessageOfEmpty(nameof(param.CustomerId)), nameof(param));
            }
            if (string.IsNullOrWhiteSpace(param.CartName))
            {
                throw new ArgumentException(GetMessageOfNullWhiteSpace(nameof(param.CartName)), nameof(param));
            }
            if (param.CultureInfo == null)
            {
                throw new ArgumentException(GetMessageOfNull(nameof(param.CultureInfo)), nameof(param));
            }
            if (string.IsNullOrWhiteSpace(param.CouponCode))
            {
                throw new ArgumentException(GetMessageOfNullWhiteSpace(nameof(param.CouponCode)), nameof(param));
            }

            var request = new AddCouponRequest
            {
                CartName    = param.CartName,
                CouponCode  = param.CouponCode,
                CultureName = param.CultureInfo.Name,
                CustomerId  = param.CustomerId,
                ScopeId     = param.Scope
            };

            var cacheKey = BuildCartCacheKey(param.Scope, param.CustomerId, param.CartName);

            return(CacheProvider.ExecuteAndSetAsync(cacheKey, () => OvertureClient.SendAsync(request)));
        }
        public void WHEN_parameter_is_ok_SHOULD_return_cartViewModel()
        {
            //Arrange
            Container.Use(CartRepositoryFactory.Create());
            Container.Use(CartViewModelFactoryMock.Create());

            var p = new CouponParam()
            {
                CartName    = GetRandom.String(7),
                CouponCode  = GetRandom.String(10),
                CultureInfo = CultureInfo.InvariantCulture,
                CustomerId  = GetRandom.Guid(),
                Scope       = GetRandom.String(7)
            };

            var sut = Container.CreateInstance <CouponViewService>();

            //Act
            var vm = sut.RemoveCouponAsync(p);

            //Assert
            vm.Should().NotBeNull();
        }
示例#14
0
        public async Task WHEN_CouponCode_is_expired_SHOULD_call_repo_and_cleanup_invalid_coupons()
        {
            //Arrange
            var p = new CouponParam()
            {
                CartName    = GetRandom.String(10),
                CouponCode  = GetRandom.String(6),
                CultureInfo = CultureInfo.InvariantCulture,
                CustomerId  = GetRandom.Guid(),
                Scope       = GetRandom.String(5),
                BaseUrl     = GetRandom.String(32)
            };

            Container.Use(CreateCartRepoMock(CartRepositoryFactory.CreateCartBasedOnAddCouponRequest, CouponState.Expired));

            var sut = Container.CreateInstance <CouponViewService>();

            //Act
            var vm = await sut.AddCouponAsync(p);

            //Assert
            vm.Should().NotBeNull();
            Container.Verify <ICartRepository>();
        }
 public Task <ProcessedCart> AddCouponAsync(CouponParam param)
 {
     throw new NotImplementedException();
 }