public virtual async Task <WishList> Process(CommerceContext commerceContext, string wishListId, WishListLineComponent line)
        {
            using (CommandActivity.Start(commerceContext, this))
            {
                var context            = commerceContext.GetPipelineContextOptions();
                var findEntityArgument = new FindEntityArgument(typeof(WishList), wishListId, true);
                var wishList           = await _getPipeline.Run(findEntityArgument, context) as WishList;

                if (wishList == null)
                {
                    await context.CommerceContext.AddMessage(commerceContext.GetPolicy <KnownResultCodes>().ValidationError, "EntityNotFound", new object[] { wishListId }, string.Format("Entity {0} was not found.", wishListId));

                    return(null);
                }

                if (!wishList.IsPersisted)
                {
                    wishList.Id       = wishListId;
                    wishList.Name     = wishListId;
                    wishList.ShopName = commerceContext.CurrentShopName();
                }

                var result = await _addToWishListPipeline.Run(new WishListLineArgument(wishList, line), context);

                await _persistEntityPipeline.Run(new PersistEntityArgument(result), context);

                return(result);
            }
        }
        private Cart GetCart(string cartId, CommerceContext commerceContext, string baseUrl)
        {
            var shopName    = commerceContext.CurrentShopName();
            var shopperId   = commerceContext.CurrentShopperId();
            var customerId  = commerceContext.CurrentCustomerId();
            var environment = commerceContext.Environment.Name;

            var url = string.Format(Constants.Settings.EndpointUrl, baseUrl, cartId);

            var client = new HttpClient();

            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(Constants.Settings.AppJson));
            client.DefaultRequestHeaders.Add(Constants.Settings.ShopName, shopName);
            client.DefaultRequestHeaders.Add(Constants.Settings.ShopperId, shopperId);
            client.DefaultRequestHeaders.Add(Constants.Settings.Language, "en-US");
            client.DefaultRequestHeaders.Add(Constants.Settings.Environment, environment);
            client.DefaultRequestHeaders.Add(Constants.Settings.CustomerId, customerId);
            client.DefaultRequestHeaders.Add(Constants.Settings.Currency, commerceContext.CurrentCurrency());
            client.DefaultRequestHeaders.Add(Constants.Settings.Roles, Constants.Settings.CartRoles);


            try
            {
                var cart = new Cart();

                var response = client.GetAsync(url).Result;

                if (response != null)
                {
                    var task = response.Content.ReadAsStreamAsync().ContinueWith(t =>
                    {
                        var stream = t.Result;
                        using (var reader = new StreamReader(stream))
                        {
                            var responseValue = reader.ReadToEnd();
                            cart = JsonConvert.DeserializeObject <Cart>(responseValue);
                        }
                    });

                    task.Wait();
                }

                client.Dispose();
                return(cart);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                client.Dispose();
                return(null);
            }
        }
        public virtual async Task <Cart> Process(CommerceContext commerceContext, string wishlistId, CartLineComponent line)
        {
            AddWishListLineItemCommand addCartLineCommand = this;
            Cart result = (Cart)null;

            using (CommandActivity.Start(commerceContext, (CommerceCommand)addCartLineCommand))
            {
                await addCartLineCommand.PerformTransaction(commerceContext, (Func <Task>)(async() =>
                {
                    FindEntityArgument findEntityArgument = new FindEntityArgument(typeof(Cart), wishlistId, true);
                    var context = commerceContext.GetPipelineContextOptions();

                    Cart cart = await this._getPipeline.Run(findEntityArgument, (IPipelineExecutionContextOptions)commerceContext.GetPipelineContextOptions()).ConfigureAwait(false) as Cart;
                    if (cart == null)
                    {
                        string str = await context.CommerceContext.AddMessage(commerceContext.GetPolicy <KnownResultCodes>().ValidationError, "EntityNotFound", new object[1]
                        {
                            (object)wishlistId
                        }, string.Format("Entity {0} was not found.", (object)wishlistId));
                    }
                    else
                    {
                        if (!cart.IsPersisted)
                        {
                            cart.Id       = wishlistId;
                            cart.Name     = wishlistId;
                            cart.ShopName = commerceContext.CurrentShopName();
                            cart.SetComponent((Component) new ListMembershipsComponent()
                            {
                                Memberships = (IList <string>) new List <string>()
                                {
                                    CommerceEntity.ListName <Cart>()
                                }
                            });

                            cart.SetComponent(new CartTypeComponent()
                            {
                                CartType = CartTypeEnum.Wishlist.ToString()
                            });
                        }


                        result = await this._addWishListLineItemPipeli.Run(new CartLineArgument(cart, line), (IPipelineExecutionContextOptions)context);
                    }
                }));
            }

            return(result);
        }
        /// <summary>
        /// The process of the command
        /// </summary>
        /// <param name="commerceContext">
        /// The commerce context
        /// </param>
        /// <param name="cartId"></param>
        /// <returns>
        /// The <see cref="Task"/>.
        /// </returns>
        public async Task <Cart> Process(CommerceContext commerceContext, string cartId)
        {
            using (var activity = CommandActivity.Start(commerceContext, this))
            {
                var arg = new ResolveCartArgument(commerceContext.CurrentShopName(), cartId,
                                                  commerceContext.CurrentShopperId());

                var cart = await _getCartPipeline.Run(arg, commerceContext.GetPipelineContextOptions());

                var cartComponent = cart.GetComponent <TestSimulationComponent>();
                cartComponent.Status = true;

                var persistEntityArgument = await this._persistEntityPipeline.Run(new PersistEntityArgument(cart), commerceContext.GetPipelineContextOptions());

                return(cart);
            }
        }
Пример #5
0
        public virtual async Task <WishList> Process(CommerceContext commerceContext, string wishListId, bool secureResult = true)
        {
            using (CommandActivity.Start(commerceContext, this))
            {
                var context = commerceContext.GetPipelineContextOptions();
                var resolveWishListArgument = new ResolveWishListArgument(commerceContext.CurrentShopName(), wishListId, commerceContext.CurrentShopperId());
                var objects = commerceContext.GetObjects <WishList>();
                if (objects.Any(p => p.Id == wishListId))
                {
                    commerceContext.Logger.LogTrace(string.Format("GetWishListCommand.AlreadyLoaded: WishListId={0}", wishListId), Array.Empty <object>());
                    return(objects.FirstOrDefault(p => p.Id == wishListId));
                }
                commerceContext.Logger.LogTrace(string.Format("GetWishListCommand.LoadingWishList: WishListId={0}", wishListId), Array.Empty <object>());
                var wishList = await _pipeline.Run(resolveWishListArgument, context);

                commerceContext.Logger.LogTrace(string.Format("GetWishListCommand.WishListLoaded: WishListId={0}", wishListId), Array.Empty <object>());

                return(wishList);
            }
        }