public async static Task <Dictionary <int, int> > GetDatabaseCartItems(MystoreRepository rep, Person person)
        {
            Dictionary <int, int> pairs      = new Dictionary <int, int>();
            MystoreRepository     repository = rep;

            var cartItems = await repository.GetCartItems(person.Id);

            if (cartItems == null)
            {
                return(new Dictionary <int, int>());
            }

            foreach (CartItem item in cartItems)
            {
                pairs.Add(item.ProductId, item.Number);
            }

            return(pairs);
        }
 public BaseController(mystoreContext context)
 {
     db         = context;
     repository = new MystoreRepository(db);
 }
        /// <summary>
        /// Synchronize cart items from Cookies and DataBase
        /// </summary>
        /// <param name="rep"></param>
        /// <param name="controller"></param>
        /// <param name="person"></param>
        public static async Task <bool> SynchronizeShoppingCart(MystoreRepository rep, Controller controller, Person person)
        {
            MystoreRepository repository = rep;

            if (person == null)
            {
                return(false);
            }

            Dictionary <int, int> databaseProducts = await GetDatabaseCartItems(rep, person);

            Dictionary <int, int> cookiesProducts = GetCookiesCartItems(controller);

            // updating DB cart items
            if (CookiesService.IsShoppingCartCookiesExist(controller))
            {
                foreach (var pair in cookiesProducts)
                {
                    int      productId     = pair.Key;
                    int      productNumber = pair.Value;
                    CartItem cartItem;

                    cartItem = await repository.GetCartItem(person.Id, productId);

                    if (cartItem != null)
                    {
                        cartItem.Number += productNumber;
                        await repository.UpdateCartItem(cartItem);
                    }
                    else
                    {
                        cartItem = await repository.CreateCartItem(person.Id, productId, productNumber);
                    }

                    // save changes ???
                }
            }

            // unite full list
            foreach (var pair in databaseProducts)
            {
                if (cookiesProducts.ContainsKey(pair.Key))
                {
                    cookiesProducts[pair.Key] += pair.Value;
                }
                else
                {
                    cookiesProducts.Add(pair.Key, pair.Value);
                }
            }

            string cookiesCartData = "";
            int    cookiesCartCost = 0;

            foreach (var pair in cookiesProducts)
            {
                Product product = await repository.GetProduct(pair.Key);

                cookiesCartData += "," + product.Id + "." + pair.Value;
                cookiesCartCost += product.Price * pair.Value;
            }

            if (!String.IsNullOrEmpty(cookiesCartData))
            {
                cookiesCartData = cookiesCartData.Substring(1);
                CookiesService.UpdateShoppingCartCookies(controller, cookiesCartCost.ToString(), cookiesCartData);
            }
            // await ???
            repository.SaveChanges();
            return(true);
        }