-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDemoRegistrationMethods.cs
90 lines (77 loc) · 3.43 KB
/
DemoRegistrationMethods.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
namespace SimCube.Spartan.ExampleConsole;
/// <summary>
/// This is a simple example of how to use the Spartan API.
/// </summary>
public static class DemoRegistrationMethods
{
/// <summary>
/// Adds support services used by the demo. Not Required directly by spartan.
/// </summary>
/// <param name="services">The services collection to populate.</param>
/// <returns>The populated service collection.</returns>
public static IServiceCollection RegisterRequiredServicesForDemo(this IServiceCollection services)
{
services.AddEndpointsApiExplorer();
services.AddSwaggerGen();
services.Configure<SwaggerGeneratorOptions>(options => options.InferSecuritySchemes = true);
services.AddOutputCache(options =>
{
options.AddPolicy(nameof(GetExampleCachedRequest), policyBuilder =>
{
policyBuilder.Cache()
.Expire(TimeSpan.FromSeconds(10));
});
});
services.AddProblemDetails();
return services;
}
/// <summary>
/// This method shows how to manually register mediated requests.
/// </summary>
/// <param name="app">The web application instance.</param>
/// <returns>The web application instance with the mediated requests mapped.</returns>
public static WebApplication AddExampleManualMaps(this WebApplication app)
{
app.MediatedGet<GetExampleRequest>("example/{name}/{age}");
app.MediatedPatch<PatchExampleRequest>("example/{name}/{age}");
return app;
}
/// <summary>
/// This method shows how to automatically register mediated requests using the attribute.
/// </summary>
/// <param name="app">The web application instance.</param>
/// <returns>The web application instance with the mediated requests mapped.</returns>
public static WebApplication AddExampleDiscoveredMaps(this WebApplication app)
{
app.AddMediatedEndpointsFromAttributes();
return app;
}
/// <summary>
/// This method shows how to add grouped mapping to the app route instance..
/// </summary>
/// <param name="app">The web application instance.</param>
/// <returns>The web application instance with the mediated requests mapped.</returns>
public static WebApplication AddExampleGroupedMaps(this WebApplication app)
{
var group = app.MapGroup("/group-example");
// Register the requests on the type array parameter if using the attribute, passing multiple types into the group (can be chained directly on the RouteBuilder instance).
group.AddMediatedEndpointsToGroup(typeof(GetExampleGroupRequest));
// Or manually register the requests on the group if they do not have the attribute.
group.MediatedGet<GetExampleTwoGroupRequest>("/example-two/{name}/{age}");
group.MediatedGet<GetExampleThreeGroupRequest, Results<Ok<string>, NotFound>>("/example-three/{name}/{age}");
return app;
}
/// <summary>
/// Adds support services used by the demo. Not Required directly by spartan.
/// </summary>
/// <param name="app">The web application instance.</param>
/// <returns>The configured web application instance.</returns>
public static WebApplication AddSupportServiceForDemoToApp(this WebApplication app)
{
app.UseStatusCodePages();
app.UseOutputCache();
app.UseSwagger();
app.UseSwaggerUI();
return app;
}
}