public HttpResponseMessage PostComment(Comment comment) { comment = repository.Add(comment); var response = Request.CreateResponse<Comment>(HttpStatusCode.Created, comment); response.Headers.Location = new Uri(Request.RequestUri, "/api/comments/" + comment.ID.ToString()); return response; }
public void PostComment() { Comment comment = new Comment() { Author = "Dan", Email = "*****@*****.**", Text = "I love ASP.NET Web API!" }; HttpConfiguration config = new HttpConfiguration(); WebApiConfig.Register(config); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost"); request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config; ICommentRepository repository = new InitialData(); CommentsController controller = new CommentsController(repository) { Request = request }; HttpResponseMessage response = controller.PostComment(comment); Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); Assert.IsNotNull(response.Headers.Location); Comment postedComment = response.Content.ReadAsAsync<Comment>().Result; Assert.IsTrue(repository.TryGet(postedComment.ID, out postedComment)); }
public bool Update(Comment comment) { bool update = comments.ContainsKey(comment.ID); comments[comment.ID] = comment; return update; }
public bool TryGet(int id, out Comment comment) { return comments.TryGetValue(id, out comment); }
public Comment Add(Comment comment) { comment.ID = nextID++; comments[comment.ID] = comment; return comment; }