Ejemplo n.º 1
0
        public ActionResult <CommentView> AddComment(string slug, [FromBody] string body)
        {
            if (!string.IsNullOrWhiteSpace(body))
            {
                BlogPost blogPost = db.BlogPosts.Include(bp => bp.Comments).FirstOrDefault(bp => bp.Slug == slug);
                if (blogPost != null)
                {
                    Comment comment = new Comment
                    {
                        BlogPostID = blogPost.BlogPostID,
                        Body       = body,
                        CreatedAt  = DateTime.Now,
                        UpdatedAt  = DateTime.Now
                    };

                    blogPost.Comments.Add(comment);
                    db.Entry(blogPost).State = EntityState.Modified;
                    db.SaveChanges();

                    return(commentConverter.ToCommentView(comment));
                }

                return(NotFound());
            }

            else
            {
                return(BadRequest());
            }
        }
        public ActionResult <BlogPostView> UpdateBlogPost(string slug, [FromBody] InsertBlogView insertBlogView)
        {
            if (string.IsNullOrWhiteSpace(slug))
            {
                BadRequest();
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            BlogPost blogPost = db.BlogPosts.Include(bp => bp.PostTags).FirstOrDefault(bp => bp.Slug == slug);

            if (blogPost != null)
            {
                if (!string.IsNullOrWhiteSpace(insertBlogView.Body))
                {
                    blogPost.Body = insertBlogView.Body;
                }

                if (!string.IsNullOrWhiteSpace(insertBlogView.Description))
                {
                    blogPost.Description = insertBlogView.Description;
                }

                if (!string.IsNullOrWhiteSpace(insertBlogView.Title) && !blogPost.Title.Equals(insertBlogView.Title))
                {
                    blogPost.Title = insertBlogView.Title;
                    blogPost.Slug  = SlugGenerator(insertBlogView.Title);
                }

                blogPost.UpdatedAt = DateTime.Now;

                db.Entry(blogPost).State = EntityState.Modified;
                db.SaveChanges();

                return(bpConverter.ToBlogPostView(blogPost));
            }

            return(null);
        }