public IHttpActionResult CreateNewsStory([FromBody]NewsStoryBindingModel model) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var newsStory = new NewsStory() { Title = model.Title, Content = model.Content, PublishDate = model.PublishDate }; this.Data.News.Add(newsStory); try { this.Data.SaveChanges(); var newNewsStory = this.Data.News.All().FirstOrDefault(n => n.Title == model.Title); if (newNewsStory == null) { return this.BadRequest("There was an error creating news story"); } return this.Created(string.Format("{0}", newNewsStory.Id), newNewsStory); } catch (DbUpdateException ex) { return this.BadRequest(string.Format("A news story with title {0} already exists.", model.Title)); } catch (Exception ex) { return this.BadRequest("There was a problem creating news story. Please try again"); } }
private bool FindNewsStoryByID(int id, out NewsStory newsStory) { newsStory = this.Data.News.Find(id); if (newsStory == null) { return false; } return true; }