public IHttpActionResult AddCommentToPost( int postId, AddCommentBindingModel model) { var post = this.Data.Posts.Find(postId); if (post == null) { return(this.NotFound()); } if (model == null) { return(this.BadRequest("Empty model is not allowed")); } if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var userId = this.User.Identity.GetUserId(); var comment = new Comment() { Content = model.Content, PostedOn = DateTime.Now, AuthorId = userId }; post.Comments.Add(comment); this.Data.SaveChanges(); return(this.Ok()); }
public IHttpActionResult AddComment([FromUri] int id, [FromBody] AddCommentBindingModel model) { var post = this.Data.Posts.Find(id); if (post == null) { return(this.NotFound()); } var currentUserId = User.Identity.GetUserId(); var currentUser = this.Data.Users.FirstOrDefault(x => x.Id == currentUserId); if (currentUser == null) { return(this.BadRequest("Invalid user token! Please login again!")); } var newCommentToAdd = new Comment() { Author = currentUser, Post = post, PostedOn = DateTime.Now, Content = model.CommentContent }; post.Comments.Add(newCommentToAdd); this.Data.SaveChanges(); var newCommentViewModel = this.Data.Comments .Where(c => c.Id == newCommentToAdd.Id) .Select(CommentViewModel.Create) .FirstOrDefault(); return(this.Ok(newCommentViewModel)); }
public ActionResult CommentForm(int id) { AddCommentBindingModel acbm = new AddCommentBindingModel { Id = id }; return(this.PartialView("_WriteCommentPartial", acbm)); }
public ActionResult AddComment(AddCommentBindingModel model) { if (ModelState.IsValid) { this.service.AddComment(model); } return(RedirectToAction("Details", "Snippet", new { id = model.SnippetId })); }
public async Task <HttpResponseMessage> AddPostComment([FromUri] int postId, [FromBody] AddCommentBindingModel model) { if (model == null) { return(await this.BadRequest("Comment cannot be empty.").ExecuteAsync(new CancellationToken())); } if (!ModelState.IsValid) { return(await this.BadRequest(this.ModelState).ExecuteAsync(new CancellationToken())); } var post = this.Data.GroupPosts.Find(postId); if (post == null) { return(await this.NotFound().ExecuteAsync(new CancellationToken())); } var currentUserId = User.Identity.GetUserId(); var currentUser = this.Data.Users.FirstOrDefault(x => x.Id == currentUserId); if (currentUser == null) { return(await this.BadRequest("Invalid user token! Please login again!") .ExecuteAsync(new CancellationToken())); } if (!post.Owner.Members.Contains(currentUser) && currentUser != post.Owner.Owner) { return(await this.BadRequest("Not allowed. You must be a member of the group.") .ExecuteAsync(new CancellationToken())); } var newComment = new Comment() { Content = model.CommentContent, Author = currentUser, PostedOn = DateTime.Now }; post.Comments.Add(newComment); this.Data.SaveChanges(); var commentPreview = this.Data.Comments .Where(p => p.Id == newComment.Id) .Select(AddCommentViewModel.Create) .FirstOrDefault(); commentPreview.PostId = post.Id; return(await this.Ok(commentPreview).ExecuteAsync(new CancellationToken())); }
public async Task <IActionResult> AddComment([FromBody] AddCommentBindingModel addCommentBindingModel, int tvSeriesId) { var user = User.Identity.Name; var result = await _commentService.AddComment(addCommentBindingModel, tvSeriesId, user); if (result.ErrorOccurred) { return(BadRequest(result)); } return(Ok(result)); }
public async Task <HttpResponseMessage> EditPostComment([FromUri] int postId, [FromUri] int commentId, [FromBody] AddCommentBindingModel model) { if (model == null) { return(await this.BadRequest("Comment cannot be empty.").ExecuteAsync(new CancellationToken())); } if (!ModelState.IsValid) { return(await this.BadRequest(this.ModelState).ExecuteAsync(new CancellationToken())); } var post = this.Data.GroupPosts.Find(postId); if (post == null) { return(await this.NotFound().ExecuteAsync(new CancellationToken())); } var comment = this.Data.Comments.Find(commentId); if (comment == null) { return(await this.NotFound().ExecuteAsync(new CancellationToken())); } var currentUserId = User.Identity.GetUserId(); var currentUser = this.Data.Users.FirstOrDefault(x => x.Id == currentUserId); if (comment.Author != currentUser) { return(await this.BadRequest("Not allowed. User must be author of comment.") .ExecuteAsync(new CancellationToken())); } comment.Content = model.CommentContent; this.Data.SaveChanges(); var commentPreview = this.Data.Comments .Where(p => p.Id == comment.Id) .Select(AddCommentViewModel.Create) .FirstOrDefault(); commentPreview.PostId = post.Id; return(await this.Ok(commentPreview).ExecuteAsync(new CancellationToken())); }
public ActionResult Comment(AddCommentBindingModel acbm) { var currentUserId = this.User.Identity.GetUserId(); if (!ModelState.IsValid) { return(RedirectToAction("Details", "Reviews", new { id = acbm.Id })); } if (!this._service.IsCurrentUserAllowedToComment(currentUserId)) { return(RedirectToAction("Details", "Reviews", new { id = acbm.Id })); } this._service.WriteCommentToReview(currentUserId, acbm); return(RedirectToAction("Details", "Reviews", new { id = acbm.Id })); }
public void WriteCommentToReview(string currentUserId, AddCommentBindingModel acbm) { var customer = this.Context.Customers.First(c => c.UserId == currentUserId); var review = this.Context.Reviews.Find(acbm.Id); Comment comment = new Comment { Review = review, Content = acbm.Comment, Customer = customer, PublishDate = DateTime.Now }; this.Context.Comments.Add(comment); this.Context.SaveChanges(); }
public IHttpActionResult AddCommentForGivenBug(int id, AddCommentBindingModel addCommentModel) { if (addCommentModel == null) { return(this.BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var bug = db.Bugs.FirstOrDefault(b => b.Id == id); if (bug == null) { return(this.NotFound()); } var currentUserId = User.Identity.GetUserId(); var author = db.Users.FirstOrDefault(u => u.Id == currentUserId); Comment comment = new Comment() { Text = addCommentModel.Text, PublishDate = DateTime.Now, Author = author }; bug.Comments.Add(comment); db.SaveChanges(); if (author == null) { return(this.Ok(new { Id = comment.Id, Message = "Added anonymous comment for bug #" + bug.Id })); } return(this.Ok(new { Id = comment.Id, Author = author.UserName, Message = "User comment added for bug #" + bug.Id })); }
public async Task <ResponseDto <BaseModelDto> > AddComment(AddCommentBindingModel addCommentBindingModel, int tvSeriesId, string userId) { var response = new ResponseDto <BaseModelDto>(); var tvSeriesExists = await _tvSeriesRepository.ExistAsync(x => x.Id == tvSeriesId); if (!tvSeriesExists) { response.AddError(Model.TvShow, Error.tvShow_NotFound); return(response); } if (userId == null) { response.AddError(Model.Account, Error.account_Login); return(response); } Comment comment = new Comment(); comment.Content = addCommentBindingModel.Content; comment.TvSeriesId = tvSeriesId; comment.UserId = userId; comment.CreateDateTime = DateTime.Now; comment.UpdateDateTime = DateTime.Now; var result = await _commentRepository.AddAsync(comment); if (!result) { response.AddError(Model.Comment, Error.comment_Adding); return(response); } else { if (userId != null) { AddNotificationBindingModel addNotificationBindingModel = new AddNotificationBindingModel(); addNotificationBindingModel.Type = "commentedTvSeries"; await _notificationService.AddNotification(addNotificationBindingModel, tvSeriesId, userId); } } return(response); }
public IHttpActionResult ReplyComment(AddCommentBindingModel bindingModel, int postId, int commentId) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } if (bindingModel == null) { return(this.BadRequest("Invalid data.")); } var currentUserId = this.UserIdProvider.GetUserId(); var currentUser = this.Data.Users.Find(currentUserId); var post = this.Data.Posts.Find(postId); if (post == null) { return(this.NotFound()); } var comment = this.Data.Comments.Find(commentId); if (comment == null) { return(this.NotFound()); } if (this.HasAuthorization(currentUser, post)) { return(this.BadRequest("Unable to comment. You can comment only posts of your friends or posts on their wall.")); } CommentReply commentReply = new CommentReply { AuthorId = currentUserId, CommentId = comment.Id, Content = bindingModel.CommentContent, RepliedOn = DateTime.Now }; comment.Replies.Add(commentReply); this.Data.SaveChanges(); return(this.Ok()); }
public IHttpActionResult PostComment(int id, AddCommentBindingModel model) { var trip = this.Data.Trips.Find(id); if (model == null) { return(this.BadRequest("Model cannot be null (no data in request)")); } if (trip == null) { return(this.BadRequest("Trip does not exist")); } if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var comment = new Comment() { AuthorId = this.User.Identity.GetUserId(), Content = model.Content, PostedOn = DateTime.Now, TripId = id }; var notification = new Notification() { RecieverId = trip.DriverId, Type = NotificationType.Comment }; trip.Comments.Add(comment); this.Data.Notifications.Add(notification); this.Data.SaveChanges(); var data = this.Data.Comments .All().Where(t => t.Id == comment.Id) .Select(CommentViewModel.Create) .FirstOrDefault(); return(this.Ok(data)); }
public async Task <IHttpActionResult> PostEventComment(AddCommentBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var comment = new Comment { UserId = CurrentUser.UserId, EntityId = model.EntityId, EntityType = CommentEntityTypes.Event, Text = model.Text, DateCreate = DateTime.Now }; await commentsRepository.SaveInstance(comment); return(CreatedAtRoute("DefaultApi", new { id = comment.CommentId }, comment)); }
public void AddComment(AddCommentBindingModel model) { Snippet snippet = this.Context.Snippets.FirstOrDefault(s => s.Id == model.SnippetId); DateTime currentTime = DateTime.Now; UserLogged author = this.Context.UserLoggeds.FirstOrDefault(u => u.User.Id == model.UserId); string content = model.Content; Comment comment = new Comment() { Author = author, Content = content, CreationTime = currentTime, Snippet = snippet }; this.Context.Comments.Add(comment); this.Context.SaveChanges(); }
public IHttpActionResult UpdateComment(int id, AddCommentBindingModel model) { var comment = this.Data.Comments.Find(id); if (comment == null) { return(this.BadRequest(string.Format("There is no comment with Id {0}", id))); } var loggedUserId = this.User.Identity.GetUserId(); if (comment.AuthorId != loggedUserId) { return(this.Unauthorized()); } if (model == null) { return(this.BadRequest("Model cannot be null (no data in request)")); } if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } comment.Content = model.Content; var notification = new Notification() { RecieverId = comment.Trip.DriverId, Type = NotificationType.Comment }; this.Data.Notifications.Add(notification); this.Data.SaveChanges(); var data = this.Data.Comments .All().Where(t => t.Id == comment.Id) .Select(CommentViewModel.Create) .FirstOrDefault(); return(this.Ok(data)); }
public IHttpActionResult AddNewComment(AddCommentBindingModel bindingModel, int postId) { if (bindingModel == null) { return(this.BadRequest("Invalid data!")); } if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var currentUserId = this.UserIdProvider.GetUserId(); var currentUser = this.Data.Users.Find(currentUserId); var post = this.Data.Posts.Find(postId); if (post == null) { return(this.NotFound()); } if (this.HasAuthorization(currentUser, post)) { return(this.BadRequest("Unable to post comment. You can comment only on your friend's posts or post made on their wall.")); } Comment comment = new Comment { Content = bindingModel.CommentContent, PostedOn = DateTime.Now, PostId = postId, AuthorId = currentUserId }; post.Comments.Add(comment); this.Data.SaveChanges(); CommentViewModel commentViewModel = CommentViewModel.ConvertTo(comment, currentUser); return(this.Ok(commentViewModel)); }
public IHttpActionResult AddNewComment(AddCommentBindingModel bindingModel, int postId) { if (bindingModel == null) { return this.BadRequest("Invalid data!"); } if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var currentUserId = this.UserIdProvider.GetUserId(); var currentUser = this.Data.Users.Find(currentUserId); var post = this.Data.Posts.Find(postId); if (post == null) { return this.NotFound(); } if (this.HasAuthorization(currentUser, post)) { return this.BadRequest("Unable to post comment. You can comment only on your friend's posts or post made on their wall."); } Comment comment = new Comment { Content = bindingModel.CommentContent, PostedOn = DateTime.Now, PostId = postId, AuthorId = currentUserId }; post.Comments.Add(comment); this.Data.SaveChanges(); CommentViewModel commentViewModel = CommentViewModel.ConvertTo(comment, currentUser); return this.Ok(commentViewModel); }
public IHttpActionResult EditComment([FromUri] int postId, int commentId, [FromBody] AddCommentBindingModel model) { var post = this.Data.Posts.Find(postId); if (post == null) { return(this.NotFound()); } var postComment = post.Comments.FirstOrDefault(c => c.Id == commentId); if (postComment == null) { return(this.NotFound()); } var currentUserId = User.Identity.GetUserId(); var currentUser = this.Data.Users.FirstOrDefault(x => x.Id == currentUserId); if (currentUser == null) { return(this.BadRequest("Invalid user token! Please login again!")); } if (postComment.Author.Id != currentUserId) { return(this.BadRequest("Not allowed. User must be author of comment.")); } postComment.Content = model.CommentContent; this.Data.SaveChanges(); var postCommentViewModel = this.Data.Comments.Where(c => c.Id == postComment.Id) .Select(CommentViewModel.Create) .FirstOrDefault(); postCommentViewModel.PostId = post.Id; return(this.Ok(postCommentViewModel)); }
public IHttpActionResult AddCommentToPost(int postId, AddCommentBindingModel model) { var post = this.Data.Posts.Find(postId); if (post == null) { return(this.NotFound()); } if (model == null) { return(this.BadRequest("Model cannot be null!")); } if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var userId = this.User.Identity.GetUserId(); var comment = new Comment() { Content = model.Content, PostId = post.Id, PostedOn = DateTime.Now, UserId = userId }; this.Data.Comments.Add(comment); this.Data.SaveChanges(); var data = this.Data.Comments .Where(c => c.Id == comment.Id) .Select(CommentDataModel.Create) .FirstOrDefault(); return(this.Ok(data)); }
public IHttpActionResult EditComment(AddCommentBindingModel bindingModel, int postId, int commentId) { if (bindingModel == null) { return(this.BadRequest("Invalid data!")); } if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var post = this.Data.Posts.Find(postId); if (post == null) { return(this.NotFound()); } var comment = this.Data.Comments.Find(commentId); if (comment == null) { return(this.NotFound()); } var currentUserId = this.UserIdProvider.GetUserId(); if (comment.AuthorId != currentUserId) { return(this.BadRequest("This comment is not yours! You have no permission to edit it.")); } comment.Content = bindingModel.CommentContent; Data.SaveChanges(); return(this.Ok()); }
public IHttpActionResult EditComment(int commentId, AddCommentBindingModel model) { var comment = this.Data.Comments.Find(commentId); if (comment == null) { return(this.NotFound()); } if (model == null) { return(this.BadRequest("Model cannot be null!")); } if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var userId = this.User.Identity.GetUserId(); if (userId != comment.UserId) { return(this.Unauthorized()); } comment.Content = model.Content; this.Data.SaveChanges(); var data = this.Data.Comments .Where(c => c.Id == comment.Id) .Select(CommentDataModel.Create) .FirstOrDefault(); return(this.Ok(data)); }
public IHttpActionResult ReplyComment(AddCommentBindingModel bindingModel, int postId, int commentId) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } if (bindingModel == null) { return this.BadRequest("Invalid data."); } var currentUserId = this.UserIdProvider.GetUserId(); var currentUser = this.Data.Users.Find(currentUserId); var post = this.Data.Posts.Find(postId); if (post == null) { return this.NotFound(); } var comment = this.Data.Comments.Find(commentId); if (comment == null) { return this.NotFound(); } if (this.HasAuthorization(currentUser, post)) { return this.BadRequest("Unable to comment. You can comment only posts of your friends or posts on their wall."); } CommentReply commentReply = new CommentReply { AuthorId = currentUserId, CommentId = comment.Id, Content = bindingModel.CommentContent, RepliedOn = DateTime.Now }; comment.Replies.Add(commentReply); this.Data.SaveChanges(); return this.Ok(); }
public IHttpActionResult EditComment(AddCommentBindingModel bindingModel, int postId, int commentId) { if (bindingModel == null) { return this.BadRequest("Invalid data!"); } if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var post = this.Data.Posts.Find(postId); if (post == null) { return this.NotFound(); } var comment = this.Data.Comments.Find(commentId); if (comment == null) { return this.NotFound(); } var currentUserId = this.UserIdProvider.GetUserId(); if (comment.AuthorId != currentUserId) { return this.BadRequest("This comment is not yours! You have no permission to edit it."); } comment.Content = bindingModel.CommentContent; Data.SaveChanges(); return this.Ok(); }