Exemplo n.º 1
0
        public IActionResult AddFood(ViewModelFood model)
        {
            Food food = new Food();

            food.Name       = model.CurrentFood.Name;
            food.CategoryId = model.CurrentFood.CategoryId;
            food.Price      = model.CurrentFood.Price;

            _context.Food.Add(food);
            _context.SaveChanges();

            foreach (var item in model.AllIngredients)
            {
                if (item.Selected == true)
                {
                    FoodIngredient foodIng = new FoodIngredient();
                    foodIng.FoodId       = model.CurrentFood.FoodId;
                    foodIng.IngredientId = item.IngredientId;

                    _context.Add(foodIng);
                    _context.SaveChanges();
                }
            }

            return(RedirectToAction("ManageMenu"));
        }
Exemplo n.º 2
0
        public IActionResult ChangeMembership(ViewModelFood model)
        {
            if (model.CurrentUser.isPremium)
            {
                AspNetUserRoles role = new AspNetUserRoles();
                role.RoleId = ("F4F4367D-940C-4FD7-A848-137508F418C1");
                role.UserId = model.CurrentUser.Id;

                var cust = _context.Customer.SingleOrDefault(c => c.UserId == role.UserId);
                cust.Points = 0;

                _context.AspNetUserRoles.Add(role);
                _context.SaveChanges();
            }
            else
            {
                var role = _context.AspNetUserRoles.SingleOrDefault(r => r.UserId == model.CurrentUser.Id &&
                                                                    r.RoleId == "F4F4367D-940C-4FD7-A848-137508F418C1");

                var cust = _context.Customer.SingleOrDefault(c => c.UserId == role.UserId);
                cust.Points = 0;

                _context.Remove(role);
                _context.SaveChanges();
            }

            return(RedirectToAction("ManageUsers"));
        }
Exemplo n.º 3
0
        public IActionResult EditProfile(ViewModelFood model)
        {
            var id = User.FindFirstValue(ClaimTypes.NameIdentifier);

            Customer cust = new Customer();

            cust.Name       = model.RegisterUser.Name;
            cust.Address    = model.RegisterUser.Address;
            cust.PostalCode = model.RegisterUser.PostalCode;
            cust.Phone      = model.RegisterUser.Phone;
            cust.City       = model.RegisterUser.City;
            cust.Username   = model.RegisterUser.Username;
            cust.Email      = model.RegisterUser.Email;

            var old = _context.Customer.SingleOrDefault(c => c.UserId == id);

            cust.Points = old.Points;

            cust.CustomerId = old.CustomerId;
            cust.UserId     = old.UserId;

            _context.Entry(old).CurrentValues.SetValues(cust);
            _context.SaveChanges();

            model.CurrentCustomer = cust;

            return(View("ViewProfile", model));
        }
Exemplo n.º 4
0
        public IActionResult ChangeOrderState(ViewModelFood model)
        {
            var order = _context.Order.SingleOrDefault(o => o.OrderId == model.CurrentOrder.OrderId);

            _context.Entry(order).CurrentValues.SetValues(model.CurrentOrder);
            _context.SaveChanges();

            return(RedirectToAction("ManageOrders"));
        }
Exemplo n.º 5
0
        public ViewModelFood GetViewModel()
        {
            ViewModelFood model = new ViewModelFood();

            model.AllFoods           = _context.Food.ToList();
            model.AllFoodIngredients = _context.FoodIngredient.ToList();
            model.AllIngredients     = _context.Ingredient.OrderBy(i => i.IngredientName).ToList();
            model.AllFoodOrders      = _context.FoodOrder.ToList();
            model.AllOrders          = _context.Order.ToList();
            model.AllUsers           = _context.AspNetUsers.ToList();

            foreach (var item in _context.Category)
            {
                model.AllCategories.Add(new SelectListItem {
                    Text = item.Title, Value = item.CategoryId.ToString()
                });
            }

            foreach (var item in model.AllFoods)
            {
                foreach (var ing in model.AllFoodIngredients.Where(f => f.FoodId == item.FoodId).ToList())
                {
                    item.Ingredients.Add(model.AllIngredients.SingleOrDefault(i => i.IngredientId == ing.IngredientId));
                }
            }

            foreach (var item in model.AllOrders)
            {
                item.Customer = _context.Customer.SingleOrDefault(c => c.CustomerId == item.CustomerId);
            }

            foreach (var item in model.AllUsers)
            {
                var roles = _context.AspNetUserRoles.Where(r => r.UserId == item.Id);

                foreach (var role in roles)
                {
                    var userRole = _context.AspNetRoles.Where(ur => ur.Id == role.RoleId).ToList();

                    foreach (var ur in userRole)
                    {
                        if (ur.NormalizedName == "PREMIUM")
                        {
                            item.isPremium = true;
                        }
                        if (ur.NormalizedName == "ADMINISTRATOR")
                        {
                            item.isAdmin = true;
                        }
                    }
                }
            }

            return(model);
        }
