public ActionResult AddToCart(long productId, int quantity)
        {
            var user = _loggedInUser.GetLoggedInUser(System.Web.HttpContext.Current);

            if (user == null)
            {
                throw new HttpException("Unable to find User");
            }
            try
            {
                var settings = new ConnectionSettings(new Uri("http://127.0.0.1:9200")).DefaultIndex("usercart");
                var client   = new ElasticClient(settings);

                var dataToIndex = new IndexToElasticSearch()
                {
                    Id = user.Id,
                };
                dataToIndex.Items.Add(productId, quantity);

                var indexExist = client.Get <IndexToElasticSearch>(new GetRequest("usercart", "IndexToElasticSearch", user.Id));

                if (!indexExist.Found)
                {
                    var indexResponse       = client.Index(dataToIndex, descriptor => descriptor.Type("IndexToElasticSearch").Id(user.Id));
                    var indexResponseResult = indexResponse.ApiCall.Success;
                    if (!indexResponseResult)
                    {
                        throw  new ElasticsearchClientException("Index could not be created");
                    }
                }
                else if (indexExist.IsValid)
                {
                    var updateResponse = client.Update <IndexToElasticSearch, PartialIndexToElasticSearch>(dataToIndex, descriptor =>
                    {
                        var partialIndexToElasticSearch = new PartialIndexToElasticSearch()
                        {
                            Items = new Dictionary <long, long>()
                            {
                                { productId, quantity }
                            }
                        };

                        return(descriptor.Doc(partialIndexToElasticSearch).Type("IndexToElasticSearch"));
                    });

                    var updateResponseResult = updateResponse.ApiCall.Success;
                    if (!updateResponseResult)
                    {
                        throw new ElasticsearchClientException("Document could not be updated");
                    }
                    //TODO Handle errors
                }

                var searchResponse = client.Search <IndexToElasticSearch>(descriptor => descriptor.From(0).Size(100)
                                                                          .Type("IndexToElasticSearch").Query(containerDescriptor => containerDescriptor
                                                                                                              .Match(queryDescriptor => queryDescriptor.Field(search => search.Id).Query(user.Id.ToString()))));

                if (searchResponse.IsValid)
                {
                    //  System.Web.HttpContext.Current.User.Identity.
                    user.CartItems = searchResponse.Total;
                }

                return(new JsonResult()
                {
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    Data = new { Success = true }
                });
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
                throw;
            }
        }
        public ActionResult Index()
        {
            try
            {
                var user = _loggedInUser.GetLoggedInUser(System.Web.HttpContext.Current);

                if (user == null)
                {
                    throw new HttpException("Unable to find User");
                }

                var personAddress = new PersonQuery().GetPersonWithAddress(user.Id);


                var settings = new ConnectionSettings(new Uri("http://127.0.0.1:9200"))
                               .DefaultIndex("usercart").DisableDirectStreaming();

                var client = new ElasticClient(settings);

                var searchResponse = client.Search <IndexToElasticSearch>(descriptor => descriptor.From(0).Size(100)
                                                                          .Type("IndexToElasticSearch")
                                                                          .Query(containerDescriptor => containerDescriptor
                                                                                 .Match(queryDescriptor => queryDescriptor.Field(search => search.Id).Query(user.Id.ToString()))));

                var requestBodyInBytes = searchResponse.ApiCall.RequestBodyInBytes;
                var requestBody        = Encoding.UTF8.GetString(requestBodyInBytes);

                //Write or log this request to some where

                var isSuccess = searchResponse.IsValid;
                if (isSuccess)
                {
                    var checkoutModel = new FullCheckOutModel()
                    {
                        UserId = user.Id
                    };

                    var totalListOfProducts = 0M;
                    var hits = searchResponse.Hits.ToList();

                    if (hits.Count > 1)
                    {
                        throw new ElasticsearchClientException("Not sure what es brought");
                    }

                    var listOfProducts = new List <long>();
                    foreach (var hit in hits)
                    {
                        var productsListForUser = hit.Source.Items.Keys.ToList();
                        listOfProducts.AddRange(productsListForUser);

                        var products = new ProductsQuery().GetFromDatabase(listOfProducts);

                        foreach (var product in products)
                        {
                            var cost = 0M;
                            if (hit.Source.Items.TryGetValue(product.Id, out var quant))
                            {
                                cost = quant * product.Price;
                                totalListOfProducts = totalListOfProducts + cost;
                            }

                            checkoutModel.ProductsData.Add(new ProductsData()
                            {
                                Price       = cost,
                                ProductData = new ProductData()
                                {
                                    Id = product.Id, ProductName = product.ProductName, Price = product.Price
                                },
                                Quantity = quant
                            });
                        }
                    }

                    checkoutModel.AddressLine1 = personAddress.Address.AddressLine1;
                    checkoutModel.AddressLine2 = personAddress.Address.AddressLine2;
                    checkoutModel.City         = personAddress.Address.City;
                    checkoutModel.State        = personAddress.Address.State;
                    if (personAddress.Address.ZipCode != null)
                    {
                        checkoutModel.ZipCode = personAddress.Address.ZipCode.Value;
                    }
                    checkoutModel.Name = user.FirstName;
                    checkoutModel.TotalPriceOfAllProducts = totalListOfProducts;

                    return(View("Checkout", checkoutModel));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

            return(View("Checkout"));
        }