Exemple #1
0
        public ActionResult InsertUpdateBasket(List <ShoppingBasketModel> items)
        {
            BasketRepository br = new BasketRepository();

            var user = UserManager.FindByEmail(this.User.Identity.Name);

            if (user != null)
            {
                userId = user.Id;
            }
            var userBasket = br.LoadByUserId(userId).ToList();

            if (userBasket == null)
            {
                userBasket = new List <ShoppingBasket>();
            }

            userBasket = userBasket.Where(x => x.ArchiveDateUTC == null).ToList();

            foreach (var item in items.Where(x => x.Id > 0))
            {
                //updates
                ShoppingBasket m = userBasket.Where(y => y.Id == item.Id).FirstOrDefault();
                m.Name        = item.Name;
                m.Description = item.Description;
                m.Currency    = item.Currency;
                m.Price       = item.Price;
                m.Quantity    = item.Quantity;
                m.Url         = item.Url;
            }

            foreach (var item in items.Where(x => x.Id == 0))
            {
                //inserts
                var m = new ShoppingBasket();
                m.Id          = (item.Id > 0 ? item.Id : 0);
                m.Name        = item.Name;
                m.Description = item.Description;
                m.Currency    = item.Currency;
                m.Price       = item.Price;
                m.Quantity    = item.Quantity;
                m.Url         = item.Url;
                m.UserId      = userId;
                userBasket.Add(m);
            }

            br.InsertUpdateBasket(userBasket);

            return(Json(true));
        }