Skip to content

Commit

Permalink
Revert "Validation exception when Sort by or Sort direction are empty…
Browse files Browse the repository at this point in the history
… in the request. (#57)" (#58)

This reverts commit 675226c.
  • Loading branch information
msljivic authored Dec 1, 2023
1 parent 675226c commit f07b176
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 107 deletions.
204 changes: 102 additions & 102 deletions Enigmatry.Entry.Core.EntityFramework/EntityQueryableExtensions.cs
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}");
}
4 changes: 2 additions & 2 deletions Enigmatry.Entry.Core/Paging/IPagedRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
7 changes: 4 additions & 3 deletions Enigmatry.Entry.Core/Paging/PagedRequest.cs
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;
}
}

0 comments on commit f07b176

Please sign in to comment.