using Microsoft.EntityFrameworkCore;
namespace MyWebLog.Data;
public static class PageExtensions
{
///
/// Count the number of pages
///
/// The number of pages
public static async Task CountAll(this DbSet db) =>
await db.CountAsync().ConfigureAwait(false);
///
/// Count the number of pages in the page list
///
/// The number of pages in the page list
public static async Task CountListed(this DbSet db) =>
await db.CountAsync(p => p.ShowInPageList).ConfigureAwait(false);
///
/// Retrieve all pages (non-tracked)
///
/// A list of all pages
public static async Task> FindAll(this DbSet db) =>
await db.OrderBy(p => p.Title).ToListAsync().ConfigureAwait(false);
///
/// Retrieve a page by its ID (non-tracked)
///
/// The ID of the page to retrieve
/// The requested page (or null if it is not found)
public static async Task FindById(this DbSet db, string id) =>
await db.SingleOrDefaultAsync(p => p.Id == id).ConfigureAwait(false);
///
/// Retrieve a page by its ID, including its revisions (non-tracked)
///
/// The ID of the page to retrieve
/// The requested page (or null if it is not found)
public static async Task FindByIdWithRevisions(this DbSet db, string id) =>
await db.Include(p => p.Revisions).SingleOrDefaultAsync(p => p.Id == id).ConfigureAwait(false);
///
/// Retrieve a page by its permalink (non-tracked)
///
/// The permalink
/// The requested page (or null if it is not found)
public static async Task FindByPermalink(this DbSet db, string permalink) =>
await db.SingleOrDefaultAsync(p => p.Permalink == permalink).ConfigureAwait(false);
///
/// Retrieve a page of pages (non-tracked)
///
/// The page number to retrieve
/// The pages
public static async Task> FindPageOfPages(this DbSet db, int pageNbr) =>
await db.OrderBy(p => p.Title).Skip((pageNbr - 1) * 25).Take(25).ToListAsync().ConfigureAwait(false);
///
/// Retrieve a page by its ID (tracked)
///
/// The ID of the page to retrieve
/// The requested page (or null if it is not found)
public static async Task GetById(this DbSet db, string id) =>
await db.AsTracking().Include(p => p.Revisions).SingleOrDefaultAsync(p => p.Id == id).ConfigureAwait(false);
}