Ejemplo n.º 1
0
        public void DeleteDetailImageBlog(int?id)
        {
            BlogDetailImage detailImage = _db.BlogDetailImages.Where(cd => cd.Id == id).FirstOrDefault();

            //RemovePhoto(_env.WebRootPath, detailImage.ImageUrl);
            _db.BlogDetailImages.Remove(detailImage);
            _db.SaveChanges();
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Edit(Blog blog)
        {
            if (User.Identity.IsAuthenticated && User.IsInRole("moderator"))
            {
                if (blog == null)
                {
                    return(PartialView("ErrorPage"));
                }
                if (!ModelState.IsValid)
                {
                    return(NotFound());
                }
                Blog blogFromDb = await _context.Blogs.Where(b => b.Id == blog.Id).Include(b => b.BlogDetailImages).FirstOrDefaultAsync();

                if (blog.MainPhoto != null)
                {
                    if (!blog.MainPhoto.ContentType.Contains("image/"))
                    {
                        ModelState.AddModelError("MainPhoto", "File type should be image");
                        return(View(blogFromDb));
                    }
                    RemovePhoto(_env.WebRootPath, blogFromDb.MainImageUrl);
                    blogFromDb.MainImageUrl = await blog.MainPhoto.SavePhotoAsync(_env.WebRootPath, "blogs");
                }
                if (blog.DetailPhotos != null)
                {
                    foreach (var photo in blog.DetailPhotos)
                    {
                        if (!photo.ContentType.Contains("image/"))
                        {
                            ModelState.AddModelError("DetailPhotos", "File type should be image");
                            return(View(blogFromDb));
                        }
                    }
                    foreach (var photo in blog.DetailPhotos)
                    {
                        BlogDetailImage detailImage = new BlogDetailImage
                        {
                            BlogId   = blogFromDb.Id,
                            ImageUrl = await photo.SavePhotoAsync(_env.WebRootPath, "blogs")
                        };
                        await _context.BlogDetailImages.AddAsync(detailImage);
                    }
                }
                blogFromDb.Header      = blog.Header;
                blogFromDb.Content     = blog.Content;
                blogFromDb.UpdatedDate = DateTime.Now;
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewBag.IsHeaderNonVisible = true;
            return(Redirect("/account/login"));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Create(Blog blog)
        {
            if (User.Identity.IsAuthenticated && User.IsInRole("moderator"))
            {
                if (blog == null)
                {
                    return(PartialView("ErrorPage"));
                }
                if (!ModelState.IsValid)
                {
                    return(NotFound());
                }
                if (blog.Content.Length < 300)
                {
                    ModelState.AddModelError("Content", "Content is too short");
                    return(View(blog));
                }
                Blog newBlog = new Blog();
                await _context.AddAsync(newBlog);

                if (blog.MainPhoto == null)
                {
                    ModelState.AddModelError("MainPhoto", "Main image should be selected");
                    return(View(blog));
                }
                if (!blog.MainPhoto.ContentType.Contains("image/"))
                {
                    ModelState.AddModelError("MainPhoto", "File type should be image");
                    return(View(blog));
                }
                newBlog.MainImageUrl = await blog.MainPhoto.SavePhotoAsync(_env.WebRootPath, "blogs");

                newBlog.Header      = blog.Header;
                newBlog.Content     = blog.Content;
                newBlog.PublishDate = DateTime.Now;
                newBlog.UpdatedDate = DateTime.Now;
                if (blog.DetailPhotos != null)
                {
                    foreach (var photo in blog.DetailPhotos)
                    {
                        if (!photo.ContentType.Contains("image/"))
                        {
                            ModelState.AddModelError("DetailPhotos", "File type should be image");
                            return(View(blog));
                        }
                    }
                    foreach (var photo in blog.DetailPhotos)
                    {
                        BlogDetailImage detailImage = new BlogDetailImage
                        {
                            BlogId   = newBlog.Id,
                            ImageUrl = await photo.SavePhotoAsync(_env.WebRootPath, "blogs")
                        };
                        await _context.BlogDetailImages.AddAsync(detailImage);
                    }
                }
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewBag.IsHeaderNonVisible = true;
            return(Redirect("/account/login"));
        }