-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
99 lines (78 loc) · 2.54 KB
/
Program.cs
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using CosmosRepositoryPagingSample;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.CosmosRepository;
using Microsoft.Azure.CosmosRepository.Paging;
var builder = WebApplication.CreateBuilder(args);
const string databaseName = "people-database";
const string peopleContainerName = "people-container";
const string partitionKey = "/partitionKey";
builder.Services.AddCosmosRepository(options =>
{
// In order to set this connection string run
// dotnet user-secrets set RepositoryOptions:CosmosConnectionString "<your-connection-string>
// options.CosmosConnectionString = "<taken-from-config>";
options.DatabaseId = databaseName;
options.ContainerPerItemType = true;
options.ContainerBuilder
.Configure<Person>(containerOptionsBuilder =>
{
containerOptionsBuilder
.WithContainer(peopleContainerName)
.WithPartitionKey(partitionKey);
});
});
var app = builder.Build();
app.MapGet("/", () => "Azure Cosmos Repository Paging Sample");
app.MapGet("/generate", async (IRepository<Person> repository) =>
{
var tasks = new List<Task>();
for (var i = 0; i < 100; i++)
{
var personBuilder = new Bogus.Person();
var person = new Person(
personBuilder.FullName,
personBuilder.Random.Number(10, 80),
personBuilder.Address.Street);
tasks.Add(repository.CreateAsync(person).AsTask());
}
await Task.WhenAll(tasks);
});
app.MapGet("/skipTake", async (
[FromServices] IRepository<Person> repository,
[FromQuery] int pageNumber,
[FromQuery] int pageSize) =>
{
IPageQueryResult<Person> result = await repository.PageAsync(
x => x.PartitionKey == nameof(Person),
pageNumber,
pageSize);
return new
{
requestUnits = result.Charge,
count = result.Total,
pages = result.TotalPages,
people = result.Items
};
});
app.MapGet("/tokenBased", async (
HttpContext context,
[FromServices] IRepository<Person> repository,
[FromQuery] int pageSize) =>
{
string? continuationToken =
context.Request.Headers["X-Continuation-Token"];
IPage<Person> result = await repository.PageAsync(
x => x.PartitionKey == nameof(Person),
pageSize,
continuationToken);
context.Response.Headers["X-Continuation-Token"] =
result.Continuation;
return new
{
requestUnits = result.Charge,
count = result.Total,
people = result.Items
};
});
app.Run();