public HttpResponseMessage AddComment( int placeId, CommentModel commentModel, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { PlacesContext context = new PlacesContext(); using (context) { var user = UsersController.GetUserBySessionKey(context, sessionKey); var place = context.Places.FirstOrDefault(p => p.Id == placeId); if (place == null) { throw new ArgumentNullException("place", "A place with the specified ID does not exist."); } place.Comments.Add(new Comment() { Content = commentModel.Content, Date = DateTime.Now, Place = place, User = user, }); context.SaveChanges(); return this.Request.CreateResponse(HttpStatusCode.OK, commentModel); } }); return responseMsg; }
public HttpResponseMessage CreateComment(CommentModel commentModel, int id, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMessage = this.PerformOperationAndHandleExceptions( () => { PlacesContext context = new PlacesContext(); using (context) { var user = UsersController.GetUserBySessionKey(context, sessionKey); var place = context.Places.FirstOrDefault(p => p.Id == id); if (user == null) { throw new InvalidOperationException("Invalid username or password!"); } var comment = new Comment() { Content = commentModel.Content, Date = DateTime.Now, User = user }; place.Comments.Add(comment); context.SaveChanges(); var createdComment = new CommentModel() { Date = comment.Date, Content = comment.Content, PostedBy = comment.User.Nickname }; var response = this.Request.CreateResponse(HttpStatusCode.Created, createdComment); return response; } }); return responseMessage; }