Ejemplo n.º 1
0
        public ActionResult Create(SubCategoryAndCategoryViewModel model)
        {
            if (ModelState.IsValid)
            {
                var doesSubCategoryExists = db.SubCategory.Include(s => s.Category).Where(s => s.Name == model.SubCategory.Name && s.Category.Id == model.SubCategory.CategoryId);

                if (doesSubCategoryExists.Count() > 0)
                {
                    //Error
                    model.StatusMessage = "Error : Sub Category exists under " + doesSubCategoryExists.First().Category.Name + " category. Please use another name.";
                }
                else
                {
                    db.SubCategory.Add(model.SubCategory);
                    db.SaveChangesAsync();
                    return(RedirectToAction(nameof(Index)));
                }
            }
            SubCategoryAndCategoryViewModel modelVM = new SubCategoryAndCategoryViewModel()
            {
                CategoryList    = db.Category.ToList(),
                SubCategory     = model.SubCategory,
                SubCategoryList = db.SubCategory.OrderBy(p => p.Name).Select(p => p.Name).ToList(),
                StatusMessage   = model.StatusMessage
            };

            return(View(modelVM));
        }
Ejemplo n.º 2
0
        public ActionResult Minus(int id)
        {
            var cart = context.ShoppingCart.ToList().Where(e => e.CartId == id).FirstOrDefault();

            if (cart.Count == 1)
            {
                context.ShoppingCart.Remove(cart);
                context.SaveChangesAsync();

                var count = context.ShoppingCart.Where(u => u.AppUserId == cart.AppUserId).ToList().Count;
            }
            else
            {
                cart.Count -= 1;

                context.SaveChanges();
            }

            return(RedirectToAction("GetShoppingCart"));
        }
Ejemplo n.º 3
0
        public ActionResult Details(ShoppingCart CartObject)
        {
            CartObject.Id = 0;
            if (ModelState.IsValid)
            {
                var claimsIdentity = (ClaimsIdentity)this.User.Identity;
                var claim          = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier);
                CartObject.ApplicationUserId = claim.Value;

                ShoppingCart cartFromDb = _db.ShoppingCart.Where(c => c.ApplicationUserId == CartObject.ApplicationUserId &&
                                                                 c.MenuItemId == CartObject.MenuItemId).FirstOrDefault();

                if (cartFromDb == null)
                {
                    _db.ShoppingCart.Add(CartObject);
                }
                else
                {
                    cartFromDb.Count = cartFromDb.Count + CartObject.Count;
                }
                _db.SaveChangesAsync();

                var count = _db.ShoppingCart.Where(c => c.ApplicationUserId == CartObject.ApplicationUserId).ToList().Count();
                //HttpContext.Session.SetInt32(SD.ssShoppingCartCount, count);

                return(RedirectToAction("Index"));
            }
            else
            {
                var menuItemFromDb = _db.MenuItem.Include(m => m.Category).Include(m => m.SubCategory).Where(m => m.Id == CartObject.MenuItemId).FirstOrDefault();

                ShoppingCart cartObj = new ShoppingCart()
                {
                    MenuItem   = menuItemFromDb,
                    MenuItemId = menuItemFromDb.Id
                };

                return(View(cartObj));
            }
        }
Ejemplo n.º 4
0
        public ActionResult Create(SubCategoryAndCategoryViewModel model)
        {
            if (ModelState.IsValid)
            {
                //read all sub categories from db
                //include the category data with include
                //but only where the name is correct for the current selected model
                //and its in the same cat id
                var doesSubCategoryExists = db.SubCategory.Include(s => s.Category)
                                            .Where(s => s.Name == model.SubCategory.Name &&
                                                   s.Category.Id == model.SubCategory.CategoryId);

                if (doesSubCategoryExists.Count() > 0)
                {
                    //Bad, we dont want duplicate sub categories

                    // StatusMessage = "Error : Sub Category already exist under " + doesSubCategoryExists.First().Category.Name + " please choose another name";
                }
                else
                {
                    db.SubCategory.Add(model.SubCategory);
                    db.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }

            //the model passeed in is not in a valid state so we have to make
            //a new one to reset to and tell the user the issue
            SubCategoryAndCategoryViewModel modelVM = new SubCategoryAndCategoryViewModel()
            {
                CategoryList    = db.Category.ToList(),
                SubCategory     = model.SubCategory,
                SubCategoryList = db.SubCategory.OrderBy(p => p.Name).Select(p => p.Name).Distinct().ToList(),
                //StatusMessage = StatusMessage
            };

            return(View(modelVM));
        }