public HttpResponseMessage PostCreateComment(CommentCreateModel inputComment, string sessionKey) { HttpResponseMessage responseMessage = this.PerformOperationAndHandleExceptions( () => { ForumContext context = new ForumContext(); User currentUser = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey); Post currentPost = context.Posts.FirstOrDefault(pst => pst.Id == inputComment.PostId); if (currentUser == null) { throw new ArgumentNullException("If you want to comment posts, you should be logged or registered."); } if (currentPost == null) { throw new ArgumentNullException("The post you are trying to comment, doesn't exist."); } Comment newComment = new Comment() { Author = currentUser, Post = currentPost, Content = inputComment.Content, CreationDate = DateTime.Now }; context.Comments.Add(newComment); context.SaveChanges(); CommentModel createdComment = new CommentModel() { Author = newComment.Author.Username, Content = newComment.Content, CreationDate = newComment.CreationDate }; return this.Request.CreateResponse(HttpStatusCode.Created, createdComment); }); return responseMessage; }
//public HttpResponseMessage PostNewPost(PostNewModel model) //{ // var responseMsg = this.PerformOperationAndHandleExceptions( // () => // { // var context = new BlogSystemContext(); // using (context) // { // var user = context.Users.FirstOrDefault( // usr => usr.SessionKey == sessionKey); // if (user == null) // { // throw new InvalidOperationException("Invalid sessionKey"); // } // if (model.Title == null || model.Text == null) // { // throw new ArgumentNullException("Post title or post text cannot be null"); // } // string[] titleWords = model.Title.Split( // new char[] { ' ', ',', '.', '!', '?', '\'', '(', ')' }, // StringSplitOptions.RemoveEmptyEntries); // IList<Tag> tags = new List<Tag>(); // if (model.Tags != null) // { // foreach (var item in model.Tags) // { // var tag = context.Tags.FirstOrDefault(t => t.Name == item.ToLower()); // if (tag == null) // { // tag = new Tag() // { // Name = item.ToLower() // }; // context.Tags.Add(tag); // context.SaveChanges(); // } // tags.Add(tag); // } // } // foreach (var item in titleWords) // { // var tag = context.Tags.FirstOrDefault(t => t.Name == item.ToLower()); // if (tag == null) // { // tag = new Tag() // { // Name = item.ToLower() // }; // context.Tags.Add(tag); // context.SaveChanges(); // } // tags.Add(tag); // } // var post = new Post() // { // Title = model.Title, // Text = model.Text, // PostDate = DateTime.Now, // User = user, // Tags = tags // }; // context.Posts.Add(post); // context.SaveChanges(); // var createdModel = new PostCreatedModel() // { // Id = post.Id, // Title = post.Title // }; // var response = // this.Request.CreateResponse(HttpStatusCode.Created, // createdModel); // return response; // } // }); // return responseMsg; //} //[ActionName("comment")] public HttpResponseMessage PutComment(int id, CommentModel model, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var context = new ForumContext(); using (context) { var post = context.Posts.FirstOrDefault(p => p.Id == id); if (post == null) { throw new InvalidOperationException("Invalid id"); } var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey); //var user = context.Users.FirstOrDefault(u => u.Id == 2); if (user == null) { throw new InvalidOperationException("Invalid sessionKey"); } if (model.Text == null) { throw new ArgumentNullException("Comment text cannot be null"); } var comment = new Comment() { Text = model.Text, Creator = user, CreatedOn = DateTime.Now, Post = post }; context.Comments.Add(comment); context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.OK, "NULL"); return response; } }); return responseMsg; }