Esempio n. 1
0
        public async Task <IActionResult> EditPost(EditPostViewModel postvm)
        {
            var post = new Post
            {
                Id              = postvm.Id,
                Title           = postvm.Title,
                Introduction    = postvm.Introduction,
                Body            = postvm.Body,
                Description     = postvm.Description,
                Tags            = postvm.Tags,
                Featured        = postvm.Featured,
                CommentsAllowed = postvm.CommentsAllowed,
                Slug            = SlugGenerator.ToSlug(postvm.Title)
            };

            if (postvm.Image == null)
            {
                post.Image = postvm.CurrentImage;
            }
            else
            {
                if (!string.IsNullOrEmpty(postvm.CurrentImage))
                {
                    _fileManager.RemovePostImage(postvm.CurrentImage);
                }
                post.Image = _fileManager.SavePostImage(postvm.Image);
            }
            if (post.Id > 0)
            {
                if (postvm.CategoryId != postvm.CurrentCategoryId)
                {
                    var category = _repo.GetCategoryNoTracking(postvm.CategoryId);
                    post.Category = category;
                }
                post.CreatedDate = postvm.CreatedDate;
                _repo.UpdatePost(post);
            }
            else
            {
                var category = _repo.GetCategory(postvm.CategoryId);
                post.Category = category;
                _repo.AddPost(post);
            }
            if (await _repo.SaveChangesAsync())
            {
                return(RedirectToAction("ManageBlog"));
            }
            else
            {
                return(View(post));
            }
        }