public IHttpActionResult Edit(int id, [FromBody] PostUploadModel newPostModel) { var post = this.data.Posts.All().Where(p => p.Id == id).Select(p => new { p.Id, p.UserId }).FirstOrDefault(); if (post == null) { return(this.NotFound()); } if (newPostModel == null) { this.ModelState.AddModelError("post", "There is no post uploaded"); } if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var postUpdate = Mapper.Map <Post>(newPostModel); postUpdate.Id = post.Id; postUpdate.UserId = post.UserId; // ReSharper disable once PossibleNullReferenceException postUpdate.Tags = this.GetOrCreateTags(newPostModel.Tags); this.data.Posts.Update(postUpdate); this.data.SaveChanges(); this.data.Posts.Detach(postUpdate); return(this.Ok(newPostModel)); }
public IHttpActionResult Add([FromBody] PostUploadModel newPostModel) { if (newPostModel == null) { this.ModelState.AddModelError("post", "There is no post"); } if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } // ReSharper disable once PossibleNullReferenceException newPostModel.UserId = this.User.Identity.GetUserId(); var post = Mapper.Map <Post>(newPostModel); post.Tags = this.GetOrCreateTags(newPostModel.Tags); this.data.Posts.Add(post); this.data.SaveChanges(); this.data.Posts.Detach(post); newPostModel.Id = post.Id; return(this.Ok(newPostModel)); }