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> AddBlogPost([FromBody] InsertBlogView ibv) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } BlogPost blogPost = new BlogPost(); blogPost.Title = ibv.Title; blogPost.Description = ibv.Description; blogPost.Body = ibv.Body; foreach (string tag in ibv.TagList) { // If for some reason an non-existant an unknown tag is passed try { PostTags pt = new PostTags { BlogPostID = blogPost.BlogPostID, TagID = db.Tags.SingleOrDefault(t => t.TagName == tag).TagID }; if (pt != null) { blogPost.PostTags.Add(pt); } } catch { continue; } } blogPost.CreatedAt = DateTime.Now; blogPost.UpdatedAt = blogPost.CreatedAt; blogPost.Favorited = false; blogPost.FavoritesCount = 0; blogPost.Slug = SlugGenerator(blogPost.Title); db.BlogPosts.Add(blogPost); db.SaveChanges(); return(bpConverter.ToBlogPostView(blogPost)); }