public async Task DeleteAsync(int id) { var media = await _entities.SingleAsync(m => m.Id == id); _entities.Remove(media); await _db.SaveChangesAsync(); }
/// <summary> /// Deletes a tag and its associated posts. /// </summary> /// <param name="id"></param> /// <returns></returns> /// <remarks> /// Tag and PostTag are related by FK with cascade delete. /// </remarks> public async Task DeleteAsync(int id) { var tag = await _entities.SingleAsync(t => t.Id == id); _db.Remove(tag); // cascade delete will take care of PostTag assoc await _db.SaveChangesAsync(); }
/// <summary> /// Creates a <see cref="Post"/>. /// </summary> /// <param name="post">The post to create.</param> /// <param name="tagTitles">A list of tag titles associated with the post.</param> /// <returns> /// The inserted post with id. /// </returns> public async Task <Post> CreateAsync(Post post, int?categoryId, string categoryTitle, IEnumerable <string> tagTitles) { // Category if (!categoryTitle.IsNullOrEmpty()) { // from metaweblog with a cat inputted post.Category = _db.Set <Category>().First(c => c.Title.Equals(categoryTitle, StringComparison.CurrentCultureIgnoreCase)); } else { post.CategoryId = categoryId.HasValue ? // from browser categoryId : // from metaweblog with no cat inputted Convert.ToInt32(_db.Set <Meta>().First(m => m.Key.Equals("blogsettings.defaultcategoryid")).Value); } // PostTags if (!tagTitles.IsNullOrEmpty()) { // make sure list has no empty strings and only unique values tagTitles = tagTitles.Where(s => !string.IsNullOrWhiteSpace(s)).Distinct(); var tags = _db.Set <Tag>(); // all tags foreach (var title in tagTitles) { // lookup the tag (any new tag is already created prior) var tag = tags.First(t => t.Title.Equals(title, StringComparison.CurrentCultureIgnoreCase)); post.PostTags.Add(new PostTag { Post = post, Tag = tag }); } } await _entities.AddAsync(post); await _db.SaveChangesAsync(); return(post); }
/// <summary> /// Creates a <see cref="Post"/>. /// </summary> /// <param name="post">The post to create.</param> /// <param name="categoryTitle">The category title is available when called from metaweblog.</param> /// <param name="tagTitles">A list of tag titles associated with the post.</param> /// <returns> /// The inserted post with id. /// </returns> public async Task <Post> CreateAsync(Post post, string categoryTitle, IEnumerable <string> tagTitles) { // Category if (!categoryTitle.IsNullOrEmpty()) { // cat title present, olw and setup post.Category = _db.Set <Category>().First(c => c.Title.ToUpper() == categoryTitle.ToUpper()); } else if (!post.CategoryId.HasValue) // from browser CategoryId will have value { // from metaweblog with no cat inputted, give it the default cat id post.CategoryId = Convert.ToInt32(_db.Set <Meta>().First(m => m.Key.Equals("blogsettings.defaultcategoryid")).Value); } // PostTags if (!tagTitles.IsNullOrEmpty()) { // make sure list has no empty strings and only unique values tagTitles = tagTitles.Where(s => !string.IsNullOrWhiteSpace(s)).Distinct(); var tags = _db.Set <Tag>(); // all tags foreach (var title in tagTitles) { // lookup the tag (any new tag is already created prior) var tag = tags.First(t => t.Title.ToUpper() == title.ToUpper()); post.PostTags.Add(new PostTag { Post = post, Tag = tag }); } } await _entities.AddAsync(post); await _db.SaveChangesAsync(); return(post); }
/// <summary> /// Deletes a <see cref="Post"/> by Id, if the post is a root page, /// it will also delete all child pages. /// </summary> public async Task DeleteAsync(int id) { // SingleAsync will throw if id is not found or not unique var post = await _entities.SingleAsync(c => c.Id == id); // root page if (post.Type == EPostType.Page && post.RootId == 0) { var posts = _entities.Where(po => po.RootId == id); _db.RemoveRange(posts); } else { _db.Remove(post); } await _db.SaveChangesAsync(); }
/// <summary> /// Deletes a <see cref="Category"/> by id and re-categorize its posts to the given /// default category id. /// </summary> /// <remarks> /// Since <see cref="Post.CategoryId"/> is nullable, there is no Cascade Delete between /// Post and Category, which happens to be what we want. User can choose to delete a /// category and we should delete all posts associated with that category, instead we /// apply the default category on these posts. /// /// The defaultCategoryId is BlogSettings DefaultCategoryId, I choose to have it pass in /// from BLL for convenience instead of querying Meta for it. /// </remarks> public async Task DeleteAsync(int id, int defaultCategoryId) { if (id == defaultCategoryId) { return; } // remove it var category = await _entities.SingleAsync(c => c.Id == id); _db.Remove(category); // update its posts to default category var posts = _db.Set <Post>().Where(p => p.CategoryId == id); foreach (var post in posts) { post.CategoryId = defaultCategoryId; } await _db.SaveChangesAsync(); }