2024-12-06 14:55:07 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Persistence.Database.Model;
|
|
|
|
|
using Persistence.Models;
|
|
|
|
|
using Persistence.Models.Requests;
|
|
|
|
|
|
|
|
|
|
namespace Persistence.Repository;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// класс с набором методов, необходимых для фильтрации записей
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class QueryBuilders
|
|
|
|
|
{
|
2024-12-06 17:06:16 +05:00
|
|
|
|
public static IQueryable<TEntity> Apply<TEntity>(this IQueryable<TEntity> query, SectionPartRequest request)
|
2024-12-06 14:55:07 +05:00
|
|
|
|
where TEntity : class, IWithSectionPart
|
|
|
|
|
{
|
|
|
|
|
if (request.IdSection.HasValue)
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(e => e.IdSection == request.IdSection);
|
|
|
|
|
}
|
|
|
|
|
if (request.DepthStart.HasValue)
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(e => e.DepthStart >= request.DepthStart);
|
|
|
|
|
}
|
|
|
|
|
if (request.DepthEnd.HasValue)
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(e => e.DepthEnd <= request.DepthEnd);
|
|
|
|
|
}
|
2024-12-06 17:06:16 +05:00
|
|
|
|
|
|
|
|
|
return query;
|
2024-12-06 14:55:07 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-06 17:06:16 +05:00
|
|
|
|
public static IQueryable<TEntity> Apply<TEntity>(this IQueryable<TEntity> query,DateTimeOffset momentUtc)
|
2024-12-06 14:55:07 +05:00
|
|
|
|
where TEntity : class, IChangeLog
|
|
|
|
|
{
|
|
|
|
|
momentUtc = momentUtc.ToUniversalTime();
|
|
|
|
|
|
|
|
|
|
query = query
|
|
|
|
|
.Where(e => e.Creation <= momentUtc)
|
|
|
|
|
.Where(e => e.Obsolete == null || e.Obsolete >= momentUtc);
|
2024-12-06 17:06:16 +05:00
|
|
|
|
|
|
|
|
|
return query;
|
2024-12-06 14:55:07 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static async Task<PaginationContainer<TDto>> ApplyPagination<TEntity, TDto>(
|
|
|
|
|
this IQueryable<TEntity> query,
|
|
|
|
|
PaginationRequest request,
|
|
|
|
|
Func<TEntity, TDto> Convert,
|
|
|
|
|
CancellationToken token)
|
|
|
|
|
where TEntity : class, IWithSectionPart
|
|
|
|
|
where TDto : class
|
|
|
|
|
{
|
|
|
|
|
if (String.IsNullOrEmpty(request.SortSettings))
|
|
|
|
|
{
|
|
|
|
|
query = query
|
|
|
|
|
.OrderBy(e => e.IdSection)
|
|
|
|
|
.ThenBy(e => e.DepthStart)
|
|
|
|
|
.ThenBy(e => e.DepthEnd);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
query = query.SortBy(request.SortSettings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var entities = await query
|
|
|
|
|
.Skip(request.Skip)
|
|
|
|
|
.Take(request.Take)
|
|
|
|
|
.ToArrayAsync(token);
|
|
|
|
|
|
2024-12-09 10:15:17 +05:00
|
|
|
|
var count = await query.CountAsync(token);
|
|
|
|
|
var items = entities.Select(Convert);
|
2024-12-06 14:55:07 +05:00
|
|
|
|
var result = new PaginationContainer<TDto>
|
|
|
|
|
{
|
|
|
|
|
Skip = request.Skip,
|
|
|
|
|
Take = request.Take,
|
2024-12-09 10:15:17 +05:00
|
|
|
|
Items = items,
|
|
|
|
|
Count = count
|
2024-12-06 14:55:07 +05:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|