Exemplo n.º 1
0
        public virtual async Task <CommerceCommand> Process(CommerceContext commerceContext, string customerId, string productId)
        {
            RemoveWishlistCommand removeWishlistCommand = this;

            try
            {
                var customerEntity = await _getCustomerCommand.Process(commerceContext, customerId);

                if (customerEntity != null)
                {
                    if (customerEntity.HasComponent <WishlistComponent>())
                    {
                        WishlistComponent wishlistComponent = customerEntity.GetComponent <WishlistComponent>();
                        if (wishlistComponent.WishlistCollection != null)
                        {
                            foreach (var item in wishlistComponent.WishlistCollection)
                            {
                                if (item.ProductId == productId)
                                {
                                    wishlistComponent.WishlistCollection.Remove(item);

                                    if (wishlistComponent.WishlistCollection.Count == 0)
                                    {
                                        customerEntity.RemoveComponents(wishlistComponent);
                                    }
                                    else
                                    {
                                        customerEntity.SetComponent(wishlistComponent);
                                    }
                                    break;
                                }
                            }
                        }
                        else
                        {
                            commerceContext.Logger.LogInformation($"start process of RemoveWishlistCommand has no Wishlist Component");
                        }
                    }
                    else
                    {
                        commerceContext.Logger.LogDebug($"start process of RemoveWishlistCommand has no Wishlist Component");
                    }

                    await this._persistEntityPipeline.Run(new PersistEntityArgument(customerEntity), commerceContext.PipelineContext);
                }
                else
                {
                    commerceContext.Logger.LogDebug($"start process of RemoveProductIdCommand has no Customer with this Id");
                }
            }
            catch (Exception ex)
            {
                commerceContext.Logger.LogError(ex.Message, ex);
            }
            return(removeWishlistCommand);
        }
        public virtual async Task <CommerceCommand> Process(CommerceContext commerceContext, WishlistModel wishlistModel)
        {
            AddWishlistCommand addWishlistCommand = this;

            try
            {
                var customerEntity = await _getCustomerCommand.Process(commerceContext, wishlistModel.CustomerId);

                if (customerEntity != null)
                {
                    if (customerEntity.HasComponent <WishlistComponent>())
                    {
                        var component = customerEntity.GetComponent <WishlistComponent>();
                        // Checking if product is already added to list or not
                        bool isProductAdded = false;
                        if (component.WishlistCollection == null)
                        {
                            component.WishlistCollection = new List <WishlistEntity>();
                        }
                        else
                        {
                            isProductAdded = component.WishlistCollection.Any(x => x.ProductId == wishlistModel.ProductId);
                        }

                        if (!isProductAdded)
                        {
                            WishlistEntity wishlistEntity = new WishlistEntity()
                            {
                                ProductId    = wishlistModel.ProductId,
                                ProductTitle = wishlistModel.ProductTitle,
                                ProductPrice = wishlistModel.ProductPrice
                            };
                            component.WishlistCollection.Add(wishlistEntity);
                            customerEntity.SetComponent(component);
                            commerceContext.Logger.LogInformation($"AddWishlistCommand for customer id: {wishlistModel.CustomerId} added product Id: {wishlistModel.ProductId}");
                        }
                        else
                        {
                            commerceContext.Logger.LogInformation($"AddWishlistCommand for customer id: {wishlistModel.CustomerId} NOT ADDED product Id: {wishlistModel.ProductId} as it already exists");
                        }
                    }
                    else
                    {
                        WishlistComponent wishlistComponent = new WishlistComponent();
                        wishlistComponent.WishlistCollection = new List <WishlistEntity>();
                        WishlistEntity wishlistEntity = new WishlistEntity()
                        {
                            ProductId    = wishlistModel.ProductId,
                            ProductTitle = wishlistModel.ProductTitle,
                            ProductPrice = wishlistModel.ProductPrice
                        };
                        wishlistComponent.WishlistCollection.Add(wishlistEntity);
                        customerEntity.SetComponent(wishlistComponent);
                    }
                    await this._persistEntityPipeline.Run(new PersistEntityArgument(customerEntity), commerceContext.PipelineContext);
                }
            }
            catch (Exception ex)
            {
                commerceContext.Logger.LogError($"Exception occured in getting customer { ex.StackTrace} and id is {ex.Message}");
            }
            return(addWishlistCommand);
        }