public IHttpActionResult Create(PostRequestModel postModel) { if (postModel.ValidateRequest(this, ModelState)) { // valid ApiRequest. create post postModel.Post.Created = DateTime.Now; postModel.Post.Updated = postModel.Post.Created; _db.Posts.Add(postModel.Post); _db.SaveChanges(); ApiResponse.HttpStatusCode = HttpStatusCode.Created; ApiResponse.Data = null; } return ResponseContent(ApiResponse); }
public IHttpActionResult Update(PostRequestModel postModel) { if (postModel.ValidateRequest(this, ModelState)) { // valid ApiRequest. update post var postToUpdate = _db.Posts.First(p => p.ID == postModel.Post.ID); // postModel.ValidateRequest validates Post.ID has post postToUpdate.ID = postModel.Post.ID; postToUpdate.Title = postModel.Post.Title; postToUpdate.Content = postModel.Post.Content; postToUpdate.Updated = DateTime.Now; postToUpdate.Attachment = postModel.Post.Attachment; if (postToUpdate.Attachment == true && !string.IsNullOrEmpty(postModel.Post.AttachmentPath)) { postToUpdate.AttachmentPath = postModel.Post.AttachmentPath; } _db.SaveChanges(); ApiResponse.HttpStatusCode = HttpStatusCode.OK; ApiResponse.Data = null; } return ResponseContent(ApiResponse); }