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 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); }