diff --git a/Enigmatry.Entry.Core.EntityFramework/EntityQueryableExtensions.cs b/Enigmatry.Entry.Core.EntityFramework/EntityQueryableExtensions.cs index 84fb6f0..2f1ca13 100644 --- a/Enigmatry.Entry.Core.EntityFramework/EntityQueryableExtensions.cs +++ b/Enigmatry.Entry.Core.EntityFramework/EntityQueryableExtensions.cs @@ -1,102 +1,102 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Dynamic.Core; -using System.Threading; -using System.Threading.Tasks; -using AutoMapper; -using Enigmatry.Entry.Core.Entities; -using Enigmatry.Entry.Core.Paging; -using Microsoft.EntityFrameworkCore; - -namespace Enigmatry.Entry.Core.EntityFramework; - -public static class EntityQueryableExtensions -{ - public static async Task SingleOrNotFoundAsync(this IQueryable query, - CancellationToken cancellationToken = default) - { - var result = await query.SingleOrDefaultAsync(cancellationToken); - return result ?? throw new EntityNotFoundException(typeof(T).Name); - } - - public static async Task SingleOrDefaultMappedAsync( - this IQueryable query, IMapper mapper, CancellationToken cancellationToken = default) - { - var item = await query.SingleOrDefaultAsync(cancellationToken); - return mapper.Map(item); - } - - public static async Task> ToListMappedAsync( - this IQueryable query, IMapper mapper, CancellationToken cancellationToken = default) - { - var items = await query.ToListAsync(cancellationToken); - return mapper.Map>(items); - } - - public static PagedResponse ToPagedResponse(this IQueryable query, IPagedRequest request) - { - if (request == null) - { - throw new ArgumentNullException(nameof(request)); - } - - var pagedQuery = query - .OrderByDynamic(request.SortBy, request.SortDirection); - - var skipPaging = request.PageSize == Int32.MaxValue; - - if (!skipPaging) - { - pagedQuery = pagedQuery.Skip((request.PageNumber - 1) * request.PageSize) - .Take(request.PageSize); - } - - var items = pagedQuery.ToList(); - - var totalCount = skipPaging ? items.Count : query.Count(); - - return new PagedResponse - { - Items = items, - TotalCount = totalCount, - PageSize = request.PageSize, - PageNumber = request.PageNumber - }; - } - - public static async Task> ToPagedResponseAsync(this IQueryable query, IPagedRequest request, - CancellationToken cancellationToken = default) - { - if (request == null) - { - throw new ArgumentNullException(nameof(request)); - } - - var pagedQuery = query - .OrderByDynamic(request.SortBy, request.SortDirection); - - var skipPaging = request.PageSize == Int32.MaxValue; - - if (!skipPaging) - { - pagedQuery = pagedQuery.Skip((request.PageNumber - 1) * request.PageSize) - .Take(request.PageSize); - } - - var items = await pagedQuery.ToListAsync(cancellationToken); - - var totalCount = skipPaging ? items.Count : await query.CountAsync(cancellationToken); - - return new PagedResponse - { - Items = items, - TotalCount = totalCount, - PageSize = request.PageSize, - PageNumber = request.PageNumber - }; - } - - private static IQueryable OrderByDynamic(this IQueryable query, string? orderBy, string? orderDirection) => - String.IsNullOrWhiteSpace(orderBy) ? query : query.OrderBy($"{orderBy} {orderDirection}"); -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Dynamic.Core; +using System.Threading; +using System.Threading.Tasks; +using AutoMapper; +using Enigmatry.Entry.Core.Entities; +using Enigmatry.Entry.Core.Paging; +using Microsoft.EntityFrameworkCore; + +namespace Enigmatry.Entry.Core.EntityFramework; + +public static class EntityQueryableExtensions +{ + public static async Task SingleOrNotFoundAsync(this IQueryable query, + CancellationToken cancellationToken = default) + { + var result = await query.SingleOrDefaultAsync(cancellationToken); + return result ?? throw new EntityNotFoundException(typeof(T).Name); + } + + public static async Task SingleOrDefaultMappedAsync( + this IQueryable query, IMapper mapper, CancellationToken cancellationToken = default) + { + var item = await query.SingleOrDefaultAsync(cancellationToken); + return mapper.Map(item); + } + + public static async Task> ToListMappedAsync( + this IQueryable query, IMapper mapper, CancellationToken cancellationToken = default) + { + var items = await query.ToListAsync(cancellationToken); + return mapper.Map>(items); + } + + public static PagedResponse ToPagedResponse(this IQueryable query, IPagedRequest request) + { + if (request == null) + { + throw new ArgumentNullException(nameof(request)); + } + + var pagedQuery = query + .OrderByDynamic(request.SortBy, request.SortDirection); + + var skipPaging = request.PageSize == Int32.MaxValue; + + if (!skipPaging) + { + pagedQuery = pagedQuery.Skip((request.PageNumber - 1) * request.PageSize) + .Take(request.PageSize); + } + + var items = pagedQuery.ToList(); + + var totalCount = skipPaging ? items.Count : query.Count(); + + return new PagedResponse + { + Items = items, + TotalCount = totalCount, + PageSize = request.PageSize, + PageNumber = request.PageNumber + }; + } + + public static async Task> ToPagedResponseAsync(this IQueryable query, IPagedRequest request, + CancellationToken cancellationToken = default) + { + if (request == null) + { + throw new ArgumentNullException(nameof(request)); + } + + var pagedQuery = query + .OrderByDynamic(request.SortBy, request.SortDirection); + + var skipPaging = request.PageSize == Int32.MaxValue; + + if (!skipPaging) + { + pagedQuery = pagedQuery.Skip((request.PageNumber - 1) * request.PageSize) + .Take(request.PageSize); + } + + var items = await pagedQuery.ToListAsync(cancellationToken); + + var totalCount = skipPaging ? items.Count : await query.CountAsync(cancellationToken); + + return new PagedResponse + { + Items = items, + TotalCount = totalCount, + PageSize = request.PageSize, + PageNumber = request.PageNumber + }; + } + + private static IQueryable OrderByDynamic(this IQueryable query, string? orderBy, string orderDirection = "asc") => + String.IsNullOrWhiteSpace(orderBy) ? query : query.OrderBy($"{orderBy} {orderDirection}"); +} diff --git a/Enigmatry.Entry.Core/Paging/IPagedRequest.cs b/Enigmatry.Entry.Core/Paging/IPagedRequest.cs index fe087b6..39679c0 100644 --- a/Enigmatry.Entry.Core/Paging/IPagedRequest.cs +++ b/Enigmatry.Entry.Core/Paging/IPagedRequest.cs @@ -4,7 +4,7 @@ public interface IPagedRequest { int PageNumber { get; set; } int PageSize { get; set; } - string? SortBy { get; set; } - string? SortDirection { get; set; } + string SortBy { get; set; } + string SortDirection { get; set; } } } diff --git a/Enigmatry.Entry.Core/Paging/PagedRequest.cs b/Enigmatry.Entry.Core/Paging/PagedRequest.cs index 3027255..533a3d2 100644 --- a/Enigmatry.Entry.Core/Paging/PagedRequest.cs +++ b/Enigmatry.Entry.Core/Paging/PagedRequest.cs @@ -1,4 +1,5 @@ -using MediatR; +using System; +using MediatR; namespace Enigmatry.Entry.Core.Paging { @@ -6,7 +7,7 @@ public class PagedRequest : IRequest>, IPage { public int PageNumber { get; set; } = 1; public int PageSize { get; set; } = 10; - public string? SortBy { get; set; } - public string? SortDirection { get; set; } + public string SortBy { get; set; } = String.Empty; + public string SortDirection { get; set; } = String.Empty; } }