-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Validation exception when Sort by or Sort direction are empty…
- Loading branch information
Showing
3 changed files
with
108 additions
and
107 deletions.
There are no files selected for viewing
204 changes: 102 additions & 102 deletions
204
Enigmatry.Entry.Core.EntityFramework/EntityQueryableExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<T> SingleOrNotFoundAsync<T>(this IQueryable<T> query, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
var result = await query.SingleOrDefaultAsync(cancellationToken); | ||
return result ?? throw new EntityNotFoundException(typeof(T).Name); | ||
} | ||
|
||
public static async Task<TDestination> SingleOrDefaultMappedAsync<TSource, TDestination>( | ||
this IQueryable<TSource> query, IMapper mapper, CancellationToken cancellationToken = default) | ||
{ | ||
var item = await query.SingleOrDefaultAsync(cancellationToken); | ||
return mapper.Map<TDestination>(item); | ||
} | ||
|
||
public static async Task<List<TDestination>> ToListMappedAsync<TSource, TDestination>( | ||
this IQueryable<TSource> query, IMapper mapper, CancellationToken cancellationToken = default) | ||
{ | ||
var items = await query.ToListAsync(cancellationToken); | ||
return mapper.Map<List<TDestination>>(items); | ||
} | ||
|
||
public static PagedResponse<T> ToPagedResponse<T>(this IQueryable<T> 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<T> | ||
{ | ||
Items = items, | ||
TotalCount = totalCount, | ||
PageSize = request.PageSize, | ||
PageNumber = request.PageNumber | ||
}; | ||
} | ||
|
||
public static async Task<PagedResponse<T>> ToPagedResponseAsync<T>(this IQueryable<T> 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<T> | ||
{ | ||
Items = items, | ||
TotalCount = totalCount, | ||
PageSize = request.PageSize, | ||
PageNumber = request.PageNumber | ||
}; | ||
} | ||
|
||
private static IQueryable<T> OrderByDynamic<T>(this IQueryable<T> 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<T> SingleOrNotFoundAsync<T>(this IQueryable<T> query, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
var result = await query.SingleOrDefaultAsync(cancellationToken); | ||
return result ?? throw new EntityNotFoundException(typeof(T).Name); | ||
} | ||
|
||
public static async Task<TDestination> SingleOrDefaultMappedAsync<TSource, TDestination>( | ||
this IQueryable<TSource> query, IMapper mapper, CancellationToken cancellationToken = default) | ||
{ | ||
var item = await query.SingleOrDefaultAsync(cancellationToken); | ||
return mapper.Map<TDestination>(item); | ||
} | ||
|
||
public static async Task<List<TDestination>> ToListMappedAsync<TSource, TDestination>( | ||
this IQueryable<TSource> query, IMapper mapper, CancellationToken cancellationToken = default) | ||
{ | ||
var items = await query.ToListAsync(cancellationToken); | ||
return mapper.Map<List<TDestination>>(items); | ||
} | ||
|
||
public static PagedResponse<T> ToPagedResponse<T>(this IQueryable<T> 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<T> | ||
{ | ||
Items = items, | ||
TotalCount = totalCount, | ||
PageSize = request.PageSize, | ||
PageNumber = request.PageNumber | ||
}; | ||
} | ||
|
||
public static async Task<PagedResponse<T>> ToPagedResponseAsync<T>(this IQueryable<T> 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<T> | ||
{ | ||
Items = items, | ||
TotalCount = totalCount, | ||
PageSize = request.PageSize, | ||
PageNumber = request.PageNumber | ||
}; | ||
} | ||
|
||
private static IQueryable<T> OrderByDynamic<T>(this IQueryable<T> query, string? orderBy, string orderDirection = "asc") => | ||
String.IsNullOrWhiteSpace(orderBy) ? query : query.OrderBy($"{orderBy} {orderDirection}"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
using MediatR; | ||
using System; | ||
using MediatR; | ||
|
||
namespace Enigmatry.Entry.Core.Paging | ||
{ | ||
public class PagedRequest<TResponse> : IRequest<PagedResponse<TResponse>>, IPagedRequest | ||
{ | ||
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; | ||
} | ||
} |