public async Task<ActionResult> NewComment(NewCommentModel model) { if (!ModelState.IsValid) { return RedirectToAction("Post", new {id = model.PostId}); } var blogContext = new BlogContext(); // XXX WORK HERE // add a comment to the post identified by model.PostId. // you can get the author from "this.User.Identity.Name" var comment = new Comment { Author = User.Identity.Name, Content = model.Content, CreatedAtUtc = DateTime.Now }; await blogContext.Posts.FindOneAndUpdateAsync(x => x.Id == ObjectId.Parse(model.PostId), Builders<Post>.Update.AddToSet(x => x.Comments, comment)); return RedirectToAction("Post", new {id = model.PostId}); }
public async Task<ActionResult> NewComment(NewCommentModel model) { if (!ModelState.IsValid) { return RedirectToAction("Post", new { id = model.PostId }); } var comment = new Comment { Author = User.Identity.Name, Content = model.Content, CreatedAtUtc = DateTime.UtcNow }; var blogContext = new BlogContext(); await blogContext.Posts.UpdateOneAsync( x => x.Id == model.PostId, Builders<Post>.Update.Push(x => x.Comments, comment)); return RedirectToAction("Post", new { id = model.PostId }); }
public async Task<ActionResult> NewComment(NewCommentModel model) { if (!ModelState.IsValid) { return RedirectToAction("Post", new { id = model.PostId }); } var blogContext = new BlogContext(); // XXX WORK HERE // add a comment to the post identified by model.PostId. // you can get the author from "this.User.Identity.Name" IMongoCollection<Post> postsCollection = getPostsCollection(); var builder = Builders<Post>.Filter; FilterDefinition<Post> searchFilter = builder.Eq("_id", ObjectId.Parse(model.PostId)); Post post = await postsCollection.Find(searchFilter).FirstOrDefaultAsync(); Comment newComment = new Comment(); newComment.Author = this.User.Identity.Name; newComment.Content = model.Content; //newComment.CreatedAtUtc = DateTime.Now; post.Comments.Add(newComment); var filter = Builders<Post>.Filter.Eq("_id", ObjectId.Parse(model.PostId)); await postsCollection.ReplaceOneAsync(filter, post); return RedirectToAction("Post", new { id = model.PostId }); }
public async Task<ActionResult> NewComment(NewCommentModel model) { if (!ModelState.IsValid) { return RedirectToAction("Post", new { id = model.PostId }); } var blogContext = new BlogContext(); // XXX WORK HERE // add a comment to the post identified by model.PostId. // you can get the author from "this.User.Identity.Name" var filter = Builders<Post>.Filter.Eq("Id", model.PostId); var post = await blogContext.Posts.Find(filter).SingleAsync(); var comment = new Comment { Author = this.User.Identity.Name, Content = model.Content, CreatedAtUtc = DateTime.UtcNow }; var update = Builders<Post>.Update.Push("Comments", comment); await blogContext.Posts.UpdateOneAsync(filter, update); return RedirectToAction("Post", new { id = model.PostId }); }
public async Task<ActionResult> NewComment(NewCommentModel model) { if (!ModelState.IsValid) { return RedirectToAction("Post", new {id = model.PostId}); } // XXX WORK HERE // add a comment to the post identified by model.PostId. // you can get the author from "this.User.Identity.Name" var newComment = new Comment { Author = User.Identity.Name, Content = model.Content, CreatedAtUtc = DateTime.UtcNow }; await _blogContext.Posts.UpdateOneAsync(x => x.Id == model.PostId, Builders<Post>.Update.Push(x => x.Comments, newComment)); var find = await _blogContext.Posts.Find(f => f.Id == model.PostId).ToListAsync(); return RedirectToAction("Post", new {id = model.PostId}); }
public async Task<ActionResult> NewComment(NewCommentModel model) { if (!ModelState.IsValid) { return RedirectToAction("Post", new { id = model.PostId }); } var blogContext = new BlogContext(); // XXX WORK HERE // add a comment to the post identified by model.PostId. // you can get the author from "this.User.Identity.Name" var user = Request.GetOwinContext().Authentication.User; var comment = new Comment { Author = user.Identity.Name, CreatedAtUtc = DateTime.Now.ToUniversalTime(), Content = model.Content }; var update = Builders<Post>.Update.Push<Comment>(p => p.Comments, comment); await blogContext.Posts.FindOneAndUpdateAsync(p => p.Id.Equals(model.PostId), update); return RedirectToAction("Post", new { id = model.PostId }); }
public async Task<ActionResult> NewComment(NewCommentModel model) { if (!ModelState.IsValid) { return RedirectToAction("Post", new { id = model.PostId }); } var blogContext = new BlogContext(); // XXX WORK HERE // add a comment to the post identified by model.PostId. // you can get the author from "this.User.Identity.Name" /* Response */ Comment comment = new Comment { CreatedAtUtc = DateTime.UtcNow, Author = User.Identity.Name, Content = model.Content }; UpdateDefinition<Post> update = Builders<Post>.Update.AddToSet<Comment>("Comments", comment); await blogContext.Posts.FindOneAndUpdateAsync(p => p.Id == ObjectId.Parse(model.PostId), update); /* ------- */ return RedirectToAction("Post", new { id = model.PostId }); }