public IHttpActionResult Create(int id, CommentModel model) { var userId = this.User.Identity.GetUserId(); var comment = new Comment { ArticleId = id, AuthorId = userId, Content = model.Content, DateCreated = DateTime.Now }; this.data.Comments.Add(comment); this.data.SaveChanges(); return Ok(); }
public IHttpActionResult Create(int id, CommentDataModel model) { var newComment = new Comment { AuthorID = this.User.Identity.GetUserId(), Content = model.Content, DateCreated = DateTime.Now }; this.data.Articles.Find(id).Comments.Add(newComment); this.data.SaveChanges(); model.ID = newComment.ID; model.DateCreated = newComment.DateCreated; return Ok(model); }
public IHttpActionResult CreateCommentByArticleId(int id, CommentModel model) { if (!ModelState.IsValid) { return this.BadRequest(ModelState); } var article = this.data.Articles.Find(id); if (article==null) { return this.BadRequest("No such article exists!"); } var comment = new Comment() { AuthorId = User.Identity.GetUserId(), Content = model.Content }; article.Comments.Add(comment); model.Id = comment.Id; model.AuthorId = comment.AuthorId; this.data.SaveChanges(); return this.Ok(model); }
public IHttpActionResult Comment(int id, CommentViewModel model) { var article = this.data.Articles.Find(id); if (article == null) { return BadRequest("Article with such id does not exist!"); } var userID = this.User.Identity.GetUserId(); var comment = new Comment { Article = article, AuthorId = userID, Content = model.Content, DateCreated = DateTime.Now }; this.data.Comments.Add(comment); this.data.SaveChanges(); return Ok(); }