// POST api/Comment public HttpResponseMessage PostPostComment(PostComment postcomment) { postcomment.CommentedBy = WebSecurity.CurrentUserId; postcomment.CommentedDate = DateTime.UtcNow; ModelState.Remove("postcomment.CommentedBy"); ModelState.Remove("postcomment.CommentedDate"); if (ModelState.IsValid) { db.PostComments.Add(postcomment); db.SaveChanges(); var usr = db.UserProfiles.FirstOrDefault(x => x.UserId == postcomment.CommentedBy); var ret = new { CommentedBy = postcomment.CommentedBy, CommentedByName = usr.UserName, CommentedByAvatar =imgFolder +(String.IsNullOrEmpty(usr.AvatarExt) ? defaultAvatar : postcomment.CommentedBy + "." + postcomment.UserProfile.AvatarExt), CommentedDate = postcomment.CommentedDate, CommentId = postcomment.CommentId, Message = postcomment.Message, PostId = postcomment.PostId }; HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, ret); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = postcomment.CommentId })); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }
// PUT api/Comment/5 public HttpResponseMessage PutPostComment(int id, PostComment postcomment) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != postcomment.CommentId) { return Request.CreateResponse(HttpStatusCode.BadRequest); } db.Entry(postcomment).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK); }