public void AddToCart(Movie movie)
        {
            // Get the matching cart and album instances
            var cartItem = this.storeDB.MovieCarts.SingleOrDefault(
                c => c.MovieCartId == ShoppingCartId &&
                c.MovieId == movie.MovieId);

            if (cartItem == null)
            {
                // Create a new cart item if no cart item exists
                cartItem = new MovieCart()
                {
                    MovieId     = movie.MovieId,
                    MovieCartId = ShoppingCartId,
                    Count       = 1,
                    DateCreated = DateTime.Now
                };
                this.storeDB.MovieCarts.Add(cartItem);
            }
            else
            {
                // If the item does exist in the cart, then add one to the quantity
                cartItem.Count++;
            }
            this.storeDB.SaveChanges();
        }
示例#2
0
        private MovieCart SendRequestForCart(string id)
        {
            var url      = "http://192.168.8.101:9200/movie/_search";
            var jsonData = "{ " +
                           "\"query\":{" +
                           "\"match\": {" +
                           "\"MovieID\": " + id +
                           "}" +
                           "}" +
                           "}";

            using (var client = new WebClient())
            {
                client.Headers.Add("content-type", "application/json");
                client.Credentials = new NetworkCredential("", "");
                var jsonResponse = client.UploadString(url, jsonData);

                var parsedObject = JObject.Parse(jsonResponse);
                var tmp          = parsedObject["hits"]["hits"];

                string jsonMovie = tmp[0]["_source"].ToString();

                MovieCart movie = JsonConvert.DeserializeObject <MovieCart>(jsonMovie);

                return(movie);
            }
        }
        // When a user has logged in, migrate their shopping cart to
        // be associated with their username
        public void MigrateCart(string userName)
        {
            var shoppingCart = this.storeDB.MovieCarts.Where(c => c.MovieCartId == ShoppingCartId);

            foreach (MovieCart item in shoppingCart)
            {
                // ha már van ilyen elem, akkor a count-ot növelje majd törölje
                MovieCart cart = this.storeDB.MovieCarts.FirstOrDefault(c => c.MovieId == item.MovieId && c.MovieCartId == userName);
                if (cart != null)
                {
                    cart.Count++;
                    this.storeDB.MovieCarts.Remove(item);
                }
                else
                {
                    item.MovieCartId = userName;
                }
            }
            this.storeDB.SaveChanges();
        }