Add cancellation tokens

This commit is contained in:
honzapatCZ 2025-03-04 00:42:52 +01:00
parent 41fb0b668d
commit 51f66b2b2c
3 changed files with 11 additions and 9 deletions

View File

@ -158,9 +158,9 @@ namespace NejCommon.Controllers
return query; return query;
} }
protected virtual Task<PaginationResponse<TGetAllResponse>> ApplyPagination(IQueryable<TType> query, Pagination pag) protected virtual Task<PaginationResponse<TGetAllResponse>> ApplyPagination(IQueryable<TType> query, Pagination pag, CancellationToken cancellationToken = default)
{ {
return query.ApplyPaginationRes<TType, TGetAllResponse>(providers, pag); return query.ApplyPaginationRes<TType, TGetAllResponse>(providers, pag, cancellationToken: cancellationToken);
} }
/// <summary> /// <summary>
@ -169,9 +169,9 @@ namespace NejCommon.Controllers
/// <response code="200">Success</response> /// <response code="200">Success</response>
/// <response code="0">There was an error</response> /// <response code="0">There was an error</response>
[HttpGet] [HttpGet]
public virtual async Task<Results<BadRequest<Error>, Ok<PaginationResponse<TGetAllResponse>>>> GetAll([FromServices] TOwner company, [FromQuery] Pagination pag) public virtual async Task<Results<BadRequest<Error>, Ok<PaginationResponse<TGetAllResponse>>>> GetAll([FromServices] TOwner company, [FromQuery] Pagination pag, CancellationToken cancellationToken)
{ {
var data = await ApplyPagination(ApplyDefaultOrdering(GetQuery(company)).AsNoTrackingWithIdentityResolution(), pag); var data = await ApplyPagination(ApplyDefaultOrdering(GetQuery(company)).AsNoTrackingWithIdentityResolution(), pag, cancellationToken);
//Console.Writeline(data.Data); //Console.Writeline(data.Data);

View File

@ -81,6 +81,8 @@ namespace NejCommon.Models
{ {
throw new ArgumentNullException(nameof(bindingContext)); throw new ArgumentNullException(nameof(bindingContext));
} }
var cancellationToken = bindingContext.HttpContext.RequestAborted;
cancellationToken.ThrowIfCancellationRequested();
//get the binding parameter attributes //get the binding parameter attributes
@ -133,7 +135,7 @@ namespace NejCommon.Models
query = query.Where(compiledExp); query = query.Where(compiledExp);
} }
var model = await query.FirstOrDefaultAsync(); var model = await query.FirstOrDefaultAsync(cancellationToken);
if (model == null) if (model == null)
{ {

View File

@ -17,9 +17,9 @@ public static class Extensions
query = query.Take(pag.Count); query = query.Take(pag.Count);
return query; return query;
} }
public static async Task<PaginationResponse<TResponseType>> ApplyPaginationRes<TType, TResponseType>(this IQueryable<TType> query, IServiceProvider providers, Pagination pag, Expression<Func<TType, TResponseType>>? projector = null) where TResponseType : IAutomappedAttribute<TType, TResponseType>, new() public static async Task<PaginationResponse<TResponseType>> ApplyPaginationRes<TType, TResponseType>(this IQueryable<TType> query, IServiceProvider providers, Pagination pag, Expression<Func<TType, TResponseType>>? projector = null, CancellationToken cancellationToken = default) where TResponseType : IAutomappedAttribute<TType, TResponseType>, new()
{ {
var totalCount = await query.CountAsync(); var totalCount = await query.CountAsync(cancellationToken);
query = query.Skip(pag.Offset); query = query.Skip(pag.Offset);
query = query.Take(pag.Count); query = query.Take(pag.Count);
@ -35,7 +35,7 @@ public static class Extensions
}; };
} }
public static async Task<PaginationResponse<TResponseType>> ApplySearchPaginationRes<TType, TResponseType, TEntityBinder>(this IQueryable<TType> query, string? search, IServiceProvider providers, Pagination pag, List<Expression<Func<TType, string, bool>>> matchers, Expression<Func<TType, TResponseType>>? projector = null) public static async Task<PaginationResponse<TResponseType>> ApplySearchPaginationRes<TType, TResponseType, TEntityBinder>(this IQueryable<TType> query, string? search, IServiceProvider providers, Pagination pag, List<Expression<Func<TType, string, bool>>> matchers, Expression<Func<TType, TResponseType>>? projector = null, CancellationToken cancellationToken = default)
where TType : class where TType : class
where TResponseType : IAutomappedAttribute<TType, TResponseType>, new() where TResponseType : IAutomappedAttribute<TType, TResponseType>, new()
where TEntityBinder : EntityBinder<TType> where TEntityBinder : EntityBinder<TType>
@ -90,7 +90,7 @@ public static class Extensions
} }
} }
return await query.ApplyPaginationRes<TType, TResponseType>(providers, pag, projector); return await query.ApplyPaginationRes<TType, TResponseType>(providers, pag, projector, cancellationToken);
} }
[Projectable] [Projectable]