private void ValidateCommentPostModel(CommentPostModel model) { if (model == null) { throw new ArgumentNullException("Comment post model is null."); } if (String.IsNullOrEmpty(model.Text)) { throw new ArgumentNullException("Comment text is null or empty."); } }
public void PostLeaveComment_WhenCommentIsValidAndPostExists_ShouldSaveToDB() { var userModel = RegisterValidUserAndReturnModel(); var headers = new Dictionary<string, string>(); headers["X-sessionKey"] = userModel.SessionKey; var postModel = new PostCreateRequestModel { Tags = new List<string> { "tag1", "tag2", "tag3" }, Title = "TestPost1", Text = "This is just a test post. It's labeled 'TestPostOne'" }; // post a post var response = httpServer.Post("api/posts/", postModel, headers); var commentModel = new CommentPostModel { Text = "This is just a test post. It's labeled 'TestPostOne'" }; var contentString = response.Content.ReadAsStringAsync().Result; var postModelAfterSavingToDB = JsonConvert.DeserializeObject<PostModel>(contentString); int postId = postModelAfterSavingToDB.Id; // post a comment response = httpServer.Post("api/posts/" + postId + "/comment", commentModel, headers); Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); }
public HttpResponseMessage PutCommentOnAPost( int postId, CommentPostModel model, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMsg = this.PerfromOperationAndHandleException(() => { this.ValidateCommentPostModel(model); using (var dbContext = new AcadBlogContext()) { var user = this.ValidateAndGetUserBySessionKey(dbContext, sessionKey); var post = this.ValidateAndGetPostById(dbContext, postId); var newComment = new Comment() { Content = model.Text, DateCreated = DateTime.Now, User = user, Post = post }; dbContext.Comments.Add(newComment); dbContext.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.OK); return response; } }); return responseMsg; }