private ActionResult FileUpload(Post post, AddUpdateBlogViewModel formData)
        {
            //Handling file upload and check if file extension is from list of constants - allowed
            string fileExtension;

            if (formData.Media != null)
            {
                fileExtension = Path.GetExtension(formData.Media.FileName).ToLower();

                if (!ConstantsBlog.AllowedFileExtensions.Contains(fileExtension))
                {
                    ModelState.AddModelError("", "File extension is not allowed.");
                    return(View());
                }

                //Create directory if it doesn't exists
                if (!Directory.Exists(ConstantsBlog.MappedUploadFolder))
                {
                    Directory.CreateDirectory(ConstantsBlog.MappedUploadFolder);
                }

                //Get file name with special method and calculate full path with upload folder which is in constants
                var fileName = Guid.NewGuid().ToString() + fileExtension;
                //var fileName = formData.Media.FileName;
                var fullPathWithName = ConstantsBlog.MappedUploadFolder + fileName;
                //Actual save on hard disk
                formData.Media.SaveAs(fullPathWithName);
                //Set property with relative path for image
                post.MediaUrl = ConstantsBlog.UploadFolder + fileName;
            }
            DbContext.SaveChanges();

            return(RedirectToAction(nameof(BlogController.Index)));
        }
        private ActionResult AddPostToDatabase(int?id, AddUpdateBlogViewModel formData)
        {
            //any error with any property
            if (!ModelState.IsValid)
            {
                return(View());
            }

            Post post;
            var  userId = User.Identity.GetUserId();

            if (!id.HasValue)
            {
                post        = new Post();
                post.UserId = userId;
                DbContext.Posts.Add(post);
                post.Title = formData.Title;
                post.Slug  = post.SlugCalculation(post);
            }
            else
            {
                post = DbContext.Posts.FirstOrDefault(p => p.Id == id);

                if (post == null)
                {
                    return(RedirectToAction(nameof(BlogController.Index)));
                }

                post.UserId      = userId;
                post.Title       = formData.Title;
                post.DateUpdated = DateTime.Now;
            }

            post.SubTitle  = formData.SubTitle;
            post.Body      = formData.Body;
            post.Published = formData.Published;

            return(FileUpload(post, formData));
        }
        public ActionResult Update(int?id)
        {
            ViewBag.Message = "Update Post";

            if (id.HasValue)
            {
                var post = DbContext.Posts.FirstOrDefault(p => p.Id == id.Value);

                if (post != null)
                {
                    var model = new AddUpdateBlogViewModel();

                    model.Title     = post.Title;
                    model.SubTitle  = post.SubTitle;
                    model.Body      = post.Body;
                    model.Published = post.Published;
                    model.MediaUrl  = post.MediaUrl;

                    return(View(model));
                }
            }
            return(RedirectToAction(nameof(BlogController.Index)));
        }
 public ActionResult Create(AddUpdateBlogViewModel formData)
 {
     return(AddPostToDatabase(null, formData));
 }
 public ActionResult Update(int?id, AddUpdateBlogViewModel formData)
 {
     return(AddPostToDatabase(id, formData));
 }