public IHttpActionResult Update(PostDataModel postData) { if (postData == null || postData.Id == null) { return this.BadRequest("You must provide a valid post id and a new content."); } var post = this.data.Posts.GetById(postData.Id); if (post == null) { return this.BadRequest("Invalid post id."); } if (!string.IsNullOrWhiteSpace(postData.Content)) { post.Content = postData.Content; } if (!string.IsNullOrWhiteSpace(postData.Tags)) { var newTags = this.GetTagsFromDataModel(postData); post.Tags = newTags; } this.data.SaveChanges(); return this.Ok(); }
private ICollection<Tag> GetTagsFromDataModel(PostDataModel postData) { // get the tags from the string var tags = new List<Tag>(); if (!string.IsNullOrWhiteSpace(postData.Tags)) { tags = postData.Tags.Split(',').Select(t => new Tag { Name = t.Trim() }).ToList(); } // add the new tags to the database for (var i = 0; i < tags.Count; i++) { var currentTag = tags[i]; var existingTag = this.data.Tags.Find(t => t.Name == currentTag.Name).FirstOrDefault(); if (existingTag != null) { tags[i] = existingTag; } else { this.data.Tags.Add(tags[i]); } } return tags; }
public static Post ToPost(PostDataModel postDataModel) { return(new Post { Title = postDataModel.Title, Content = postDataModel.Content, UserId = postDataModel.AuthorId.Value }); }
public static Post ToPost(PostDataModel postDataModel) { return new Post { Title = postDataModel.Title, Content = postDataModel.Content, UserId = postDataModel.AuthorId.Value }; }
public IHttpActionResult Add(PostDataModel postData) { if (postData == null || postData.AuthorId == null || string.IsNullOrWhiteSpace(postData.Content)) { return this.BadRequest("You must provide post content and author."); } var newPost = PostDataModel.ToPost(postData); if (!string.IsNullOrWhiteSpace(postData.Tags)) { var tags = this.GetTagsFromDataModel(postData); newPost.Tags = tags; } this.data.Posts.Add(newPost); this.data.SaveChanges(); return this.Ok(); }