public void EditPost_WhenDataIsValid_ShouldEditThePost() { using (new TransactionScope()) { this.db.Users.Add(this.firstUser); this.db.SaveChanges(); var newPost = new UserPost() { Content = "Content", PostDate = DateTime.Now }; this.firstUser.Posts.Add(newPost); this.db.Users.Update(this.firstUser); this.db.SaveChanges(); var editPostModel = new UserPostUpdateModel() { Id = newPost.Id, Content = "jyfyufufhfghfghfhg" }; var response = this.httpServer.CreatePutRequest( "/api/userposts/edit?sessionKey=" + this.firstUser.SessionKey, editPostModel); Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); var post = this.db.UserPosts .All() .Where(p => p.Content == "jyfyufufhfghfghfhg") .FirstOrDefault(); Assert.IsNotNull(post); Assert.AreEqual(newPost.Id, post.Id); } }
public HttpResponseMessage EditPost([FromBody] UserPostUpdateModel model, [FromUri] string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { this.ValidatePostContent(model.Content); var user = this.GetUserBySessionKey(sessionKey); var post = user.Posts.FirstOrDefault(p => p.Id == model.Id); if (post == null) { throw new ArgumentException("The user does not have a post with such id", "id"); } post.Content = model.Content; this.db.UserPosts.Update(post); this.db.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.Created); return(response); }); return(responseMsg); }