Пример #1
0
        /// <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();
        }
Пример #2
0
        /// <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();
        }
Пример #3
0
        /// <summary>
        /// Deletes a <see cref="Post"/> by Id, if the post is a parent page,
        /// it will also delete all child pages.
        /// </summary>
        public new async Task DeleteAsync(int id)
        {
            // throws if id not found or not unique
            var post = await _entities.SingleAsync(c => c.Id == id);

            // if blog post or child page
            if (post.Type == EPostType.BlogPost || (post.ParentId.HasValue && post.ParentId > 0))
            {
                _db.Remove(post);
            }
            else // parent page which may have children
            {
                var posts = _entities.Where(p => p.ParentId == id).ToArray();
                Array.Resize(ref posts, posts.Length + 1); // put the parent itself in
                posts[posts.Length - 1] = post;

                _db.RemoveRange(posts);
            }

            await _db.SaveChangesAsync();
        }
Пример #4
0
        /// <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();
        }