insight/src/Core/Insight.Infrastructure.Web/Extensions/MongoCollectionExtensions.cs

44 lines
1.5 KiB
C#
Raw Normal View History

2023-12-18 16:31:00 +01:00
using Insight.Infrastructure.Models;
using Microsoft.AspNetCore.Http;
using MongoDB.Bson;
using MongoDB.Driver;
namespace Insight.Infrastructure.Web;
public static class MongoCollectionExtensions
{
public static async Task<PagedList<TData>> GetPagedAsync<TData>(
this IMongoCollection<TData> collection,
HttpRequest request,
HttpResponse response,
FilterDefinition<TData>? filter = null,
SortDefinition<TData>? sort = null,
int offset = 0,
int limit = 10,
CancellationToken cancellationToken = default)
{
var result = await Infrastructure.MongoCollectionExtensions.GetPagedAsync(collection, filter, sort, offset, limit, cancellationToken).ConfigureAwait(false);
request?.AddPagination(result);
response?.AddPagination(result);
return result;
}
public static async Task<PagedList<TResult>> GetPagedAsync<TData, TResult>(
this IMongoCollection<TData> collection,
HttpRequest request,
HttpResponse response,
IAggregateFluent<BsonDocument> query,
int offset = 0,
int limit = 10,
CancellationToken cancellationToken = default)
{
var result = await Infrastructure.MongoCollectionExtensions.GetPagedAsync<TData, TResult>(collection, query, offset, limit, cancellationToken).ConfigureAwait(false);
request?.AddPagination(result);
response?.AddPagination(result);
return result;
}
}