-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScopesCollection.cs
More file actions
46 lines (38 loc) · 1.52 KB
/
Copy pathScopesCollection.cs
File metadata and controls
46 lines (38 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
namespace HttpsRichardy.Federation.Infrastructure.Persistence;
public sealed class ScopesCollection(IMongoDatabase database) :
AggregateCollection<Scope>(database, Collections.Scopes),
IScopeCollection
{
public async Task<IReadOnlyCollection<Scope>> GetScopesAsync(
ScopeFilters filters, CancellationToken cancellation = default)
{
var pipeline = PipelineDefinitionBuilder
.For<Scope>()
.As<Scope, Scope, BsonDocument>()
.FilterScopes(filters)
.Paginate(filters.Pagination)
.Sort(filters.Sort);
var options = new AggregateOptions { AllowDiskUse = true };
var aggregation = await _collection.AggregateAsync(pipeline, options, cancellation);
var bsonDocuments = await aggregation.ToListAsync(cancellation);
var scopes = bsonDocuments
.Select(bson => BsonSerializer.Deserialize<Scope>(bson))
.ToList();
return scopes;
}
public async Task<long> CountAsync(ScopeFilters filters, CancellationToken cancellation = default)
{
var pipeline = PipelineDefinitionBuilder
.For<Scope>()
.As<Scope, Scope, BsonDocument>()
.FilterScopes(filters)
.Count();
var aggregation = await _collection.AggregateAsync(pipeline, cancellationToken: cancellation);
var result = await aggregation.FirstOrDefaultAsync(cancellation);
if (result == null)
{
return 0;
}
return result.Count;
}
}