public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            Models.Recipe recipeToDelete = await _context.Recipes
                                           .Include(r => r.RecipeCategories)
                                           .SingleAsync(r => r.Id == id);

            if (recipeToDelete.PhotoPath != null)
            {
                var imgPath = Path.Combine(_hostEnvironment.WebRootPath, recipeToDelete.PhotoPath.TrimStart('\\'));
                if (System.IO.File.Exists(imgPath))
                {
                    System.IO.File.Delete(imgPath);
                }
            }
            if (recipeToDelete != null)
            {
                _context.Recipes.Remove(recipeToDelete);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
Esempio n. 2
0
        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.Attach(Category).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CategoryExists(Category.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }
Esempio n. 3
0
        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.Categories.Add(Category);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync(string[] selectedCategories)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            if (selectedCategories != null)
            {
                Recipe.RecipeCategories = new List <RecipeCategory>();
                foreach (var cat in selectedCategories)
                {
                    var catToAdd = new RecipeCategory {
                        CategoryId = int.Parse(cat)
                    };
                    Recipe.RecipeCategories.Add(catToAdd);
                }
            }

            string webRootPath = _hostEnvironment.WebRootPath;
            var    files       = HttpContext.Request.Form.Files;

            if (files.Count > 0)
            {
                string fileName  = Guid.NewGuid().ToString();
                var    uploads   = Path.Combine(webRootPath, @"images\recipe_photos\");
                var    extension = Path.GetExtension(files[0].FileName);
                var    fullpath  = uploads + fileName + extension;
                using (var fileStream = System.IO.File.Create(fullpath)) {
                    files[0].CopyTo(fileStream);
                }
                Recipe.PhotoPath = @"\images\recipe_photos\" + fileName + extension;
            }
            else
            {
                string fileName  = Guid.NewGuid().ToString();
                var    uploads   = Path.Combine(webRootPath, @"images\recipe_photos\");
                var    extension = Path.GetExtension(@"images\defaults\DefaultImage.jpg");
                var    fullpath  = uploads + fileName + extension;
                var    defaults  = Directory.GetFiles(Path.Combine(webRootPath, @"images\defaults\"));

                System.IO.File.Copy(defaults[0], fullpath);

                Recipe.PhotoPath = @"\images\recipe_photos\" + fileName + extension;
            }
            _context.Recipes.Add(Recipe);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Review = await _context.Reviews.FindAsync(id);

            if (Review != null)
            {
                _context.Reviews.Remove(Review);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Category = await _context.Categories.FindAsync(id);

            if (Category != null)
            {
                _context.Categories.Remove(Category);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
Esempio n. 7
0
        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync(int?id, string[] selectedCategories)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            var recipeToUpdate = await _context.Recipes
                                 .Include(r => r.RecipeCategories)
                                 .ThenInclude(r => r.Category).FirstOrDefaultAsync(c => c.Id == id);

            string webRootPath = _hostEnvironment.WebRootPath;
            var    files       = HttpContext.Request.Form.Files;

            if (files.Count > 0)
            {
                if (OldPhotoPath != null)
                {
                    var imgPath = Path.Combine(_hostEnvironment.WebRootPath, OldPhotoPath.TrimStart('\\'));
                    if (System.IO.File.Exists(imgPath))
                    {
                        System.IO.File.Delete(imgPath);
                    }
                }
                string fileName  = Guid.NewGuid().ToString();
                var    uploads   = Path.Combine(webRootPath, @"images\recipe_photos\");
                var    extension = Path.GetExtension(files[0].FileName);
                var    fullpath  = uploads + fileName + extension;
                using (var fileStream = System.IO.File.Create(fullpath)) {
                    files[0].CopyTo(fileStream);
                }
                recipeToUpdate.PhotoPath = @"\images\recipe_photos\" + fileName + extension;
            }
            else if (OldPhotoPath != null)
            {
                recipeToUpdate.PhotoPath = OldPhotoPath;
            }
            //_context.Attach(Recipe).State = EntityState.Modified;
            if (await TryUpdateModelAsync <Models.Recipe>(
                    recipeToUpdate,
                    "Recipe",
                    r => r.Title, r => r.PrepTime,
                    r => r.CookTime, r => r.FeedsQty,
                    r => r.Instructions))
            {
                UpdateRecipeCategories(_context, selectedCategories, recipeToUpdate);
                await _context.SaveChangesAsync();

                return(RedirectToPage("./Index"));
            }
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!RecipeExists(Recipe.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }