/// <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();
        }
Example #2
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();
        }