Esempio n. 1
0
 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;
 }
Esempio n. 2
0
 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));
 }
Esempio n. 3
0
 public bool Update(Comment comment)
 {
     bool update = comments.ContainsKey(comment.ID);
     comments[comment.ID] = comment;
     return update;
 }
Esempio n. 4
0
 public bool TryGet(int id, out Comment comment)
 {
     return comments.TryGetValue(id, out comment);
 }
Esempio n. 5
0
 public Comment Add(Comment comment)
 {
     comment.ID = nextID++;
     comments[comment.ID] = comment;
     return comment;
 }