Exemplo n.º 1
0
        public async Task <IActionResult> EditPantryItem(EditPantryItemViewModel vm)
        {
            GroceryItem editedPantryItem = Context.GroceryItems.Single(c => c.ID == vm.PantryId);

            var isAuthorized = await AuthorizationService.AuthorizeAsync(
                User, editedPantryItem, FoodOperations.Update);

            var currentUserId = UserManager.GetUserId(User);

            if (!isAuthorized.Succeeded)
            {
                return(Forbid());
            }

            if (ModelState.IsValid)
            {
                editedPantryItem.Name        = vm.Name;
                editedPantryItem.GroceryNote = vm.GroceryNote;
                editedPantryItem.LocationID  = vm.GroceryItemLocationID;

                Context.SaveChanges();
                return(Redirect("/Pantry"));
            }

            EditPantryItemViewModel newEditViewModel = new EditPantryItemViewModel(editedPantryItem, Context.Locations.ToList());

            newEditViewModel.PantryList = Context.GroceryItems.Where(g => g.IsInPantry == true).Where(p => p.UserID == currentUserId).ToList();

            return(View(newEditViewModel));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> EditPantryItem(int pantryId)
        {
            var currentUserId = UserManager.GetUserId(User);

            GroceryItem pantryItem = Context.GroceryItems.Single(c => c.ID == pantryId);

            var isAuthorized = await AuthorizationService.AuthorizeAsync(
                User, pantryItem, FoodOperations.Update);

            if (!isAuthorized.Succeeded)
            {
                return(Forbid());
            }

            EditPantryItemViewModel vm = new EditPantryItemViewModel(pantryItem, Context.Locations.ToList());

            vm.PantryList = Context.GroceryItems.Where(g => g.IsInPantry == true).Where(p => p.UserID == currentUserId).ToList();

            return(View(vm));
        }