public IHttpActionResult AddComment(int id, CommentInputModel commentData) { if (commentData == null) { return BadRequest("Missing comment data."); } if (!ModelState.IsValid) { return BadRequest(ModelState); } var bug = db.Bugs.Find(id); if (bug == null) { return this.NotFound(); } var currentUserId = User.Identity.GetUserId(); var comment = new Comment() { Text = commentData.Text, AuthorId = currentUserId, DateCreated = DateTime.Now, BugId = bug.Id }; db.Comments.Add(comment); db.SaveChanges(); if (currentUserId != null) { var currentUserName = User.Identity.GetUserName(); return this.Ok(new { comment.Id, Author = currentUserName, Message = "User comment added for bug #" + id }); } return this.Ok(new { comment.Id, Message = "Added anonymous comment for bug #" + id }); }
public IHttpActionResult PostCommentForBug(int id, CommentInputModel commentData) { var currentUser = this.User.Identity.GetUserId(); var bugCheck = this.db.Bugs.Find(id); if (bugCheck == null) { return this.NotFound(); } if (commentData.Text == null || commentData.Text.Length <= 1) { return this.BadRequest(); } var comment = new Comment() { Text = commentData.Text, AuthorId = currentUser, BugId = id, Date = DateTime.Now, }; this.db.Comments.Add(comment); this.db.SaveChanges(); if (currentUser == null) { return this.Ok(new { Id = comment.Id, Message = "Added anonymous comment for bug #"+ id }); } return this.Ok(new { Id = comment.Id, Author = User.Identity.GetUserName(), Message = "User comment added for bug #"+ id }); }
public IHttpActionResult AddComment(int id, CommentInputModel model) { var bug = this.Data.Bugs.Find(id); if (bug == null) { return this.NotFound(); } if (model == null || !this.ModelState.IsValid) { return this.BadRequest("The comment text is required."); } var newComment = new Comment { Text = model.Text, DateCreated = DateTime.Now, BugId = bug.Id, UserId = this.User.Identity.GetUserId() }; this.Data.Comments.Add(newComment); this.Data.SaveChanges(); object infoToReturn = new { newComment.Id, Message = "Added anonymous comment for bug #" + bug.Id }; if (newComment.UserId != null) { infoToReturn = new { newComment.Id, Author = this.User.Identity.GetUserName(), Message = "User comment added for bug #" + bug.Id }; } return this.Ok(infoToReturn); }