Exemplo n.º 6
0
        public IActionResult EditIngredient(ViewModelFood edited)
        {
            var old = GetViewModel();

            old.CurrentIngredient = _context.Ingredient.SingleOrDefault(i => i.IngredientId == edited.CurrentIngredient.IngredientId);

            _context.Entry(old.CurrentIngredient).CurrentValues.SetValues(edited.CurrentIngredient);
            _context.SaveChanges();

            return(RedirectToAction("ManageIngredients"));
        }
Exemplo n.º 7
0
        public IActionResult ManageIngredients(ViewModelFood model)
        {
            Ingredient ing = new Ingredient();

            ing.IngredientName = model.CurrentIngredient.IngredientName;

            _context.Ingredient.Add(ing);
            _context.SaveChanges();

            return(RedirectToAction("ManageIngredients"));
        }
Exemplo n.º 8
0
        public IActionResult ChangeAdmin(ViewModelFood model)
        {
            if (model.CurrentUser.isAdmin)
            {
                AspNetUserRoles role = new AspNetUserRoles();
                role.RoleId = ("1F6CE05B-A599-4985-9AB0-87A5BBAE970A");
                role.UserId = model.CurrentUser.Id;

                _context.AspNetUserRoles.Add(role);
                _context.SaveChanges();
            }
            else
            {
                var role = _context.AspNetUserRoles.SingleOrDefault(r => r.UserId == model.CurrentUser.Id &&
                                                                    r.RoleId == "1F6CE05B-A599-4985-9AB0-87A5BBAE970A");
                _context.Remove(role);
                _context.SaveChanges();
            }

            return(RedirectToAction("ManageUsers"));
        }
Exemplo n.º 9
0
        public IActionResult EditMenu(ViewModelFood edited)
        {
            var old = GetViewModel();

            old.CurrentFood = _context.Food.SingleOrDefault(f => f.FoodId == edited.CurrentFood.FoodId);
            edited.CurrentFood.Ingredients = new List <Ingredient>();

            foreach (var item in edited.AllIngredients)
            {
                if (item.Selected == true)
                {
                    if (old.CurrentFood.Ingredients.FirstOrDefault(i => i.IngredientId == item.IngredientId) == null)
                    {
                        FoodIngredient foodIng = new FoodIngredient();
                        foodIng.FoodId       = edited.CurrentFood.FoodId;
                        foodIng.IngredientId = item.IngredientId;

                        _context.Add(foodIng);
                        _context.SaveChanges();
                    }
                }
                else
                {
                    if (old.CurrentFood.Ingredients.FirstOrDefault(i => i.IngredientId == item.IngredientId) != null)
                    {
                        var foodIng = _context.FoodIngredient.FirstOrDefault(i => i.IngredientId == item.IngredientId &&
                                                                             i.FoodId == edited.CurrentFood.FoodId);

                        _context.Remove(foodIng);
                        _context.SaveChanges();
                    }
                }
            }


            _context.Entry(old.CurrentFood).CurrentValues.SetValues(edited.CurrentFood);
            _context.SaveChanges();

            return(RedirectToAction("ManageMenu"));
        }
Exemplo n.º 10
0
        // GET: /<controller>/
        public IActionResult Index()
        {
            ViewModelFood model = new ViewModelFood();

            model.AllFoods           = _context.Food.ToList();
            model.AllFoodIngredients = _context.FoodIngredient.ToList();
            model.AllIngredients     = _context.Ingredient.ToList();

            var cartModel = GetViewModel();

            model.CartList = cartModel.CartList;

            foreach (var item in model.AllFoods)
            {
                foreach (var ing in model.AllFoodIngredients.Where(f => f.FoodId == item.FoodId).ToList())
                {
                    item.Ingredients.Add(model.AllIngredients.SingleOrDefault(i => i.IngredientId == ing.IngredientId));
                }
            }

            return(View(model));
        }
Exemplo n.º 11
0
        public ViewModelFood GetViewModel()
        {
            ViewModelFood model = new ViewModelFood();

            var id = User.FindFirstValue(ClaimTypes.NameIdentifier);

            model.CurrentCustomer = _context.Customer.SingleOrDefault(c => c.UserId == id);

            if (HttpContext.Session.GetString("cart") == null)
            {
                model.CartList = new List <Food>();
            }
            else
            {
                var temp = HttpContext.Session.GetString("cart");
                model.CartList = JsonConvert.DeserializeObject <List <Food> >(temp);
            }

            HttpContext.Session.SetString("cart", JsonConvert.SerializeObject(model.CartList));

            return(model);
        }
Exemplo n.º 12
0
        public IActionResult AddToCart(int id)
        {
            ViewModelFood model = new ViewModelFood();

            Food newFood = new Food();

            newFood = _context.Food.SingleOrDefault(f => f.FoodId == id);

            if (HttpContext.Session.GetString("cart") == null)
            {
                model.CartList = new List <Food>();
            }
            else
            {
                var temp = HttpContext.Session.GetString("cart");
                model.CartList = JsonConvert.DeserializeObject <List <Food> >(temp);
            }

            model.CartList.Add(newFood);
            HttpContext.Session.SetString("cart", JsonConvert.SerializeObject(model.CartList));

            return(PartialView("_ShowCart", model));
        }