public HttpResponseMessage PutComment(string sessionKey, LeaveCommentModel commentModel, int postId) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var context = new BloggingSystemContext(); using (context) { var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey); var post = context.Posts.FirstOrDefault(p => p.Id == postId); var comment = new Comment() { Text = commentModel.Text, User = user, Post = post, PostDate = DateTime.Now }; context.Comments.Add(comment); context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.OK); return response; } }); return responseMsg; }
public HttpResponseMessage GetPosts(int Id, [FromBody] CommentInPostModel commentModel, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var messageResponse = this.TryExecuteOperation<HttpResponseMessage>(() => { var user = unitOfWork.userRepository.All().Single(x => x.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("User has not logged in!"); } var matchedPost = unitOfWork.postRepository.All().SingleOrDefault(x => x.PostId == Id); if (matchedPost == null) { throw new ArgumentException("The thread does not exist!"); } Comment commentToAdd = new Comment() { Text = commentModel.Text, PostDate = DateTime.Now, CommentedBy = user }; matchedPost.Comments.Add(commentToAdd); this.unitOfWork.postRepository.Update(matchedPost.PostId, matchedPost); return Request.CreateResponse(HttpStatusCode.Created, PostResponseModel.FromEntity(matchedPost)); }); return messageResponse; }