// GET api/<controller>/5 public Posts Get(int PostID) { data.PostThread posts = postFactory.GetPostByID(PostID); Posts _postView = new Posts(); List<data.Comment> _comments = commentFactory.GetCommentByPostID(posts.PostID); List<Comments> _commentView = new List<Comments>(); foreach (data.Comment item in _comments) { _commentView.Add(new Comments { CommentID = item.CommentID.ToString(), CommentAuthor = item.CommentAuthor, CommentContent = item.CommentContent, PostID = item.PostID.ToString() }); _postView.PostAuthor = posts.PostAuthor; _postView.PostContent = posts.PostContent; _postView.PostID = posts.PostID.ToString(); _postView.comments = _commentView; } return _postView; }
// POST api/<controller> public HttpResponseMessage PostThread(Posts post) { if (ModelState.IsValid) { data.PostThread postThread = new data.PostThread(); postThread.PostDate = DateTime.Now; postThread.PostContent = post.PostContent; postThread.PostAuthor = post.PostAuthor; postThread = postFactory.SavePost(postThread); post.PostID = postThread.PostID.ToString(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, post); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { PostID = post.PostID })); return response; } else { return Request.CreateResponse(HttpStatusCode.BadRequest); } }